Laravel 12 Generate PDF Using DomPDF with Example

In this tutorial, I will show you how to generate an invoice PDF in Laravel 12 using DomPDF. Generating PDF files in Laravel is a common task, especially for creating invoices, reports, receipts, and more.

We will use the barryvdh/laravel-dompdf package, which is widely used for generating PDF files in Laravel. By the end of this tutorial, you will be able to:

  • Generate an invoice PDF with dynamic data.
  • Download or view the PDF file.
  • Use a Blade template to design the invoice.

So, let's get started.

Laravel 12 Generate PDF Using DomPDF with Example

 

Step 1: Install Laravel 12

If you have not installed Laravel 12 yet, you can install it using the following command:

laravel new laravel12-invoice

 

Step 2: Install DomPDF Package

Now, install the barryvdh/laravel-dompdf package using composer.

composer require barryvdh/laravel-dompdf

 

Step 3: Create Invoice Model and Migration

Now, let's create a model, migration, and controller for the invoice:

Run the following command:

php artisan make:model Invoice -mcr

 

Step 4: Update Migration File

Open the migration file from:

public function up()
{
    Schema::create('invoices', function (Blueprint $table) {
        $table->id();
        $table->string('customer_name');
        $table->string('customer_email');
        $table->text('customer_address');
        $table->decimal('amount', 10, 2);
        $table->date('invoice_date');
        $table->timestamps();
    });
}

Now migrate the database:

php artisan migrate

 

Step 5: Add Dummy Data (Optional)

To test the PDF generation, you can add some dummy data in your database using tinker or directly from the database.:

php artisan tinker

Add some data:

use App\Models\Invoice;

Invoice::create([
    'customer_name' => 'John Doe',
    'customer_email' => '[email protected]',
    'customer_address' => '123 Street, City, Country',
    'amount' => 250.00,
    'invoice_date' => now(),
]);

 

Step 6: Update Invoice Model

Open the model app/Models/Invoice.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{
    use HasFactory;

    protected $fillable = [
        'customer_name',
        'customer_email',
        'customer_address',
        'amount',
        'invoice_date',
    ];
}

 

Step 7: Create Route

Now open the web.php file from:

use App\Http\Controllers\InvoiceController;

Route::get('invoice/{id}', [InvoiceController::class, 'generateInvoice'])->name('invoice.pdf');

 

Step 8: Create InvoiceController

Now open the app/Http/Controllers/InvoiceController.php

<?php

namespace App\Http\Controllers;

use App\Models\Invoice;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;

class InvoiceController extends Controller
{
    public function generateInvoice($id)
    {
        $invoice = Invoice::findOrFail($id);

        $pdf = Pdf::loadView('invoices.invoice', compact('invoice'));

        return $pdf->download('invoice-' . $invoice->id . '.pdf');
    }
}

 

Step 9: Create Invoice Blade Template

Now create a folder called invoices inside:

resources/views/invoices/invoice.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Invoice #{{ $invoice->id }}</title>
</head>
<body>
    <h2>Invoice</h2>
    <p><strong>Invoice ID:</strong> {{ $invoice->id }}</p>
    <p><strong>Customer Name:</strong> {{ $invoice->customer_name }}</p>
    <p><strong>Email:</strong> {{ $invoice->customer_email }}</p>
    <p><strong>Address:</strong> {{ $invoice->customer_address }}</p>
    <p><strong>Amount:</strong> ${{ number_format($invoice->amount, 2) }}</p>
    <p><strong>Invoice Date:</strong> {{ $invoice->invoice_date }}</p>
    <br><br>
    <p>Thank you for your business!</p>
</body>
</html>

 

Step 10: Test the Invoice PDF

Now, start your Laravel server:

php artisan serve

 

Step 11: View PDF Without Download (Optional)

If you want to view the PDF in the browser instead of downloading it, update the InvoiceController like this:

app/Http/Controllers/InvoiceController.php

public function generateInvoice($id)
{
    $invoice = Invoice::findOrFail($id);
    $pdf = Pdf::loadView('invoices.invoice', compact('invoice'));

    return $pdf->stream('invoice-' . $invoice->id . '.pdf');
}

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS