Generating a PDF from a Blade view — an invoice, a report, a certificate — is a matter of rendering that view's HTML and passing it to a PDF rendering library, most commonly DomPDF for a pure-PHP solution with no external binary dependency.
Installing barryvdh/laravel-dompdf
composer require barryvdh/laravel-dompdf
Generating a PDF from a Blade view
use Barryvdh\DomPDF\Facade\Pdf;
public function downloadInvoice(Invoice $invoice)
{
$pdf = Pdf::loadView('invoices.pdf', compact('invoice'));
return $pdf->download("invoice-{$invoice->id}.pdf");
}
loadView() renders the Blade template just like a normal response would, then passes the resulting HTML into DomPDF for rendering — the same view file can be reused both as a normal web page and, unchanged, as the source for the PDF.
Streaming the PDF inline instead of forcing a download
return $pdf->stream("invoice-{$invoice->id}.pdf");
stream() opens the PDF directly in the browser (if the browser has a PDF viewer, which most modern ones do) rather than triggering a download prompt — the better choice for letting a user preview a document before deciding whether to save it.
Saving the PDF to storage instead of sending it to the browser
$pdf = Pdf::loadView('invoices.pdf', compact('invoice'));
Storage::put("invoices/invoice-{$invoice->id}.pdf", $pdf->output());
Setting paper size and orientation
$pdf = Pdf::loadView('reports.pdf', compact('report'))
->setPaper('a4', 'landscape');
A PDF-specific Blade view, kept simple for DomPDF's rendering engine
{{-- resources/views/invoices/pdf.blade.php --}}
Invoice #{{ $invoice->id }}
@foreach ($invoice->items as $item)
{{ $item->name }}
{{ $item->price }}
@endforeach
DomPDF doesn't support the full modern CSS feature set a real browser does — flexbox and grid layouts in particular are unreliable — sticking to simpler CSS (tables, basic block layout) for a PDF-specific template avoids fighting the rendering engine's real limitations.
When DomPDF isn't enough: browser-based rendering as an alternative
For a PDF that needs to visually match a genuinely modern, JavaScript-driven web page exactly, a headless-browser-based approach (like Puppeteer, run as a separate service Laravel calls out to) renders using an actual browser engine — considerably heavier to set up than DomPDF, but capable of accurately rendering CSS and JavaScript that DomPDF's simpler engine can't.