laravel-dompdf Documentation

repository·master·Indexed 27 days ago

https://github.com/barryvdh/laravel-dompdf

A Laravel wrapper for the Dompdf HTML to PDF converter, providing a Facade and integration for generating PDF documents within Laravel and Lumen applications. It supports loading content from Blade views, HTML strings, or files, and offers output options such as streaming, downloading, or saving to the filesystem. The package includes support for PDF/A-3b compliance, embedded XML for Factur-X/Zugferd, and configurable options for paper size, orientation, and remote asset loading.

Tokens
4.6K
Snippets
15
Records
35
Agent score
92%

What's inside laravel-dompdf

  1. Identify key Laravel DOMPDF components and files

    master

    The package is composed of several key components that manage the integration between Laravel and the underlying Dompdf engine:

    • ServiceProvider (Barryvdh\DomPDF\ServiceProvider): Registers the dompdf, dompdf.options, and dompdf.wrapper bindings and handles publishing the configuration file.
    • PDF Wrapper (src/PDF.php): The main class used by the Pdf or PDF facade. It provides the primary API for loading content and generating output.
    • Configuration (config/dompdf.php): The central location for DOMPDF defaults and security settings.
    • Facades: You can access the package functionality using the Pdf or PDF aliases.
  2. Best Practices for Blade PDF Views

    master

    To ensure high-quality PDF rendering from Blade templates, follow these guidelines:

    • Character Encoding: Add <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> to the <head> of your view to support non-ASCII text.
    • CSS: Use print-friendly HTML and conservative CSS. Use CSS page-break properties for multi-page layouts.
    • Assets: Prefer absolute filesystem paths or confirmed reachable URLs for images and fonts.
    • Fonts: If fonts render incorrectly, verify your font_dir and font_cache settings in the configuration.
  3. Enable PDF/A-3b support

    master

    PDF/A-3b support requires dompdf >= 3.1.0 and the CPDF backend (which is the default). You can enable it globally in config/dompdf.php or per-PDF at runtime.

    Warning: PDF/A requires all fonts to be embedded. Do not use core PDF fonts like Helvetica, Courier, or Times; use fonts like DejaVu Sans or DejaVu Serif instead.

    // Global configuration in config/dompdf.php
    'pdfa' => [
        'enabled' => true,
    ],
    
    // Per-PDF at runtime
    $pdf = Pdf::loadView('invoice', $data)
        ->setPdfA()
        ->download('invoice.pdf');
  4. Generate PDFs using the Pdf Facade

    master

    You can generate PDFs by loading a view, an HTML string, or an HTML file. Once loaded, you can stream the PDF to the browser, download it, or save it to a file.

    use Barryvdh\DomPDF\Facade\Pdf;
    
    // Load a view with data and download
    $pdf = Pdf::loadView('pdf.invoice', $data);
    return $pdf->download('invoice.pdf');
    
    // Or chain methods to load a file, save it, and stream it
    return Pdf::loadFile(public_path().'/myfile.html')
        ->save('/path-to/my_stored_file.pdf')
        ->stream('download.pdf');
    
    // Or load HTML and customize paper/warnings
    Pdf::loadHTML($html)
        ->setPaper('a4', 'landscape')
        ->setWarnings(false)
        ->save('myfile.pdf');
  5. Publish and configure dompdf settings

    master

    To customize global settings like default paper size or font, publish the configuration file to config/dompdf.php using the Artisan command. You can also modify options at runtime using setOption().

    php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
    // Set options at runtime
    Pdf::setOption(['dpi' => 150, 'defaultFont' => 'sans-serif']);