- How do I install laravel-dompdf in a Laravel 12 project?
- Run `composer require barryvdh/laravel-dompdf` in your project directory. The package auto-discovers Laravel and registers the service provider. No manual configuration is needed for basic usage, though you may want to publish the config file with `php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"` for custom settings.
- What’s the simplest way to generate a PDF from a Blade view?
- Use the facade: `Pdf::loadView('invoice')->stream()` to render the Blade view and stream the PDF directly to the browser. For downloading, append `->download('filename.pdf')`. This abstracts DomPDF’s complexity while keeping the API intuitive for Laravel developers.
- Does laravel-dompdf support custom fonts, and how do I add them?
- Yes, place your TTF or OTF font files in `storage/fonts/` and register them in the `config/dompdf.php` file under the `fonts` array. For dynamic fonts, use `Pdf::loadView()->setOption('defaultFont', 'YourFont')`. Note that custom fonts must be pre-registered before rendering.
- What Laravel and PHP versions does the latest version (3.x) support?
- The latest stable version (3.1.2) supports Laravel 9 through 13 and PHP 8.1 through 8.5. If you’re using Laravel 8 or PHP 8.0, you’ll need to use version 2.x of the package, though it lacks security updates from DomPDF 3.x. Always check the [release notes](https://github.com/barryvdh/laravel-dompdf/releases) for specifics.
- How do I handle remote content (e.g., images from external URLs) in PDF generation?
- Remote content is disabled by default in DomPDF 3.x for security. To allow specific domains, configure `allowedRemoteHosts` in `config/dompdf.php`. For example, `['https://example.com']` will permit images from that domain. Avoid enabling this for untrusted sources.
- Can I queue PDF generation for large or complex documents?
- Yes, use Laravel’s queue system to offload PDF generation. Wrap your PDF logic in a job (e.g., `PdfJob`) and dispatch it with `dispatch(new PdfJob($view, $filename))`. This prevents timeouts and improves performance for resource-intensive PDFs. Ensure your queue worker processes the jobs promptly.
- What are the breaking changes when upgrading from v2.x to v3.x?
- Key changes include dropping support for Laravel <9 and PHP <8.1, stricter security defaults (e.g., remote content blocking), and a facade rename from `PDF` to `Pdf`. DomPDF 3.x also enforces data URI validation and changes some method names (e.g., `setOptions()` → `setOption()`). Review the [upgrade guide](https://github.com/barryvdh/laravel-dompdf#upgrading) for full details.
- How do I test PDF output for visual consistency across environments?
- Use snapshot testing tools like Laravel’s `assertPdf` or third-party libraries such as `spatie/pdf-snapshot` to compare PDF outputs. For critical documents, manually verify fonts, spacing, and layout in staging environments. Automate tests in CI pipelines to catch regressions early.
- What happens if PDF generation fails (e.g., missing fonts, malformed HTML)?
- By default, DomPDF throws exceptions for critical errors like missing fonts or invalid HTML. Catch these exceptions in your code (e.g., `try-catch`) and handle them gracefully, such as logging errors or generating a fallback PDF. For user-facing errors, redirect to a support page or notify admins.
- Are there alternatives to laravel-dompdf for generating PDFs in Laravel?
- Yes, alternatives include `snappy` (wkhtmltopdf wrapper), `spatie/pdf` (Snappy + DomPDF), or `barryvdh/laravel-snappy` for HTML-to-PDF conversion. Choose based on your needs: DomPDF is lightweight but less accurate with CSS, while Snappy offers better rendering but requires system dependencies (e.g., wkhtmltopdf). Evaluate performance and feature requirements before switching.