Laravel DOMPDF is a package that allows you to generate PDF documents in your Laravel applications using the DOMPDF library. This is useful for creating downloadable invoices, reports, or any other content that needs to be printed or saved as a PDF.
Key Features
- Easy Integration: Seamlessly integrates with Laravel for quick PDF generation.
- HTML to PDF: Converts HTML content into PDF format, allowing you to use Blade templates.
- Customizable: Supports custom fonts, images, and styling in PDFs.
- Stream or Download: Offers options to either stream or download the generated PDFs.
Installation
You can install the package via Composer:
1 |
composer require barryvdh/laravel-dompdf |
Configuration
After installing the package, you may publish the configuration file:
1 |
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider" |
This will create a configuration file at config/dompdf.php
, where you can adjust various settings if needed.
Basic Usage
Generating a PDF
To generate a PDF, you can use the PDF
facade provided by the package. Here’s a simple example:
- Create a Blade Template
Create a Blade view file (e.g.,
resources/views/pdf.blade.php
) with the content you want in your PDF:html
1234567891011121314151617<!DOCTYPE html><html><head><title>Invoice</title><style>/* Add your custom styles here */body {font-family: Arial, sans-serif;}</style></head><body><h1>Invoice</h1><p>Thank you for your purchase!</p><p>Amount: $100</p></body></html> - Generate and Stream/Download the PDF
In your controller, you can generate the PDF from the Blade view:
php
12345678use PDF; // Import the PDF facadepublic function generatePDF(){$pdf = PDF::loadView('pdf'); // Load the viewreturn $pdf->stream('invoice.pdf'); // Stream the PDF// Or use return $pdf->download('invoice.pdf'); to download instead}
Passing Data to the PDF
You can also pass data to your Blade view to generate dynamic content in the PDF:
1 2 3 4 5 6 7 8 9 10 |
public function generatePDF() { $data = [ 'amount' => 100, 'customer' => 'John Doe', ]; $pdf = PDF::loadView('pdf', $data); // Pass data to the view return $pdf->stream('invoice.pdf'); } |
Customizing PDF Settings
You can customize various settings in the dompdf.php
configuration file, such as:
- Paper Size: Set the default paper size (A4, A5, etc.).
- Orientation: Set the default orientation (portrait or landscape).
- Font Options: Specify custom fonts or load fonts from external sources.
Custom Fonts
To use custom fonts, you need to include them in your project and register them in the dompdf.php
configuration file. For example:
- Add Fonts: Place your custom font files in the
storage/fonts
directory. - Register Fonts: Update the
font_dir
andfont_cache
settings inconfig/dompdf.php
to include your custom fonts.
Conclusion
Laravel DOMPDF provides an effective way to generate PDF documents from your Laravel applications using Blade templates. With its ease of use and flexibility, you can create customized PDFs tailored to your application’s needs.