spatie/laravel-pdf

repository·main·Indexed 21 days ago

https://github.com/spatie/laravel-pdf

A Laravel package providing a unified API to generate PDFs from Blade views using various drivers, including Browsershot, Gotenberg, WeasyPrint, DOMPDF, Cloudflare Browser Run, and chrome-php/chrome. It features PDF caching, support for Tailwind CSS, custom macros for the PdfBuilder, and readiness flags for JavaScript-heavy views. The package also includes a Laravel Boost skill for AI agents.

Tokens
29K
Snippets
124
Records
158
Agent score
74%

What's inside spatie/laravel-pdf

  1. Configure Chrome flags for local assets

    main

    When your PDF templates reference local assets like CSS, images, or fonts, Chrome may block them due to security restrictions. To resolve this, use withBrowsershot to pass specific Chrome flags to the instance.

    Commonly used flags include:

    • --disable-web-security: Disables Chrome's same-origin policy and other web security features.
    • --allow-file-access-from-files: Allows local files to access other local files.
  2. Understand the trade-offs of Chrome Headless PDF generation

    main

    Laravel PDF generates PDFs using Chrome Headless.

    Pros:

    • Supports any CSS you want, ensuring high fidelity and correct rendering of modern web layouts.

    Cons:

    • Generating PDFs via Chromium can be resource-intensive compared to pure PHP-based engines.
  3. How PDF cache keys are determined

    main

    The default cache key is automatically derived from all factors influencing the output: rendered HTML, headers, footers, formatting options, metadata, and encryption settings. This ensures that any change in the PDF content results in a new cache entry.

    If you want to use a stable identifier (like an invoice ID) as the cache key, pass it as the key argument to the cache() method.

    Pdf::view('pdf.invoice', ['invoice' => $invoice])
        ->cache(key: "invoice-{$invoice->id}")
        ->save('invoice.pdf');
  4. Available PDF drivers

    main

    The package supports several drivers depending on your infrastructure and CSS requirements:

    • Browsershot (Chromium): Best for modern CSS (Grid, Flexbox).
    • Gotenberg (Docker-based): A containerized solution.
    • Cloudflare Browser Run: Cloudflare-specific runtime.
    • WeasyPrint (Python-based): Excellent for CSS Paged Media support.
    • DOMPDF (pure PHP): Best for zero-dependency setups.
    • chrome-php/chrome (Chromium): Another Chromium-based option.
  5. How headers, footers, and page numbers work in WeasyPrint

    main

    WeasyPrint utilizes CSS Paged Media to handle repeating elements.

    • Headers and Footers: When using headerHtml() or footerHtml(), the driver wraps content in elements using CSS position: running(), placing them into @page margin boxes so they repeat on every page.
    • Page Numbers: You can use the @pageNumber and @totalPages Blade directives. These leverage CSS counters (counter(page) and counter(pages)) to display dynamic page numbering.
  6. Execute JavaScript during PDF generation

    main

    When using browser-based drivers such as Browsershot, Cloudflare, or Chrome, JavaScript included in your HTML will be executed during the PDF creation process. This is useful for rendering charts or dynamic content via JS libraries.

    Note: The DomPDF driver does not support JavaScript execution.

    {{-- Example Blade view with JS --}}
    <div id="target"></div>
    
    <script>
        document.getElementById('target').innerHTML = 'hello';
    </script>
    {{-- Rendering the view --}}
    use Spatie\LaravelPdf\Facades\Pdf;
    
    Pdf::view('your-view')->save($pathToPdf);
  7. Understand the driver-based architecture

    main

    The package uses a driver-based architecture, allowing you to choose a PDF generation backend based on your environment and CSS requirements.

    Available Drivers:

    • Browsershot (default): Uses Chromium via Browsershot. Requires Node.js and a Chrome/Chromium binary. Supports modern CSS (Grid, Flexbox, Tailwind).
    • Gotenberg: Uses the Gotenberg Docker-based API. Ideal for containerized/microservice environments. Supports modern CSS.
    • Cloudflare: Uses Cloudflare's Browser Run API via HTTP. No Node.js or Chrome binary required. Supports modern CSS.
    • WeasyPrint: Uses WeasyPrint via pontedilana/php-weasyprint. Excellent for CSS Paged Media (repeating headers/footers, page counters). Requires the WeasyPrint binary.
    • DOMPDF: Uses dompdf/dompdf. Pure PHP with no external dependencies. Best for simple PDFs using CSS 2.1/some CSS 3. Does not support advanced modern layouts.
    • Chrome: Uses chrome-php/chrome to communicate directly with a local Chrome/Chromium 65+ binary.
  8. Select a PDF generation driver

    main

    The driver option determines which backend is used for PDF generation. You can set a global default in your configuration or switch drivers at runtime for specific PDF instances.

    Supported drivers:

    • browsershot (default)
    • chrome
    • cloudflare
    • dompdf
    • gotenberg
    • weasyprint
    // In config/laravel-pdf.php
    'driver' => env('LARAVEL_PDF_DRIVER', 'browsershot'),
    
    // Switching drivers at runtime
    use Spatie\LaravelPdf\Facades\Pdf;
    
    Pdf::view('invoice', $data)
        ->driver('cloudflare')
        ->save('invoice.pdf');
  9. How margins, headers, and footers work in DOMPDF

    main

    DOMPDF handles layout and repeating elements differently than Chromium-based drivers:

    • Margins: The driver injects an @page { margin: ... } CSS rule into your HTML. Margins are applied via CSS.
    • Headers and Footers: DOMPDF does not support repeating headers and footers on every page. Instead, headerHtml()/headerView() is prepended to the body, and footerHtml()/footerView() is appended to the body. They will only appear on the first page.