barryvdh/laravel-dompdf
Laravel wrapper for Dompdf to generate PDFs from HTML views. Provides a PDF facade/service, easy rendering, streaming or downloading responses, and simple configuration—ideal for invoices, reports, and other printable documents in Laravel apps.
Installation:
composer require barryvdh/laravel-dompdf
Laravel will auto-discover the package. No manual service provider registration is required.
Publish Config (Optional):
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider" --tag=config
This publishes config/dompdf.php for customization.
First Use Case: Generate a PDF from a Blade view:
use Barryvdh\DomPDF\Facade\Pdf;
$pdf = Pdf::loadView('pdf.invoice', ['data' => $invoiceData]);
return $pdf->download('invoice.pdf');
Barryvdh\DomPDF\Facade\Pdf (preferred) or Barryvdh\DomPDF\Facade\PDF (legacy).app('dompdf.wrapper').Loading Content:
$pdf = Pdf::loadView('view.name', $data);
$pdf = Pdf::loadHtml('<h1>Hello</h1>');
$pdf = Pdf::load('https://example.com');
Output Methods:
return Pdf::loadView('view')->download('filename.pdf');
return Pdf::loadView('view')->stream('filename.pdf');
$pdf->save(storage_path('app/filename.pdf'));
$pdfString = Pdf::loadView('view')->output();
Dynamic Options:
$pdf = Pdf::loadView('view')
->setOption('defaultFont', 'Courier')
->setOption('isRemoteEnabled', true)
->setOption('isPhpEnabled', true);
Chaining with DomPDF Methods: Leverage "magic methods" to call DomPDF directly:
$pdf = Pdf::loadView('view')
->setPaper('A4', 'landscape')
->bookmark('Section 1');
Queue PDF Generation: Use Laravel Queues to offload heavy PDF generation:
Pdf::loadView('view')->save(storage_path('app/queued.pdf'));
// Later, send the file via email or download.
Custom Fonts:
Add fonts via the addFont() method or configure in dompdf.php:
Pdf::loadView('view')->addFont('path/to/font.ttf', 'FontName');
Headers/Footers:
Use DomPDF’s setFooter() and setHeader() methods or inject via HTML/CSS:
$pdf = Pdf::loadView('view')
->setFooter('Page {PAGE_NUM} of {PAGE_COUNT}');
API Responses: Return PDFs in API responses with proper headers:
return response($pdf->output(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="document.pdf"',
]);
Testing:
Use Pdf::loadHtml() with static HTML for unit tests:
$pdf = Pdf::loadHtml('<h1>Test</h1>');
$this->assertStringContainsString('Test', $pdf->output());
Deprecated Methods:
setOptions() (use setOption() for granular changes).orientation config is obsolete; use default_paper_orientation in dompdf.php.Security:
isRemoteEnabled is false by default (v3.x). Enable only if needed:
Pdf::load('https://trusted-site.com')->setOption('isRemoteEnabled', true);
data:// is included if using data URIs (v3.1+):
'allowed_protocols' => ['http', 'https', 'ftp', 'data'],
Font Issues:
dompdf.php:
'font_dir' => storage_path('fonts'),
'font_cache' => storage_path('fonts/cache'),
Memory Limits:
dompdf.php:
'memory_limit' => '1024M',
Legacy Laravel:
Pdf::loadHtml($html)->debug() to preview rendering issues.dompdf.php:
'debug_pdf' => true,
config/dompdf.php for overrides:
php artisan vendor:publish --tag=dompdf-config
Custom Wrapper:
Extend Barryvdh\DomPDF\PDF for reusable logic:
class CustomPDF extends Pdf {
public function addWatermark($text) {
$this->getDomPDF()->setWatermark($text);
}
}
Event Listeners:
Hook into DomPDF events (e.g., dompdf.create):
Pdf::loadView('view')->onCreate(function ($dompdf) {
$dompdf->setPaper('A5');
});
Middleware: Restrict PDF generation to authenticated users:
Pdf::middleware('auth')->loadView('view');
Service Provider: Bind custom configurations:
$this->app->extend('dompdf.wrapper', function ($wrapper) {
$wrapper->setOption('defaultFont', 'Helvetica');
return $wrapper;
});
storage_path('fonts/cache'). Clear manually if fonts change:
php artisan dompdf:clear-font-cache
stream() for large PDFs to avoid memory spikes.table-layout: fixed for consistent table rendering.How can I help you explore Laravel packages today?