discustecnologia/pdf-report-bundle
Install Dependencies
Ensure wkhtmltopdf is installed on your system (Linux/macOS: sudo apt-get install wkhtmltopdf or brew install wkhtmltopdf; Windows: download from wkhtmltopdf.org).
Add the bundle via Composer:
composer require discustecnologia/pdf-report-bundle
Enable the Bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
DiscusTecnologia\PdfReportBundle\DiscusTecnologiaPdfReportBundle::class => ['all' => true],
First Use Case: Generate a Simple PDF In a controller, inject the service and generate a PDF from a Twig template:
use Symfony\Component\HttpFoundation\Response;
public function generateReportAction()
{
$pdfReport = $this->get('discus-tecnologia.pdf-report');
$pdfReport->addPages('default/report.twig', ['data' => 'Hello, PDF!']);
return $pdfReport->generate();
}
Create a Twig template at templates/default/report.twig:
<h1>My Report</h1>
<p>{{ data }}</p>
Template Structure
templates/default/header.twig, templates/default/footer.twig).pdfReportPageNumber and pdfReportTotalPages variables for pagination logic (e.g., multi-page reports).{% for i in ((pdfReportPageNumber -1) * 4)..(((pdfReportPageNumber * 4)-1) < (obj|length - 1) ? ((pdfReportPageNumber * 4)-1) : obj|length-1) %}
<div>{{ obj[i] }}</div>
{% endfor %}
Dynamic Data Binding
Pass data to templates via addPages():
$pdfReport->addPages('default/invoice.twig', [
'invoice' => $invoiceData,
'client' => $clientData,
]);
Styling and Layout
style="page-break-after: always;" for page breaks).$pdfReport->setMargins(10, 10, 10, 10); // top, right, bottom, left
$pdfReport->setHeader('default/header.twig');
$pdfReport->setFooter('default/footer.twig');
Integration with Forms/Entities
$users = $this->getDoctrine()->getRepository(User::class)->findAll();
$pdfReport->addPages('default/user_list.twig', ['users' => $users]);
batch filter for chunking data (e.g., 10 records per page):
{% for user in users|batch(10, 'page') %}
{# Render user data #}
{% endfor %}
Streaming Large PDFs For large datasets, stream the PDF to avoid memory issues:
$response = new Response();
$response->headers->set('Content-Type', 'application/pdf');
$pdfReport->generate()->stream($response);
return $response;
wkhtmltopdf Path Issues
wkhtmltopdf is in your PATH or configure the bundle’s path:
# config/packages/discus_tecnologia_pdf_report.yaml
discus_tecnologia_pdf_report:
wkhtmltopdf_path: '/usr/local/bin/wkhtmltopdf'
Twig Template Caching
php bin/console cache:clear
CSS/HTML Limitations
wkhtmltopdf has limited support. Use tables or simple layouts.wkhtmltopdf directly to isolate issues:
wkhtmltopdf input.html output.pdf
Memory Limits
memory_limit in php.ini or stream the response.Deprecated Bundle
knplabs/knp-snappy.Check wkhtmltopdf Logs
Run wkhtmltopdf manually with verbose output:
wkhtmltopdf -q input.html output.pdf
Inspect Generated HTML Save the Twig-rendered HTML to a file for debugging:
$html = $this->renderView('default/report.twig', ['data' => $data]);
file_put_contents('debug.html', $html);
Common Errors
wkhtmltopdf not installed or not in PATH.Customize wkhtmltopdf Options
Override default options via config:
discus_tecnologia_pdf_report:
options:
enable-javascript: true
print-media-type: true
Add Custom Headers/Footers Extend the bundle by creating a custom service:
// src/Service/CustomPdfReport.php
class CustomPdfReport extends \DiscusTecnologia\PdfReportBundle\Service\PdfReport
{
public function addCustomHeader($template) {
$this->setHeader($template);
// Add custom logic
}
}
Register as a service in services.yaml:
services:
App\Service\CustomPdfReport:
decorates: 'discus-tecnologia.pdf-report'
arguments: ['@discus-tecnologia.pdf-report.inner']
Use with API Platform For API-driven PDFs, create a custom action:
use ApiPlatform\Core\Action\ContextAwareActionTrait;
class GeneratePdfAction
{
use ContextAwareActionTrait;
public function __invoke($data, UrlGeneratorInterface $urlGenerator) {
$pdfReport = $this->container->get('discus-tecnologia.pdf-report');
$pdfReport->addPages('default/api_report.twig', ['data' => $data]);
return $pdfReport->generate();
}
}
How can I help you explore Laravel packages today?