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],
];
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).
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');
}
}
templates/creavio_pdf/ (e.g., invoice_template.html.twig).{{ pdf_service }} in templates to access mPDF-specific functions (e.g., {{ pdf_service.setCompression(true) }}).Template-Based PDFs
$pdfService->generate('template_name', ['user' => $user, 'items' => $items]);
base_layout.html.twig) for headers/footers.Direct mPDF Usage Access the underlying mPDF instance for advanced features:
$mpdf = $pdfService->getMpdf();
$mpdf->SetCompression(true);
$mpdf->WriteHTML($htmlContent);
$pdfService->output('custom.pdf');
Twig Extensions Leverage mPDF functions in templates:
{{ pdf_service.setDefaultFont('dejavusans') }}
{{ pdf_service.setFontSize(10) }}
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');
Deprecated Package
Template Paths
templates/creavio_pdf/; hardcoded in the bundle.creavio_pdf:
template_dir: '%kernel.project_dir%/templates/custom_pdf'
Memory Limits
$pdfService->stream('large_report.pdf', ['Compression' => true]);
Font Issues
mpdf/fonts/. The bundle assumes default paths.mpdf_path in config points to a valid mPDF installation with fonts.creavio_pdf:
debug: true
$mpdf = $pdfService->getMpdf();
if ($mpdf->errorCode) {
throw new \RuntimeException($mpdf->errorInfo());
}
Custom mPDF Config Override mPDF settings globally in config:
creavio_pdf:
mpdf:
format: 'A4-L'
default_font: 'dejavusans'
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' }
Service Override
Replace PdfService for custom logic:
services:
Creavio\PdfBundle\Service\PdfService:
alias: 'app.custom_pdf_service'
public: true
How can I help you explore Laravel packages today?