Skip to content

Framework integration

NextPDF Symfony

Symfony 7 bundle with full DI integration and Messenger support.

Free · Apache-2.0Symfony ^7.2 · NextPDF ^3.0 · PHP 8.4+

NextPDF Symfony provides first-class Symfony 7 integration for NextPDF. It ships a fully auto-discovered Bundle with TreeBuilder configuration, an injectable PdfFactory service, streaming HTTP responses with OWASP security headers, Messenger-based async PDF generation, and compile-time detection of optional NextPDF extensions (Artisan, Pro). All services are worker-safe for FrankenPHP and RoadRunner deployments.

How it fits

Built into your workflow

Typed config, compiled in

A TreeBuilder configuration tree exposes 40+ typed keys that compile straight down into the service container. A dedicated compiler pass auto-detects the Artisan and Pro engines, so the right services are wired without manual configuration. What you declare in config is what gets resolved at build time.

Dispatch and handle async

Dispatch a GeneratePdfMessage onto any Messenger transport and let a handler pick it up out of the request cycle. The handler renders the document asynchronously and emits the finished PDF when the work completes. Heavy generation stays off the critical path of your web requests.

Worker-safe by design

The injectable PdfFactory builds pre-configured Document instances inside a long-running worker, with the FontRegistry singleton guarded by a lock and the ImageRegistry tagged for kernel.reset. State is cleared between requests, so the same process stays clean run after run. The bundle is built for FrankenPHP and RoadRunner workers.

Get started

Install

terminal
composer require nextpdf/symfony

In code

How it works

Controller with PdfFactory

php
use Symfony\Component\HttpFoundation\Response;
use NextPDF\Symfony\Http\PdfResponse;
use NextPDF\Symfony\Service\PdfFactory;

public function download(PdfFactory $pdf, Invoice $invoice): Response
{
    $doc = $pdf->create();
    $doc->addPage();
    $doc->cell(0, 10, "Invoice #{$invoice->getNumber()}", newLine: true);

    return PdfResponse::download($doc, "invoice-{$invoice->getNumber()}.pdf");
}

Async generation via Messenger

php
use Symfony\Component\Messenger\MessageBusInterface;
use NextPDF\Symfony\Message\GeneratePdfMessage;

public function generate(MessageBusInterface $bus): Response
{
    $bus->dispatch(new GeneratePdfMessage(
        builderClass: MonthlyReportBuilder::class,
        outputPath: '/var/storage/reports/january.pdf',
        builderContext: ['month' => 1, 'year' => 2026],
    ));

    return new Response('PDF generation queued.', 202);
}

Capabilities

What you get

  • PdfFactory — injectable service that creates pre-configured Document instances from YAML config
  • Messenger — dispatch GeneratePdfMessage to any transport; the handler resolves your builder
  • TreeBuilder — 40+ typed config keys with validation, defaults, and %kernel.*% parameter support
  • Worker-safe — FontRegistry singleton with lock, ImageRegistry tagged with kernel.reset
  • Flex auto-discovery — composer require activates the bundle instantly
  • PdfResponse — inline, download, and streamed variants, all with OWASP headers
  • Extension detection — compiler pass auto-configures Artisan and Pro when installed

When to use it

Use this for Symfony 7 applications where you want an injectable PdfFactory, typed bundle configuration, and Messenger-based async generation. Worker-safe services make it a fit for FrankenPHP and RoadRunner.