barryvdh/laravel-dompdf
Generate PDFs in Laravel using Dompdf. Render Blade views or HTML to PDF, set paper size/orientation, stream or download responses, and configure fonts/options. Popular, straightforward integration for invoices, reports, and documents.
Installation:
composer require barryvdh/laravel-dompdf
Publish the config (optional but recommended for customization):
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider" --tag=config
First Use Case: Generate a PDF from a Blade view:
use Barryvdh\DomPDF\Facade\Pdf;
$pdf = Pdf::loadView('pdf.invoice');
return $pdf->download('invoice.pdf');
Key Files:
config/dompdf.php: Central configuration (e.g., paper size, security settings).resources/views/pdf/*.blade.php: Blade templates for PDF content.Loading Content:
// From a Blade view
Pdf::loadView('view.name', $data)->save('file.pdf');
// From a string
Pdf::loadHTML('<h1>Hello</h1>')->stream();
// From a URL (if `enable_remote` is true)
Pdf::load('https://example.com')->download('remote.pdf');
Dynamic Options: Override defaults per request:
Pdf::loadView('view.name')
->setOption('isHtml5ParserEnabled', true)
->setOption('defaultFont', 'DejaVu Sans')
->stream();
Output Methods:
return Pdf::loadView('view.name')->download('filename.pdf');
return Pdf::loadView('view.name')->stream();
Pdf::loadView('view.name')->save(storage_path('app/file.pdf'));
Integration with Controllers:
public function generateReport(Request $request) {
$pdf = Pdf::loadView('reports.daily', [
'data' => $request->user()->reports
]);
return $pdf->setPaper('a4', 'landscape')->stream('report.pdf');
}
Queueing for Background Jobs: Use Laravel queues to offload PDF generation:
// Job class
public function handle() {
$pdf = Pdf::loadView('pdf.bulk-report', ['data' => $this->data]);
Storage::put('reports/' . $this->filename . '.pdf', $pdf->output());
}
Custom Fonts: Add fonts via config:
'font_dir' => public_path('fonts'),
'font_data' => [
'DejaVu Sans' => Pdf::font('DejaVuSans.ttf', 'DejaVu Sans'),
],
Security:
enable_remote is false by default (v3.x). Explicitly allow remote URLs:
'enable_remote' => true,
'allowedRemoteHosts' => ['*.example.com'],
data:// is in allowed_protocols if using inline assets:
'allowed_protocols' => ['http', 'https', 'ftp', 'ftps', 'data'],
Deprecated Methods:
setOptions() (use setOption() for granular control).Pdf (not PDF) for consistency.Font Issues:
font_dir and font_data config.public_path('fonts/DejaVuSans.ttf')).CSS Quirks:
!important or inline styles.isRemoteEnabled and isPhpEnabled for debugging:
Pdf::loadHTML($html)->setOption('isRemoteEnabled', true);
Performance:
if (!Storage::exists("cache/{$filename}.pdf")) {
$pdf->save(storage_path("cache/{$filename}.pdf"));
}
return response()->file(storage_path("cache/{$filename}.pdf"));
Inspect HTML:
Use ->debug() to dump the rendered HTML before PDF conversion:
Pdf::loadView('view.name')->debug();
Log Errors: Enable DomPDF’s error handling:
Pdf::loadHTML($html)->setOption('isErrorHandlerEnabled', true);
Check Config:
Validate dompdf.php for typos (e.g., allowedRemoteHosts vs. allowed_remote_hosts).
Custom Paper Sizes: Extend via config:
'paper' => [
'custom' => ['width' => 210, 'height' => 297], // A4 in mm
],
Event Listeners: Hook into DomPDF’s lifecycle (e.g., post-generation):
Pdf::loadView('view.name')->after(function ($pdf) {
Log::info('PDF generated with ID: ' . $pdf->getId());
});
Headless Chrome Integration:
For dynamic content, use Laravel’s Puppeteer or Playwright to generate HTML first, then pass to DomPDF.
Testing:
Use Mockery to stub PDF generation in unit tests:
$mock = Mockery::mock('overload:Barryvdh\DomPDF\Facade\Pdf');
$mock->shouldReceive('loadView')->andReturnSelf();
$mock->shouldReceive('stream')->andReturn('mocked-pdf');
How can I help you explore Laravel packages today?