Installation:
composer require bushidoio/pdf-bundle:dev-master
Enable the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3-):
BushidoIO\PDFBundle\BushidoIOPDFBundle::class => ['all' => true],
First Use Case: Generate a PDF from an HTML string in a controller:
use BushidoIO\PDFBundle\Service\PDFService;
public function generatePdf(PDFService $pdfService)
{
$html = '<h1>Hello, PDF!</h1><p>This is generated from HTML.</p>';
$pdf = $pdfService->htmlToPdf($html);
return new Response($pdf, 200, ['Content-Type' => 'application/pdf']);
}
Key Files:
config/packages/bushidoio_pdf.yaml (Symfony 4+)app/config/config.yml (Symfony 3-)bushidoio_pdf (autowired or fetched via get()).HTML-to-PDF Conversion:
Inject PDFService into controllers/services and use:
$pdfContent = $pdfService->htmlToPdf($htmlString, [
'format' => 'A4', // Optional: 'A3', 'A5', etc.
'orientation' => 'portrait', // 'landscape'
'margin_top' => 10, // in mm
'margin_bottom' => 10,
'margin_left' => 10,
'margin_right' => 10,
]);
Response Handling:
Return a Response with PDF content:
return new Response(
$pdfContent,
200,
['Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="document.pdf"']
);
Dynamic HTML Generation: Combine with Twig to render templates before conversion:
$html = $this->renderView('path/to/template.html.twig', ['data' => $data]);
$pdf = $pdfService->htmlToPdf($html);
Font Management:
Place custom TTF fonts in ttffontdatapath (configured in config.yml) and reference them in HTML:
<style>@font-face { font-family: 'CustomFont'; src: url('/fonts/CustomFont.ttf'); }</style>
Ensure the path is accessible from the HTML context.
Caching:
Use the tmp directory (default: app/cache/bushidoio_pdf/tmp) for temporary files. Clear it manually if needed:
rm -rf var/cache/bushidoio_pdf/tmp/*
Batch Processing:
For multiple PDFs, reuse the PDFService instance (it’s a singleton) and loop through HTML strings:
foreach ($htmlStrings as $html) {
$pdfs[] = $pdfService->htmlToPdf($html);
}
Symfony Forms: Generate PDFs from form submissions:
public function submitFormAction(Request $request, PDFService $pdfService)
{
$form = $this->createForm(...);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$html = $this->renderView('form/pdf_template.html.twig', ['form' => $form->createView()]);
return new Response($pdfService->htmlToPdf($html), 200, ['Content-Type' => 'application/pdf']);
}
}
Font Paths:
ttffontdatapath in config.yml is correct and writable./fonts/CustomFont.ttf must resolve to the actual file).Permissions:
tmp and ttffontdatapath directories must be writable by the web server user.chmod -R 775 var/cache/bushidoio_pdf (adjust paths as needed).HTML/CSS Limitations:
position: absolute, float) may render unpredictably in PDFs.Memory Issues:
ini_set('memory_limit', '512M') in a controller or service constructor if needed.Dev-Master Dependency:
dev-master state, which may introduce breaking changes.Check Temporary Files:
Inspect var/cache/bushidoio_pdf/tmp/ for generated files (e.g., .html or .pdf intermediates).
Log Errors: Enable Symfony’s monolog to catch exceptions:
# config/packages/monolog.yaml
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
Validate HTML: Use browser dev tools to ensure HTML renders correctly before PDF conversion.
Customize PDF Options: Extend the service by overriding the default configuration:
# config/packages/bushidoio_pdf.yaml
bushidoio_pdf:
tmp: "%kernel.project_dir%/var/cache/bushidoio_pdf/tmp"
ttffontdatapath: "%kernel.project_dir%/public/fonts"
default_options:
format: 'Letter'
orientation: 'landscape'
Add Headers/Footers:
Inject custom HTML via the options array:
$pdfService->htmlToPdf($html, [
'header_html' => '<div style="text-align: center;">Header Content</div>',
'footer_html' => '<div style="text-align: center;">Footer Content</div>',
]);
Event Listeners:
Hook into PDF generation by creating a custom event subscriber (if the bundle supports events; check the source for Events class).
Alternative Renderers: If the underlying library (e.g., Dompdf or wkhtmltopdf) is configurable, extend the bundle’s service to pass additional options:
// Override the service in config/services.yaml
services:
BushidoIO\PDFBundle\Service\PDFService:
arguments:
$options: ['custom_option' => 'value']
How can I help you explore Laravel packages today?