Skip to content

Symfony Package

TCPDF-Next Symfony
Symfony · LGPL-3.0

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.

Installation

bash
composer require yeeefang/tcpdf-next-symfony

Requirements: 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],
];

Configuration

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.0

PdfFactory 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.

Package Contents

ClassDescription
TcpdfNextBundleBundle registration and service wiring
PdfFactoryDI-friendly factory for creating PDF documents
PdfResponseHTTP response with security headers
GeneratePdfMessageMessenger message for async generation
GeneratePdfMessageHandlerHandles async PDF generation

Released under the LGPL-3.0-or-later License.