Skip to content

How to Generate PDF Files Using DomPDF in Laravel

Learn how to generate PDF files in Laravel using DomPDF. This step-by-step guide covers installation, setup, and customization to create professional, downloadable PDFs in your Laravel applications.

Dasun Kalupahana Updated 2 min read 1K views 0 comments
On this page
  1. Step 1: Install Laravel
  2. Step 2: Install DomPDF via barryvdh/laravel-dompdf
  3. Step 3: Publish the Configuration (Optional)
  4. Step 4: Create a Blade View for the PDF
  5. Step 5: Create a Route and Controller to Generate PDF
  6. Step 6: Create a Route
  7. Step 7: Preview Instead of Download (Optional)
  8. Common Errors & Fixes
  9. 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-dompdf

Step 2: Install DomPDF via barryvdh/laravel-dompdf

Run the following command to install the official wrapper for DomPDF:

composer require barryvdh/laravel-dompdf

This 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.php file 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 PDFController

B. 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 Sans for 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.

 

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.

Ask the community

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.

Rate this post
5.0
Share

Enjoying the content?

If this article helped you, you can support the author with a coffee. It keeps new posts coming.

Buy me a coffee (opens in a new tab)

Comments 0

Be kind. Comments with links are reviewed before they appear.

No comments yet

Be the first to share your thoughts.

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.