The 'barryvdh/laravel-dompdf' package is a robust and widely used Laravel wrapper for the 'dompdf/dompdf' library. Its primary purpose is to simplify the generation of PDF documents from HTML (including Blade views) within a Laravel application. Dompdf itself is an HTML-to-PDF converter that renders HTML and CSS into PDF format.
Key Features and Functionality:
1. Easy Integration: It provides a simple, fluent API that integrates seamlessly with Laravel's ecosystem, making PDF generation straightforward.
2. Blade View Support: Developers can use their existing Blade templates to design PDF content, leveraging Laravel's powerful templating engine.
3. Customization: It allows for extensive customization of PDF output, including paper size (e.g., A4, Letter), orientation (portrait, landscape), font handling, and complex CSS styling.
4. Flexible Output Options: You can choose to stream the PDF directly to the browser, force a download with a specific filename, save it to disk (e.g., using Laravel's Filesystem), or simply get the raw PDF content as a string.
5. Headers and Footers: While Dompdf doesn't have native header/footer support in its core rendering, it can be achieved by carefully positioning HTML elements with CSS (e.g., `position: fixed`) or using Dompdf's event listeners for more advanced scenarios (though the package focuses on HTML conversion).
6. Image Support: It supports embedding images within your PDFs, provided the paths are correct and accessible.
How it Works:
Under the hood, when you feed HTML (typically from a Blade view) to the package, it passes that HTML to the underlying Dompdf library. Dompdf then parses the HTML and CSS, converts it into its internal representation, and finally renders it as a PDF document. The 'barryvdh/laravel-dompdf' package handles the loading, configuration, and output handling, abstracting away the complexities of interacting directly with Dompdf.
Installation:
To use this package, you typically install it via Composer:
`composer require barryvdh/laravel-dompdf`
For Laravel versions 5.5 and above, package auto-discovery handles registration. For older versions, you might need to manually add the service provider and alias in `config/app.php`.
`php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"` can be run to publish the configuration file (`config/dompdf.php`), allowing you to customize settings like paper size, default fonts, and other Dompdf options globally.
This package is an essential tool for Laravel developers who need to generate reports, invoices, certificates, or any document requiring a printable PDF format from their web application.
Example Code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf; // Import the facade
class PdfController extends Controller
{
/
* Generate and download a PDF invoice.
*
* @param int $invoiceId
* @return \Illuminate\Http\Response
*/
public function generateInvoice($invoiceId)
{
// In a real application, you would fetch invoice data from a database
$invoiceData = [
'invoice_id' => $invoiceId,
'customer_name' => 'John Doe',
'items' => [
['description' => 'Product A', 'quantity' => 2, 'price' => 10.00],
['description' => 'Service B', 'quantity' => 1, 'price' => 25.50],
],
'total' => 45.50,
'date' => date('Y-m-d'),
];
// Load a Blade view with data and convert it to PDF
$pdf = Pdf::loadView('pdfs.invoice', $invoiceData);
// You can choose to download the PDF, stream it, or save it
// Option 1: Download the PDF directly
return $pdf->download('invoice-' . $invoiceId . '.pdf');
// Option 2: Stream the PDF to the browser (opens in new tab/window usually)
// return $pdf->stream('invoice-' . $invoiceId . '.pdf');
// Option 3: Save the PDF to a specific path (e.g., using Laravel's storage)
// $filePath = storage_path('app/public/invoices/invoice-' . $invoiceId . '.pdf');
// $pdf->save($filePath);
// return response()->json(['message' => 'PDF saved successfully', 'path' => $filePath]);
}
/
* Example of generating a simple PDF from raw HTML.
*
* @return \Illuminate\Http\Response
*/
public function generateSimpleHtmlPdf()
{
$html = "
<h1>Hello World!</h1>
<p style=\"color: blue;\">This is a simple PDF generated from raw HTML.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
";
$pdf = Pdf::loadHTML($html);
// Set paper size and orientation if needed
$pdf->setPaper('A4', 'landscape');
return $pdf->stream('simple-document.pdf');
}
}
// --- resources/views/pdfs/invoice.blade.php (Example Blade View) ---
// <!DOCTYPE html>
// <html>
// <head>
// <meta charset="utf-8">
// <title>Invoice #{{ $invoice_id }}</title>
// <style>
// body { font-family: sans-serif; margin: 20px; }
// .header { text-align: center; margin-bottom: 30px; }
// .customer-info { margin-bottom: 20px; }
// table { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
// table, th, td { border: 1px solid #ddd; padding: 8px; }
// th { background-color: #f2f2f2; text-align: left; }
// .total { text-align: right; font-weight: bold; font-size: 1.2em; }
// </style>
// </head>
// <body>
// <div class="header">
// <h1>Invoice</h1>
// <p>Date: {{ $date }}</p>
// </div>
// <div class="customer-info">
// <p><strong>Invoice ID:</strong> {{ $invoice_id }}</p>
// <p><strong>Customer:</strong> {{ $customer_name }}</p>
// </div>
// <table>
// <thead>
// <tr>
// <th>Description</th>
// <th>Quantity</th>
// <th>Unit Price</th>
// <th>Amount</th>
// </tr>
// </thead>
// <tbody>
// @foreach ($items as $item)
// <tr>
// <td>{{ $item['description'] }}</td>
// <td>{{ $item['quantity'] }}</td>
// <td>${{ number_format($item['price'], 2) }}</td>
// <td>${{ number_format($item['quantity'] * $item['price'], 2) }}</td>
// </tr>
// @endforeach
// </tbody>
// </table>
// <p class="total">Total: ${{ number_format($total, 2) }}</p>
// </body>
// </html>
// --- routes/web.php (Example Route) ---
// use App\Http\Controllers\PdfController;
// Route::get('/invoice/{id}/pdf', [PdfController::class, 'generateInvoice']);
// Route::get('/simple-pdf', [PdfController::class, 'generateSimpleHtmlPdf']);








barryvdh/laravel-dompdf