- How do I install spatie/laravel-pdf in a Laravel project?
- Run `composer require spatie/laravel-pdf` in your project directory. The package auto-discovers Laravel and no additional configuration is needed for basic usage. For specific drivers like Browsershot or Gotenberg, follow their individual setup guides.
- Which PDF driver should I choose for modern CSS support?
- Use **Browsershot** or **Cloudflare Browser Rendering** for full modern CSS support (Flexbox, Grid). These Chromium-based drivers render HTML like a real browser. For simpler layouts, **DOMPDF** works but lacks advanced CSS features.
- Can I generate PDFs asynchronously in Laravel queues?
- Yes, dispatch a job to generate the PDF and return it later. Use `Pdf::view()->save()` in the job’s `handle()` method. For Chromium-based drivers, ensure the worker has access to required dependencies (e.g., Docker for Gotenberg).
- Does spatie/laravel-pdf support Laravel 10 or 11?
- The package is fully compatible with Laravel 10 and 11. It leverages Laravel’s service container and facades, so no version-specific changes are required. Always check the [GitHub releases](https://github.com/spatie/laravel-pdf/releases) for updates.
- How do I test PDF generation in Laravel’s testing environment?
- Use `Pdf::fake()` to mock PDF generation in tests. Assert the view was rendered with `Pdf::assertSaved()` or `Pdf::assertDownloaded()`. For complex layouts, manually verify the output by saving the PDF and comparing it to a reference file.
- What are the memory/performance implications of Chromium-based drivers?
- Chromium drivers (Browsershot, Chrome-PHP) consume significant memory and CPU, especially for large or complex PDFs. For production, use queues or async processing. If scaling is critical, consider **Gotenberg** (Docker) with a dedicated service or **Cloudflare Browser Rendering** for offloaded processing.
- Is there a fallback driver if my primary one fails?
- The package doesn’t enforce a fallback by default, but you can implement logic in your controller to catch exceptions (e.g., `try-catch`) and switch to a secondary driver like DOMPDF. Example: `try { Pdf::view()->save(); } catch (...) { Pdf::setDriver('dompdf')->save(); }`
- How do I add headers/footers or custom metadata to PDFs?
- Use the fluent API methods: `Pdf::view()->setOptions(['title' => 'Invoice', 'subject' => 'Payment'])`, or add headers/footers via Blade templates (e.g., `@include('pdfs.header')`). For driver-specific options like page margins, consult the [documentation](https://spatie.be/docs/laravel-pdf).
- Can I use this package outside Laravel (e.g., plain PHP or Symfony)?
- The package is Laravel-specific and relies on its service container and Blade integration. For non-Laravel use, manually bind the PDF service or adapt the logic to your framework’s DI container. Consider alternatives like **DOMPDF** or **SnappyPDF** for broader compatibility.
- Are there security risks with user-provided HTML/CSS in PDF templates?
- Yes, user-provided HTML/CSS can lead to CSS injection or malicious payloads. Sanitize inputs using Laravel’s `Str::of()` or libraries like **HTML Purifier**. Avoid dynamic CSS from untrusted sources, especially with Chromium drivers that execute JavaScript.