- How do I generate a PDF from a Blade view in Laravel using laravel-dompdf?
- Use the `Pdf` facade to load a Blade view directly: `Pdf::loadView('invoice', $data)->stream()`. This renders the view to PDF and streams it to the browser. For downloading, replace `stream()` with `download()`. Ensure your view is optimized for PDF rendering, as complex CSS may affect output.
- Can I use laravel-dompdf with Laravel 8 or older versions?
- No, the latest version (v3.x) requires Laravel 9+ and PHP 8.1+. If you’re on an older stack, you’ll need to use v2.x or a maintained fork. Check the [changelog](https://github.com/barryvdh/laravel-dompdf/blob/master/CHANGELOG.md) for version-specific requirements. Upgrading Laravel is recommended for long-term support.
- How do I configure custom paper sizes or orientations for PDFs?
- Publish the config file with `php artisan vendor:publish --tag=dompdf-config`, then modify `config/dompdf.php`. Set `default_paper_size` (e.g., 'a4') and `default_orientation` (e.g., 'portrait'). You can also override these per generation: `Pdf::loadView()->setPaper('letter', 'landscape')->download()`.
- Is laravel-dompdf suitable for generating large reports (e.g., 100+ pages)?
- Large PDFs may hit PHP’s memory limits. Increase `dompdf.options['memory_limit']` in the config or adjust your server’s `memory_limit`. For extreme cases, consider generating PDFs asynchronously via Laravel Queues or breaking reports into smaller chunks. Test with `memory_get_usage()` to monitor consumption.
- How do I add custom fonts to ensure consistent rendering across environments?
- Use the `addFont()` method in your PDF generation code: `Pdf::loadView()->addFont('/path/to/font.ttf', 'FontName')`. For system-wide fonts, ensure they’re installed on your server. Bundle fonts with your deployment or use absolute paths to avoid environment-specific issues. Verify fonts with `Pdf::loadView()->getDompdf()->getFontList()`.
- Can I generate PDFs asynchronously using Laravel Queues?
- Yes, dispatch a job with `PdfJob::dispatch($view, $filename)`. The package supports async generation out of the box. Ensure your queue worker has sufficient memory and timeouts for large PDFs. For testing, mock the `Pdf` facade in your job’s `handle()` method to avoid actual PDF generation.
- What are the security risks of enabling remote content (e.g., images) in laravel-dompdf?
- Enabling `enable_remote` in the config allows fetching external resources (e.g., images), which can expose your app to XSS or SSRF risks. Restrict remote hosts with `allowedRemoteHosts` in the config and validate all user inputs. Avoid enabling this unless absolutely necessary, and use a CDN or local copies for critical assets.
- How do I test PDF generation in PHPUnit without generating actual files?
- Mock the `Pdf` facade to avoid file I/O: `Pdf::shouldReceive('loadView')->once()->andReturnSelf(); Pdf::shouldReceive('stream')->once()`. For assertions, capture output with `ob_start()`/`ob_get_clean()` or use `Pdf::stream()` in a test browser (e.g., Laravel Dusk). Avoid testing with real files to keep tests fast and deterministic.
- Why is my PDF rendering broken (e.g., missing styles, misaligned tables)?
- DOMPDF has limited CSS support. Avoid complex layouts, floats, or dynamic positioning. Use inline styles or simple classes for critical elements. Test with `Pdf::loadView()->getDompdf()->getHtml()` to debug the raw HTML. For advanced styling, consider SnappyPDF or direct HTML-to-PDF libraries like wkhtmltopdf.
- What alternatives should I consider if laravel-dompdf is too heavy for my needs?
- For lightweight PDFs, try SnappyPDF (uses wkhtmltopdf) or Spatie’s PDF library (simpler API). If you need minimal CSS support, consider direct DOM-to-PDF libraries like Dompdf (without the Laravel wrapper) or HTML-to-PDF services like PDFmyURL. Evaluate trade-offs: SnappyPDF is faster but less customizable, while laravel-dompdf offers deeper Blade integration.