Symfony Paket
The Symfony package provides first-class Symfony 7 integration for TCPDF-Next — DI container bindings, a PdfFactory service, Messenger-based async generation, and HTTP response helpers with OWASP security headers.
Installationation
bash
composer require yeeefang/tcpdf-next-symfonyAnforderungen: Symfony ^7.0, PHP ^8.5
The bundle is auto-configured via Symfony Flex. Register manually if needed:
php
// config/bundles.php
return [
// ...
Yeeefang\TcpdfNext\Symfony\TcpdfNextBundle::class => ['all' => true],
];Konfiguration
yaml
# config/packages/tcpdf_next.yaml
tcpdf_next:
fonts_directory: '%kernel.project_dir%/resources/fonts'
default_page_size: A4
default_orientation: portrait
auto_page_break: true
margin_bottom: 25.0PdfFactory Service
Inject the factory in your services or controllers:
php
use Yeeefang\TcpdfNext\Symfony\PdfFactory;
class InvoiceController extends AbstractController
{
public function __construct(
private readonly PdfFactory $pdfFactory,
) {}
#[Route('/invoice/{id}/pdf')]
public function download(Invoice $invoice): Response
{
$pdf = $this->pdfFactory->create()
->setTitle("Invoice #{$invoice->number}")
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, "Invoice #{$invoice->number}");
return $this->pdfFactory->response($pdf, "invoice-{$invoice->number}.pdf");
}
}HTTP Responses
The PdfResponse class returns PDFs with OWASP-recommended security headers:
php
use Yeeefang\TcpdfNext\Symfony\Http\PdfResponse;
// Inline display (browser preview)
return PdfResponse::inline($pdf, 'report.pdf');
// Force download
return PdfResponse::download($pdf, 'report.pdf');Messenger Integration
Dispatch PDF generation to a Messenger worker:
php
use Yeeefang\TcpdfNext\Symfony\Messenger\GeneratePdfMessage;
$this->bus->dispatch(new GeneratePdfMessage(
template: 'invoice',
data: ['invoice_id' => $invoice->id],
outputPath: "/tmp/invoice-{$invoice->id}.pdf",
));Worker-Safe (FrankenPHP / RoadRunner)
The bundle configures DocumentFactory as a singleton — font registries and image caches survive across requests in persistent workers. Works with FrankenPHP, RoadRunner, and any Symfony Runtime implementation.
Paket Contents
| Class | Beschreibung |
|---|---|
TcpdfNextBundle | Bundle registration and service wiring |
PdfFactory | DI-friendly factory for creating PDF documents |
PdfResponse | HTTP response with security headers |
GeneratePdfMessage | Messenger message for async generation |
GeneratePdfMessageHandler | Handles async PDF generation |