On this page
- Step 1: Install Laravel
- Step 2: Install DomPDF via barryvdh/laravel-dompdf
- Step 3: Publish the Configuration (Optional)
- Step 4: Create a Blade View for the PDF
- Step 5: Create a Route and Controller to Generate PDF
- Step 6: Create a Route
- Step 7: Preview Instead of Download (Optional)
- Common Errors & Fixes
- Conclusion
PDF generation is a common feature in many web applications — whether it’s for invoices, reports, or tickets. In this tutorial, you’ll learn how to generate PDF files in Laravel using DomPDF (via the barryvdh/laravel-dompdf package).
Let’s get started!
Step 1: Install Laravel
composer create-project laravel/laravel laravel-dompdf
cd laravel-dompdfStep 2: Install DomPDF via barryvdh/laravel-dompdf
Run the following command to install the official wrapper for DomPDF:
composer require barryvdh/laravel-dompdfThis package provides a fluent API to work with DomPDF in Laravel.
Step 3: Publish the Configuration (Optional)
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"This will publish the
config/dompdf.phpfile where you can customize settings such as paper size, font, etc.
Step 4: Create a Blade View for the PDF
Create a Blade view file that will be converted into a PDF:
resources/views/pdf/invoice.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Invoice PDF</title>
<style>
body {
font-family: DejaVu Sans;
}
</style>
</head>
<body>
<h1>Invoice</h1>
<p>Customer: {{ $customer }}</p>
<p>Amount: ${{ $amount }}</p>
</body>
</html>✅ Tip: Use inline CSS or simple styles — DomPDF doesn’t support all CSS features.
Step 5: Create a Route and Controller to Generate PDF
A. Create Controller
php artisan make:controller PDFControllerB. Add PDF Logic in the Controller
app/Http/Controllers/PDFController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;
class PDFController extends Controller
{
public function generateInvoice()
{
$data = [
'customer' => 'John Doe',
'amount' => 120.50,
];
$pdf = Pdf::loadView('pdf.invoice', $data);
// Download the PDF
return $pdf->download('invoice.pdf');
}
}Step 6: Create a Route
Visit:http://localhost:8000/invoice-pdf
… and your PDF will be downloaded automatically!
Step 7: Preview Instead of Download (Optional)
If you want to stream the PDF in the browser instead of downloading:
return $pdf->stream('invoice.pdf');If you want to pass dynamic data via a form:
A. HTML Form (Blade)
<form method="POST" action="{{ route('invoice.generate') }}">
@csrf
<input type="text" name="customer" placeholder="Customer Name">
<input type="number" name="amount" placeholder="Amount">
<button type="submit">Generate PDF</button>
</form>B. Update Route
Route::post('/invoice-generate', [PDFController::class, 'generateFromForm'])->name('invoice.generate');C. Update Controller
public function generateFromForm(Request $request)
{
$pdf = Pdf::loadView('pdf.invoice', $request->all());
return $pdf->download('dynamic_invoice.pdf');
}Common Errors & Fixes
- Fonts Not Rendering: Use
DejaVu Sansfor Unicode support. - CSS Not Applying: Avoid external stylesheets; use inline styles.
- Image Not Showing: Use
public_path('images/your-image.jpg').
Conclusion
DomPDF is a powerful way to generate PDFs in Laravel with minimal setup. You can generate static or dynamic PDFs with ease using Blade views and pass any data needed from your controller.
Whether you’re building an invoicing system, a reporting module, or just want to export printable content, this setup has you covered.
Helpful Links
If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel!
Free resources for this topic
Code snippets and fixes for this topic
Get one practical tip every week
New tutorials, fixes and free cheat sheets from DEV Talk, straight to your inbox. No spam, unsubscribe in one click.
Free download: Laravel interview questions (PDF)
Have a question about this post?
Ask the community: other developers and the author can answer.
Get notified about new articles
One click, no email needed. Turn it off anytime with the bell at the top.
Get new articles on your iPhone
Add DEV Talk to your Home Screen, open it, then tap the bell.
Enjoying the content?
If this article helped you, you can support the author with a coffee. It keeps new posts coming.
Comments 0
No comments yet
Be the first to share your thoughts.