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

Mpdf Bundle Laravel Package

akyos/mpdf-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Bundle

    composer require akyos/mpdf-bundle
    
  2. Register the Bundle Add to config/bundles.php (Symfony 4+):

    Akyos\MpdfBundle\AkyosMpdfBundle::class => ['all' => true],
    

    (For Symfony 2.x, follow the AppKernel.php instructions in the README.)

  3. First Use Case: Generate a PDF Response Inject the PDFService into a controller and return a PDF:

    use Akyos\MpdfBundle\Response\PDFResponse;
    use Akyos\MpdfBundle\Service\PDFService;
    
    class MyController extends AbstractController
    {
        public function generatePdf(PDFService $pdfService)
        {
            return new PDFResponse($pdfService->generatePdf('Hello, PDF!'));
        }
    }
    

Implementation Patterns

Core Workflow

  1. Service Injection Use dependency injection to access PDFService:

    public function __construct(private PDFService $pdfService) {}
    

    (Symfony’s autowiring handles this automatically.)

  2. Dynamic PDF Generation Pass HTML or raw text with options:

    $pdfContent = $this->pdfService->generatePdf(
        '<h1>Dynamic Content</h1><p>Generated at ' . now() . '</p>',
        ['format' => 'A4', 'default_font_size' => 10]
    );
    
  3. Reusable PDF Templates Store HTML templates in Twig and render them:

    $html = $this->renderView('pdf/template.html.twig', ['data' => $data]);
    return new PDFResponse($this->pdfService->generatePdf($html));
    
  4. Streaming Large PDFs Stream directly to the browser to avoid memory issues:

    $response = new PDFResponse($this->pdfService->generatePdf($html));
    $response->setContentDisposition('attachment; filename="report.pdf"');
    return $response;
    

Integration Tips

  • Twig Integration Extend Twig to include PDF generation logic:

    {{ app.service('akyos_mpdf.pdf').generatePdf(content) }}
    

    (Requires custom Twig extension.)

  • Queueing for Async Generation Use Symfony Messenger to offload PDF generation:

    $this->messageBus->dispatch(new GeneratePdfMessage($data));
    

    (Implement a handler to process and store the PDF.)

  • Custom mPDF Configuration Override default settings via config/packages/akyos_mpdf.yaml:

    akyos_mpdf:
        default_options:
            format: 'A4'
            default_font: 'dejavusans'
    

Gotchas and Tips

Pitfalls

  1. Memory Limits

    • Issue: Large HTML or complex PDFs may hit PHP’s memory_limit.
    • Fix: Stream output or increase memory_limit in php.ini.
  2. Font Paths

    • Issue: Custom fonts require absolute paths.
    • Fix: Use addFont() in the mPDF instance:
      $mpdf = $this->pdfService->getMpdf();
      $mpdf->addFont('custom_font', 'path/to/font');
      
  3. Deprecated Symfony Versions

    • Issue: The bundle targets Symfony 2.x–4.x but lacks Symfony 5+ support.
    • Fix: Manually patch or fork the bundle for newer Symfony versions.
  4. Caching Headaches

    • Issue: Cached Twig templates may break PDF generation if variables change.
    • Fix: Disable caching for PDF-related templates or use unique filenames.

Debugging

  • Inspect mPDF Instance Access the raw mPDF object for debugging:

    $mpdf = $this->pdfService->getMpdf();
    $mpdf->debug = true; // Enable debug mode
    
  • Log Errors Wrap generation in a try-catch:

    try {
        $pdf = $this->pdfService->generatePdf($html, $options);
    } catch (\Exception $e) {
        $this->addFlash('error', $e->getMessage());
        throw $e;
    }
    

Extension Points

  1. Custom Response Headers Extend PDFResponse to add headers:

    $response = new PDFResponse($pdfContent);
    $response->headers->set('X-Custom-Header', 'value');
    
  2. Post-Processing Hooks Override the service to add logic before/after generation:

    # config/services.yaml
    services:
        App\Service\CustomPdfService:
            decorates: 'akyos_mpdf.pdf'
            arguments: ['@akyos_mpdf.pdf']
    
  3. Storage Integration Save generated PDFs to storage (e.g., S3) before returning:

    $path = $this->storage->put('reports/' . uniqid() . '.pdf', $pdfContent);
    return $this->redirectToRoute('download_pdf', ['path' => $path]);
    
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.
yandex/translate-api
voku/simple_html_dom
league/flysystem-vfs
bkwld/upchuck
filament/spatie-laravel-tags-plugin
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php