Install the Bundle
composer require akyos/mpdf-bundle
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.)
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!'));
}
}
Service Injection
Use dependency injection to access PDFService:
public function __construct(private PDFService $pdfService) {}
(Symfony’s autowiring handles this automatically.)
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]
);
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));
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;
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'
Memory Limits
memory_limit.memory_limit in php.ini.Font Paths
addFont() in the mPDF instance:
$mpdf = $this->pdfService->getMpdf();
$mpdf->addFont('custom_font', 'path/to/font');
Deprecated Symfony Versions
Caching Headaches
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;
}
Custom Response Headers
Extend PDFResponse to add headers:
$response = new PDFResponse($pdfContent);
$response->headers->set('X-Custom-Header', 'value');
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']
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]);
How can I help you explore Laravel packages today?