4,000 emails/month for free | Mailtrap sends real emails now!

Driver-Based Architecture in Spatie's Laravel PDF v2

Last updated on by

Driver-Based Architecture in Spatie's Laravel PDF v2 image

Spatie's laravel-pdf v2 introduces a driver-based architecture for PDF generation, letting you choose between Browsershot, Cloudflare Browser Rendering, DomPdf, and — as of v2.1.0 — Gotenberg. This release also adds queued PDF generation, PDF metadata support, runtime driver switching, and custom driver support.

  • Driver-based architecture with Browsershot, Cloudflare, and DomPdf drivers
  • Gotenberg driver added in v2.1.0
  • Queued PDF generation
  • PDF metadata support (title, author, subject, keywords, creator, creation date)
  • Runtime driver switching and custom drivers

What's New

Driver-Based Architecture

The biggest change in v2 is the move to a driver-based system. Instead of being locked into Browsershot, you can now choose the PDF backend that fits your infrastructure:

  • Browsershot — Chromium-based rendering via Puppeteer (remains the default)
  • Cloudflare — Generate PDFs using Cloudflare's Browser Rendering API
  • DomPdf — Pure PHP rendering with no external binary required

Set your default driver in the config file, or switch at runtime:

use Spatie\LaravelPdf\Facades\Pdf;
 
Pdf::view('pdfs.invoice', ['invoice' => $invoice])
->driver('dompdf')
->format('a4')
->save('invoice.pdf');

You can also build custom drivers for backends the package doesn't ship with.

Gotenberg Driver (v2.1.0)

The day after v2's release, v2.1.0 added a built-in Gotenberg driver. Gotenberg is a Docker-based API for HTML-to-PDF conversion using headless Chromium. The driver sends HTML as multipart form-data to Gotenberg's conversion endpoint and supports all standard PDF options: paper size, margins, orientation, scale, page ranges, headers, and footers. It uses Laravel's built-in Http facade, so no additional Composer dependencies are required.

PR: #302

Queued PDF Generation

PDF generation can now be offloaded to a queue with saveQueued(). You can chain a callback to run after the PDF is saved — for example, sending an email with the generated file:

use Spatie\LaravelPdf\Facades\Pdf;
 
Pdf::view('pdfs.invoice', ['invoice' => $invoice])
->format('a4')
->saveQueued('invoice.pdf')
->then(fn (string $path, ?string $diskName) =>
Mail::to($user)->send(new InvoiceMail($path))
);

The method also accepts optional $connection and $queue parameters.

PDF Metadata

v2 adds a meta() method for setting PDF document metadata:

use Spatie\LaravelPdf\Facades\Pdf;
 
Pdf::view('pdfs.invoice', ['invoice' => $invoice])
->meta(
title: 'Invoice #1234',
author: 'Acme Corp',
subject: 'Monthly Invoice',
keywords: 'invoice, billing',
creator: 'Laravel PDF',
creationDate: now(),
)
->save('invoice.pdf');

Check the documentation for more details on formatting PDFs.

Upgrade Notes

This is a major release with breaking changes:

  • spatie/browsershot must now be explicitly required via Composer if you use the Browsershot driver
  • getBrowsershot() has been removed — use withBrowsershot() instead
  • The config file structure has changed (new driver key) — republish your config
  • Laravel 10 support has been dropped

References

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Livewire v4.2.0 Released with Security Hardening and Laravel 13 Support image

Livewire v4.2.0 Released with Security Hardening and Laravel 13 Support

Read article
Filament through the years image

Filament through the years

Read article
Boost Guidelines & Skills Added in Inertia v2.3.16 image

Boost Guidelines & Skills Added in Inertia v2.3.16

Read article
Laravel Launches an Open Directory of AI Agent Skills for Laravel and PHP image

Laravel Launches an Open Directory of AI Agent Skills for Laravel and PHP

Read article
Filament turns five in style! image

Filament turns five in style!

Read article
Cache Concurrency Limiting in Laravel 12.53.0 image

Cache Concurrency Limiting in Laravel 12.53.0

Read article