This package provides a simple way to create PDFs in Laravel apps. It uses a driver-based architecture, so you can choose between different PDF generation backends:
- Browsershot (default): Uses Chromium via Browsershot to generate PDFs from HTML. Requires Node.js and a Chrome/Chromium binary.
- Gotenberg: Uses Gotenberg, an open-source Docker-based API with headless Chromium. Great for containerized and microservice environments.
- Cloudflare: Uses Cloudflare's Browser Rendering API to generate PDFs with a simple HTTP call. No Node.js or Chrome binary needed. This driver was inspired by a suggestion from Dries Vints.
- DOMPDF: Uses dompdf/dompdf for pure PHP PDF generation. No external binaries, no Node.js, no Docker — works everywhere PHP runs.
The Browsershot, Gotenberg, and Cloudflare drivers support modern CSS features like grid and flexbox, or even a framework like Tailwind, to create beautiful PDFs. The DOMPDF driver supports CSS 2.1 and some CSS 3 properties, making it ideal for simpler PDFs that don't need advanced layout features.
Here's a quick example:
use Spatie\LaravelPdf\Facades\Pdf;
Pdf::view('pdfs.invoice', ['invoice' => $invoice])
->format('a4')
->save('invoice.pdf')
This will render the Blade view pdfs.invoice with the given data and save it as a PDF file.
You can also return the PDF as a response from your controller:
use Spatie\LaravelPdf\Facades\Pdf;
class DownloadInvoiceController
{
public function __invoke(Invoice $invoice)
{
return Pdf::view('pdfs.invoice', ['invoice' => $invoice])
->format('a4')
->name('your-invoice.pdf');
}
}
You can also queue PDF generation for background processing:
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)));
You can use also test your PDFs:
use Spatie\LaravelPdf\Facades\Pdf;
it('can render an invoice', function () {
Pdf::fake();
$invoice = Invoice::factory()->create();
$this->get(route('download-invoice', $invoice))
->assertOk();
Pdf::assertRespondedWithPdf(function (PdfBuilder $pdf) {
return $pdf->contains('test');
});
});
##We got badges
