- How do I install spatie/laravel-pdf in a Laravel application?
- Install the package with Composer using composer require spatie/laravel-pdf. After installation, you can use the Pdf facade with Blade views and Laravel responses. Some drivers require additional setup, such as Chromium, Docker, or Python binaries, depending on which backend you choose.
- Which PDF drivers does spatie/laravel-pdf support and which one should I choose?
- The package supports Browsershot with Chromium, Gotenberg, Cloudflare Browser Run, WeasyPrint, DOMPDF, and chrome-php/chrome. Choose Chromium-based drivers when you need modern CSS such as Grid or Flexbox, WeasyPrint for strong CSS Paged Media support, or DOMPDF for a zero-dependency pure PHP setup. Your choice should also consider your existing infrastructure, such as Docker availability or serverless constraints.
- Does spatie/laravel-pdf support Laravel 11, 12, and 13?
- As of v2.4 and later, the package is compatible with Laravel 11, 12, and 13. Older Laravel versions can use the v1.x line for backward compatibility. Check the package requirements when planning an upgrade from v1 to v2 because the config structure and driver requirements changed.
- How do I generate a PDF from a Blade view and return it as a download in Laravel?
- Use the Pdf facade to render a Blade view, set the page format, and name the file. For example, return Pdf::view('pdfs.invoice', ['invoice' => $invoice])->format('a4')->name('your-invoice.pdf') from a controller. Laravel's response system handles streaming the generated PDF to the browser.
- Can I use modern CSS like Flexbox and Grid in PDFs generated with spatie/laravel-pdf?
- Yes, but support depends on the driver. Chromium-based drivers such as Browsershot and chrome-php/chrome support modern CSS features including Grid and Flexbox. WeasyPrint excels at paged media but may lack some CSS features, while DOMPDF can struggle with complex layouts and dynamic content.
- How do I save a generated PDF to local storage or S3?
- Call save() on the PDF builder with a file path, such as save('invoice.pdf'), to write the PDF to storage. The package integrates with Laravel's filesystem and disk system, so saving to local storage or S3 is straightforward. You can also return the PDF directly as a response instead of saving it.
- How do I test PDF generation with Pdf::fake()?
- The package includes Pdf::fake() and built-in assertions to simplify unit and integration testing of PDF logic. You can fake PDF generation and assert that the expected views, formats, or output paths were used without actually rendering a PDF. This avoids driver dependencies and speeds up your test suite.
- Can I generate PDFs asynchronously in a queued job?
- Yes, the package provides saveQueued() to generate and save PDFs from queued jobs. This is useful when using resource-intensive Chromium-based drivers or when handling high PDF volumes. Queueing moves PDF generation off the request cycle, but it adds complexity around job monitoring and failure handling.
- What are the system requirements for the Browsershot or Chromium driver?
- Browsershot requires Chromium and typically additional binaries or Node tooling, so it adds operational overhead compared with pure PHP drivers. You may need to manage Chromium sandboxing, memory usage, and process limits on your servers. Gotenberg similarly requires Docker, and Cloudflare Browser Run requires API access and may be subject to rate limits.
- How do I set PDF metadata such as title and author with spatie/laravel-pdf?
- The package supports setting PDF properties such as title, author, and other metadata through its fluent API. This is useful for compliance, archival requirements, or simply producing more professional documents. Metadata support depends on the selected driver, so verify the output if your use case has strict requirements.