Laravel 11 Invoice Generate using Dompdf

Laravel invoice can be easily generated using dompdf package. Making invoice is not that much tough. To make invoice in laravel you will have to follow some rules and guidelines. We will have to install composer package, make blade file , create route and so on. This article will give you clear idea how to create invoice pdf in laravel. For making this article complete we will use barryvdh/laravel-dompdf.
For your kind information this article is valid for laravel 8, laravel 9, laravel 10, laravel 11 as well as newer versions. So let's start.
  1. Install Laravel 11 Project
  2. Install barryvdh/laravel-dompdf composer package
  3. Create Route
  4. Create InvoiceController
  5. Add Blade File
Install Laravel 11 Project
composer create-project --prefer-dist laravel/laravel laravel11-pdf
cd laravel11-pdf
Install barryvdh/laravel-dompdf Composer Package
To install barryvdh/laravel-dompdf package we will have to run below command in terminal
composer require barryvdh/laravel-dompdf
Create Necessary Route
To make pdf invoice file we will have to define route in web.php  to forward request in method > controller
use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;

Route::get('/download-pdf-invoice', [InvoiceController::class, 'generatePdfInvoice']);

Create InvoiceController Class File
Now it's time to create a controller file with method generatePdfInvoice()
namespace App\Http\Controllers;

use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Response;


class InvoiceController extends Controller
{
    public function generatePdfInvoice()
    {
        $invoice_items = [
            [
                'id' => 1,
                'description' => 'Web Development Service',
                'quantity' => 1,
                'unit_price' => 500,
                'total' => 500
            ],
            [
                'id' => 2,
                'description' => 'SEO Optimization',
                'quantity' => 2,
                'unit_price' => 200,
                'total' => 400
            ]
        ];


        $pdf = Pdf::loadView('invoice', ['invoice_items' => $invoice_items]);
        return $pdf->download();

    }
}
Create Blade File
Create a blade file at location resources/views/invoice.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Invoice</title>
    <link href="{{ public_path('css/invoice.css') }}" rel="stylesheet">
</head>
<body>

<div class="invoice-container">

    <!-- Invoice Header -->
    <div class="invoice-header">
        <div class="company-details">
            <h2>Programmingmindset.com</h2>
            <p>Dhaka, Bangladesh</p>
            <p>Email: [email protected]</p>
        </div>
        <div class="invoice-title">
            <h4>Invoice</h4>
            <p><strong>Invoice #:</strong> 00123</p>
            <p><strong>Date:</strong> February 15, 2025</p>
        </div>
    </div>

    <!-- Client Details -->
    <div class="client-details">
        <h4>Bill To:</h4>
        <p>John Doe</p>
        <p>456 Client St, City, Country</p>
        <p>Email: [email protected]</p>
    </div>

    <!-- Invoice Table -->
    <table class="table-container">
        <thead>
        <tr>
            <th>#</th>
            <th>Description</th>
            <th>Quantity</th>
            <th>Unit Price</th>
            <th>Total</th>
        </tr>
        </thead>
        <tbody>
        @foreach($invoice_items as $key=> $invoice_item)

            <tr>
                <td>{{ $invoice_item['id'] }}</td>
                <td>{{ $invoice_item['description'] }}</td>
                <td>{{ $invoice_item['quantity'] }}</td>
                <td>{{ $invoice_item['unit_price'] }}</td>
                <td>{{ $invoice_item['total'] }}</td>
            </tr>

        @endforeach
        </tbody>
    </table>
    <!-- Total Section -->
    <div class="total-section">
        <span>Payment Method: Bank Transfer</span>
        <span>Total: $700</span>
    </div>
    <!-- Footer -->
    <p class="footer">Thank you for your business!</p>
</div>
</body>
</html>
CSS File created at location public/css/invoice.css
body {
    font-family: Arial, sans-serif;
    background-color: #f8f9fa;
    padding: 20px;
}

.invoice-container {
    max-width: 800px;
    margin: auto;
    background: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid #ddd;
    padding-bottom: 15px;
    margin-bottom: 20px;
}

.company-details h2 {
    margin: 0;
    color: #333;
}

.invoice-title h4 {
    margin: 0;
    color: #555;
}

.client-details,
.payment-details {
    margin-bottom: 20px;
}

.table-container {
    width: 100%;
    border-collapse: collapse;
}

.table-container th,
.table-container td {
    border: 1px solid #ddd;
    padding: 10px;
    text-align: left;
}

.table-container th {
    background: #f2f2f2;
}

.total-section {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
    font-size: 18px;
    font-weight: bold;
}

.footer {
    text-align: center;
    margin-top: 20px;
    font-size: 14px;
    color: #666;
}

After successfully done everything start your server by php artisan serve and server will be started at http://127.0.0.1:8000
Final output file will be like below after hitting route http://127.0.0.1:8000/download-pdf-invoice

laravel dompdf invoice download - programmingmindset.com



If you find this article helpful, then bookmark it, share it with team as well as invite others to read

Tags: