spatie/laravel-pdf
Generate PDFs from Laravel Blade views with a simple fluent API. Choose drivers like Browsershot/Chromium, Gotenberg, Cloudflare Browser Run, WeasyPrint, DOMPDF, or chrome-php. Use modern CSS, set page formats, and stream or save PDFs.
Strengths:
chrome-php/chrome), aligning with modern Laravel modularity.Pdf::view()), and Laravel’s response system, reducing boilerplate.PdfBuilder is Macroable), and queued PDF generation (saveQueued()), making it adaptable to niche use cases.Pdf::fake() and assertions simplify unit/integration testing for PDF logic.Limitations:
return Pdf::view()->name('file.pdf')).saveQueued()) mitigate this but add complexity.Pdf::fake()) requires understanding of the driver’s behavior.pdftotext?)Laravel-Centric:
Pdf::view('view.name', ['data' => $data])).toMailAttachment() for emails.saveQueued() leverages Laravel Queues for async generation (e.g., with Redis or database queues).Driver Compatibility:
| Driver | Laravel Fit | Requirements | Best For |
|---|---|---|---|
| Browsershot | High | Chromium, Puppeteer | Modern CSS, dynamic content |
| Gotenberg | High | Docker, Gotenberg service | Scalable, headless Chromium |
| Cloudflare | Medium | Cloudflare account, API token | Serverless, low-maintenance |
| WeasyPrint | Medium | Python, WeasyPrint installed | CSS Paged Media, static content |
| DOMPDF | High | None | Zero-dependency, simple layouts |
| chrome-php | Medium | Chrome-PHP extension | Lightweight Chromium alternative |
Email Integration:
toMailAttachment() simplifies attaching PDFs to Laravel Mailables or notifications.php artisan vendor:publish --provider="Spatie\Pdf\PdfServiceProvider".config/pdf.php to set the default driver and options (e.g., Chromium paths, Gotenberg URL).Pdf::view() or Pdf::loadView().// Before (legacy)
$pdf = PDF::loadView('invoice', ['data' => $data])->stream();
// After (spatie/laravel-pdf)
return Pdf::view('pdfs.invoice', ['invoice' => $invoice])
->driver('browsershot')
->name('invoice.pdf');
Pdf::fake() in unit tests to mock PDF generation.spatie/pdf-to-text for text extraction).brew install chromedriver, Docker for Gotenberg).GOTENBERG_URL, CLOUDFLARE_API_TOKEN).Pdf::view() for critical PDFs (e.g., invoices, reports).Pdf::fake().dompdf as secondary driver).config/pdf.php.How can I help you explore Laravel packages today?