- How do I install and set up barryvdh/laravel-dompdf in a Laravel project?
- Run `composer require barryvdh/laravel-dompdf` to install. The package auto-discovers Laravel and publishes a config file (`config/dompdf.php`) for customization. No additional setup is needed for basic usage, but you can publish the config with `php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"` for manual adjustments.
- What Laravel versions does barryvdh/laravel-dompdf support?
- The package supports Laravel 11, 12, and 13 (as of v3.1.2). Older versions (e.g., Laravel 10) may work but are not officially tested. Always check the [GitHub releases](https://github.com/barryvdh/laravel-dompdf/releases) for compatibility notes before upgrading.
- Can I generate PDFs from Blade views or raw HTML strings?
- Yes. Use `Pdf::loadView('view.name')` for Blade templates or `Pdf::loadHTML($htmlString)` for raw HTML. Both methods integrate seamlessly with Laravel’s routing and response system. For example, return `Pdf::loadView('invoice')->stream('invoice.pdf')` in a controller to stream the PDF.
- How do I configure default PDF settings like paper size or margins?
- Edit the `config/dompdf.php` file to set defaults like `default_paper_size`, `default_orientation`, or `default_font`. You can also override settings per request using methods like `Pdf::setPaper('A4', 'landscape')` or `Pdf::setOption('isHtml5ParserEnabled', true)`.
- What are the differences between `stream()`, `download()`, and `save()` methods?
- `stream()` sends the PDF inline (e.g., for preview), `download()` forces a download with a filename, and `save()` writes the PDF to disk (e.g., `Pdf::save(storage_path('reports/report.pdf'))`). Use `stream()` for real-time previews and `download()` for user-triggered exports.
- Does barryvdh/laravel-dompdf support async PDF generation with Laravel queues?
- Yes. Chain the `queue()` method to defer PDF generation: `Pdf::loadView('report')->queue(function ($job, $pdf) { $job->load($pdf->download('report.pdf')); })`. This is useful for long-running or resource-intensive PDFs to avoid timeouts or blocking the user.
- How do I handle custom fonts or images in PDFs?
- Custom fonts require manual installation in Dompdf’s font directory (e.g., `vendor/dompdf/dompdf/lib/fonts/`). Use `Pdf::setOption('font_dir', storage_path('fonts'))` to specify a custom path. For images, ensure they’re accessible via a URL or local path, and Dompdf will embed them automatically.
- What are the security implications of using dompdf v3.x with Laravel?
- Dompdf v3.x disables remote content by default (`enable_remote = false`). If your app uses external resources (e.g., images from a CDN), whitelist allowed hosts in `config/dompdf.php` under `allowedRemoteHosts`. Protocol validation (e.g., blocking `data://` URIs) may also require config updates if you rely on inline data.
- How can I test PDF generation in Laravel’s testing environment?
- Use Laravel’s `Pdf::fake()` helper to mock PDF generation in tests. For example: `Pdf::fake(); $response = $this->get('/invoice'); Pdf::assertDownloaded('invoice.pdf')`. This prevents actual PDF generation during tests and verifies the correct file is triggered.
- What alternatives exist for HTML-to-PDF in Laravel, and when should I consider them?
- Alternatives include `spatie/laravel-pdf` (wrapper for SnappyPDF/WKHTMLtoPDF), `mikehaertl/phpwkhtmltopdf` (direct WKHTMLtoPDF), or `barryvdh/laravel-snappy` (SnappyPDF). Choose WKHTMLtoPDF-based solutions for complex CSS/JS rendering or if you need Chrome-headless support. Use Dompdf for simpler, server-dependent PDFs with minimal dependencies.