barryvdh/laravel-dompdf
Laravel wrapper for DOMPDF that makes generating PDFs from Blade views straightforward. Supports streaming or downloading, paper size/orientation options, and basic HTML/CSS rendering—ideal for invoices, reports, and other server-side PDF output in Laravel apps.
Pros:
new Dompdf) and facade-based usage (Pdf::loadView()), accommodating varying architectural preferences (e.g., dependency injection vs. service locator).dompdf.generating) for pre/post-processing (e.g., adding headers, watermarks).Pdf::loadView('invoice')), reducing boilerplate for dynamic content.Cons:
Laravel Ecosystem Synergy:
bind('dompdf', fn() => new Dompdf())).php artisan vendor:publish --tag=dompdf-config exposes customization points (e.g., allowedRemoteHosts, default_paper_size).PdfJob::dispatch($view, $filename)), critical for performance.Pdf::shouldReceive('loadView')->once()) and built-in test utilities (e.g., Pdf::stream() for assertions).External Dependencies:
setFont() or addFont()) for consistent rendering across environments.Breaking Changes:
enable_remote defaults to false (security hardening).allowedRemoteHosts and artifactPathValidation added for remote resource control.data:// URI support now requires explicit config (v3.1.0+).Barryvdh\DomPDF\Facade → Barryvdh\DomPDF\Facade\Pdf (v2.0.0+).setOptions() → setOption() (v2.0.0+).Performance Risks:
memory_limit. Monitor with memory_get_usage() and adjust dompdf.options['memory_limit'].Dompdf instances) require synchronization.Security Risks:
enable_remote enables fetching external resources (e.g., images). Restrict with allowedRemoteHosts and validate inputs.addFont('/malicious/path')).data:// by default; update config if needed.Stack Compatibility:
Use Case Complexity:
Pdf::loadView().page_script, but rendering may vary.Customization Needs:
default_font, isRemoteEnabled)? Publish the config and extend it.setOption() or extend the Dompdf class.dompdf.generating or use Pdf::stream() with middleware.Deployment Constraints:
imagick, fileinfo) are included in your image.Fallback Strategy:
Laravel-Centric Design:
Pdf::loadView('view.name', $data) for Blade templates or Pdf::loadHTML($html) for dynamic content.Dompdf to the container for DI:
$this->app->bind('dompdf', fn() => new \Barryvdh\DomPDF\Facade\Pdf());
dompdf.generating for pre-processing:
event(new Generating($pdf, $view));
PdfMiddleware to add headers/watermarks:
Pdf::setOption('default_font', 'DejaVu Sans');
Non-Laravel PHP:
dompdf/dompdf library directly if not using Laravel’s ecosystem. The wrapper adds minimal value outside Laravel.Frontend Integration:
return Pdf::loadView('invoice', $data)->stream('invoice.pdf');
return Pdf::loadView('invoice', $data)->download('invoice.pdf');
$base64 = Pdf::loadView('invoice', $data)->output();
| Current State | Migration Steps | Tools/Commands |
|---|---|---|
| No PDF Generation | Install via Composer: composer require barryvdh/laravel-dompdf. Publish config: php artisan vendor:publish --tag=dompdf-config. |
composer require, php artisan vendor:publish |
| Legacy Laravel (<9) / PHP (<8.1) | Fork the package or use a maintenance branch (e.g., v2.x). Alternatively, upgrade Laravel/PHP to v3.x. | Fork, composer require barryvdh/laravel-dompdf:^2.2 |
| Custom dompdf Integration | Replace direct dompdf usage with the facade/wrapper. Update config to match new defaults (e.g., enable_remote). |
Search/replace, php artisan config:clear |
| Queue-Based Generation | Create a job extending ShouldQueue and use `Pdf::loadView()->save($ |
How can I help you explore Laravel packages today?