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:
So, let's get started.
If you have not installed Laravel 12 yet, you can install it using the following command:
laravel new laravel12-invoice
Now, install the barryvdh/laravel-dompdf package using composer.
composer require barryvdh/laravel-dompdf
Now, let's create a model, migration, and controller for the invoice:
Run the following command:
php artisan make:model Invoice -mcr
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
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(),
]);
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',
];
}
Now open the web.php file from:
use App\Http\Controllers\InvoiceController;
Route::get('invoice/{id}', [InvoiceController::class, 'generateInvoice'])->name('invoice.pdf');
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');
}
}
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>
Now, start your Laravel server:
php artisan serve
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: