Laravel DOMPDF
Use this skill when a Laravel task involves PDF generation through barryvdh/laravel-dompdf.
Documentation
Use search-docs when it is available for Laravel integration details. For package-specific behavior, inspect:
config/dompdf.phpsrc/PDF.phpsrc/ServiceProvider.phpreferences/example.mdreferences/package.md
Configuration
Laravel auto-discovers the service provider and facade aliases. Publish the config when the app needs local overrides:
php artisan vendor:publish --provider="Barryvdh\\DomPDF\\ServiceProvider"
Basic Usage
Typical controller flow:
use Barryvdh\DomPDF\Facade\Pdf;
return Pdf::loadView('pdf.invoice', [
'order' => $order,
])->download('invoice.pdf');
Useful output methods:
download('file.pdf')returns an attachment responsestream('file.pdf')renders inline in the browsersave('path/file.pdf', $disk)stores the generated PDFoutput()returns the raw PDF bytes for mail attachments or custom storage
Examples
Use resources/example.md for copyable Laravel examples covering:
- Downloading a PDF from a Blade view
- Streaming a PDF inline in the browser
- Generating from an HTML string
- Saving to a path or Laravel filesystem disk
- Setting paper size, orientation, DPI, and default font
- Enabling PDF/A mode
- Enabling remote images with trusted hosts
Working Pattern
- Build a dedicated Blade view for the PDF.
- Pass only the data the view needs.
- Generate through
Pdf::loadView()unless the source is already HTML or a file. - Set paper, orientation, and options explicitly when the document format matters.
- Test the response headers or persisted file, not just the route status.
Important Package Behavior
PdfandPDFfacade aliases are both registered by the package.loadView()renders a Blade view and then passes the HTML to DOMPDF.- The service provider binds
dompdf,dompdf.options, anddompdf.wrapperinto the container. setOption()andsetOptions()write directly to DOMPDF options.setWarnings(true)makes render warnings throw exceptions.- Since package 3.x, remote access is disabled by default. Enable it deliberately in
config/dompdf.phpwhen the PDF must load remote assets. - Local file access is constrained by DOMPDF
chroot; keep assets inside the allowed path.
PDF/A And Embedded Files
For archival PDFs:
- Enable PDF/A in
config/dompdf.phpor callsetPdfA() - Use embedded fonts such as
DejaVu SansorDejaVu Serif - Keep the backend on
CPDF
For Factur-X / Zugferd style workflows, the package exposes:
setPdfA()setAdditionalXmpRdf()addEmbeddedFile()
These methods require the CPDF backend.
Blade And Asset Guidance
- Add
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>to PDF views that render non-ASCII text. - Use print-friendly HTML and conservative CSS.
- Use page-break CSS when multi-page layout matters.
- Prefer absolute filesystem paths or confirmed reachable URLs for images and fonts.
- If fonts render incorrectly, check
font_dir,font_cache, and whether the font can legally be embedded.
Verification
For HTTP endpoints returning PDFs, verify:
Content-Typeisapplication/pdfContent-Dispositionmatchesdownload()orstream()- Generated bytes are non-empty
For saved PDFs, assert the file exists and compare its contents with output() when practical.
Common Pitfalls
- Forgetting to publish or override
config/dompdf.phpwhen custom options are needed - Expecting remote images to work while
isRemoteEnabledis still disabled - Using fonts that are not embedded for PDF/A validation
- Referencing files outside DOMPDF
chroot - Treating browser HTML and DOMPDF HTML/CSS support as identical