- Which Laravel PDF package is best for generating invoices with modern CSS like Flexbox?
- Use `spatie/laravel-pdf` with the **Browsershot** or **Gotenberg** driver for full Chromium support, enabling modern CSS features like Flexbox and Grid. Both drivers render Blade views as PDFs with high fidelity. For simpler setups, **WeasyPrint** also handles CSS well but requires Python.
- How do I install and configure spatie/laravel-pdf for Laravel 10?
- Run `composer require spatie/laravel-pdf` and install a driver (e.g., `spatie/browsershot` for Chromium). No additional configuration is needed for basic use. For Gotenberg or Cloudflare, add their respective dependencies and configure environment variables (e.g., `GOTENBERG_URL` or `CLOUDFLARE_API_TOKEN`).
- Can I use DOMPDF with laravel-pdf, and what are its limitations?
- Yes, DOMPDF is supported as a zero-dependency driver. However, it lacks full modern CSS support (e.g., Flexbox/Grid) and may struggle with complex layouts. It’s ideal for lightweight projects where Chromium isn’t an option. Test thoroughly for rendering quirks in your Blade templates.
- How do I generate a PDF from a Blade view and return it as a downloadable response?
- Use the fluent API: `Pdf::view('view.name', ['data'])->name('filename.pdf')` in your controller. This automatically streams the PDF as a downloadable response. For queued generation, chain `->saveQueued()` to avoid blocking the request.
- Which driver should I choose for self-hosted PDF generation without external dependencies?
- For self-hosted setups, **DOMPDF** (pure PHP) or **WeasyPrint** (Python) are the best choices. DOMPDF requires no extra services but has CSS limitations, while WeasyPrint offers better Paged Media support. Avoid Chromium-based drivers (Browsershot/Gotenberg) if you can’t manage Docker or Chromium binaries.
- How do I test PDF generation in Laravel’s testing environment?
- Use `Pdf::fake()` to intercept PDF generation in tests. For assertions, install `pdftotext` (Poppler) to parse PDF content. Example: `Pdf::assertDownloaded('invoice.pdf')`. Note that fake testing doesn’t cover driver-specific behaviors (e.g., Chromium rendering quirks), so supplement with manual checks.
- What are the performance implications of using Browsershot or Gotenberg for PDFs?
- Chromium-based drivers (Browsershot/Gotenberg) are resource-intensive, consuming significant CPU/memory. For high-traffic apps, offload generation to queue jobs (`saveQueued()`) or use a dedicated server. Gotenberg (Docker) adds latency but scales better than local Chromium. Monitor response times under load.
- How do I handle failures if the Cloudflare Browser Run API is down?
- The package doesn’t include built-in fallbacks, but you can implement a custom driver or use a secondary driver (e.g., Browsershot) as a backup. Wrap the PDF generation in a try-catch block and log failures for monitoring. Consider caching PDFs to reduce API dependency.
- Can I customize PDF metadata (author, subject, keywords) with laravel-pdf?
- Yes, use the `->setOptions()` method to pass metadata to the underlying driver. For example: `Pdf::view(...)->setOptions(['title' => 'Invoice', 'author' => 'Your App'])` works with most drivers. Check each driver’s documentation for supported metadata fields (e.g., DOMPDF uses `setPaper()` and `setOption()`).
- Are there alternatives to laravel-pdf for generating PDFs in Laravel?
- Yes, alternatives include **barryvdh/laravel-dompdf** (DOMPDF wrapper), **snappy** (WkHTMLtoPDF), or **mikehaertl/phpwkhtmltopdf** (WkHTMLtoPDF). However, `laravel-pdf` stands out for its **driver flexibility**, **Blade integration**, and **modern CSS support** (via Chromium). Choose based on your need for simplicity (DOMPDF) or advanced rendering (Chromium).