Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pdf Bundle Laravel Package

creavio/pdf-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the bundle via Composer:

    composer require creavio/pdf-bundle
    

    Enable it in config/bundles.php (Symfony):

    return [
        // ...
        Creavio\PdfBundle\CreavioPdfBundle::class => ['all' => true],
    ];
    
  2. Configuration Publish the default config (if needed):

    php bin/console creavio:pdf:install
    

    Verify config/packages/creavio_pdf.yaml exists and adjust paths (e.g., mpdf_path).

  3. First Use Case Generate a PDF from a Twig template:

    use Creavio\PdfBundle\Service\PdfService;
    
    class InvoiceController extends AbstractController
    {
        public function generatePdf(PdfService $pdfService)
        {
            $pdf = $pdfService->generate('invoice_template', [
                'invoice' => $this->getInvoiceData(),
            ]);
            return $pdf->stream('invoice.pdf');
        }
    }
    
    • Place Twig templates in templates/creavio_pdf/ (e.g., invoice_template.html.twig).
    • Use {{ pdf_service }} in templates to access mPDF-specific functions (e.g., {{ pdf_service.setCompression(true) }}).

Implementation Patterns

Core Workflows

  1. Template-Based PDFs

    • Dynamic Data: Pass variables to Twig templates:
      $pdfService->generate('template_name', ['user' => $user, 'items' => $items]);
      
    • Reusable Components: Extend base templates (e.g., base_layout.html.twig) for headers/footers.
  2. Direct mPDF Usage Access the underlying mPDF instance for advanced features:

    $mpdf = $pdfService->getMpdf();
    $mpdf->SetCompression(true);
    $mpdf->WriteHTML($htmlContent);
    $pdfService->output('custom.pdf');
    
  3. Twig Extensions Leverage mPDF functions in templates:

    {{ pdf_service.setDefaultFont('dejavusans') }}
    {{ pdf_service.setFontSize(10) }}
    

Integration Tips

  • Symfony Forms: Render forms as PDFs by converting form HTML to Twig:

    {{ form_start(form) }}
        {{ form_widget(form) }}
    {{ form_end(form) }}
    
  • Queueing Long-Generating PDFs: Use Symfony Messenger to offload PDF generation:

    $message = new GeneratePdfMessage($template, $data, $filename);
    $this->messageBus->dispatch($message);
    

    Implement a handler to use PdfService.

  • Storage: Save PDFs to disk or cloud storage:

    $pdfService->save('template', $data, 'path/to/invoice.pdf');
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package

    • Last release in 2017; mPDF has evolved since. Test thoroughly for compatibility (e.g., PHP 8.x, mPDF v8+).
    • Mitigation: Fork the repo or wrap the bundle in a service layer to isolate changes.
  2. Template Paths

    • Templates must be in templates/creavio_pdf/; hardcoded in the bundle.
    • Fix: Override the path in config:
      creavio_pdf:
          template_dir: '%kernel.project_dir%/templates/custom_pdf'
      
  3. Memory Limits

    • Complex PDFs may hit memory limits. Use mPDF’s streaming:
      $pdfService->stream('large_report.pdf', ['Compression' => true]);
      
  4. Font Issues

    • mPDF requires fonts in mpdf/fonts/. The bundle assumes default paths.
    • Solution: Verify mpdf_path in config points to a valid mPDF installation with fonts.

Debugging

  • Logs: Enable debug mode to trace template rendering:
    creavio_pdf:
         debug: true
    
  • Output Inspection: Dump mPDF errors:
    $mpdf = $pdfService->getMpdf();
    if ($mpdf->errorCode) {
        throw new \RuntimeException($mpdf->errorInfo());
    }
    

Extension Points

  1. Custom mPDF Config Override mPDF settings globally in config:

    creavio_pdf:
        mpdf:
            format: 'A4-L'
            default_font: 'dejavusans'
    
  2. Event Listeners Hook into PDF generation lifecycle (e.g., pre/post-processing):

    // src/EventListener/PdfListener.php
    public function onPdfGenerate(PdfGenerateEvent $event) {
        $event->getMpdf()->SetTitle('Custom Title');
    }
    

    Register in services.yaml:

    services:
        App\EventListener\PdfListener:
            tags:
                - { name: 'kernel.event_listener', event: 'creavio.pdf.generate', method: 'onPdfGenerate' }
    
  3. Service Override Replace PdfService for custom logic:

    services:
        Creavio\PdfBundle\Service\PdfService:
            alias: 'app.custom_pdf_service'
            public: true
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui