Using Dompdf in Laravel 9 app

In this tutorial we’ll use laravel-dompdf package in order to generate PDF file out of Laravel 9 app. For this article we’ll utilize the models and data created in a previous article. So, first follow the steps given there for creating models, migrations, and relationship and then continue from here.

By following the above-mentioned article, we now have a couple of tables (customers and invoices) having one-to-many relationship along with test data; so, let’s move onto generating an invoice in PDF format. First import laravel-dompdf package into the project by executing following command in project’s parent folder;

composer require barryvdh/laravel-dompdf

Next, create new Blade view file to serve as PDF templating file;

 touch resources/views/invoice.blade.php

Then, insert following code inside resources/views/invoice.blade.php;

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Invoice - {{$invoice->invoice_number}}</title>
    </head>
    <body style="margin: 20px; font-family: 'dejavu serif'; font-size: 12px;">
    <?php
        $fmt = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );
        $fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);
        $fmt->setSymbol(NumberFormatter::CURRENCY_SYMBOL, '$');
        $words = new NumberFormatter( 'en_US', NumberFormatter::SPELLOUT );
    ?>
        <br>
        <div>
            <table style="width: 100%;">
                <tr>
                    <td style="width: 40%; text-align: left;">
                        <h4>Ref: {{$invoice->invoice_number}}</h4>
                    </td>
                    <td style="text-align: center;">
                    </td>
                    <td style="width: 40%; text-align: right;">
                        <h1>LOGO HERE</h1>
                    </td>
                </tr>
                <tr>
                    <td style="width: 40%; text-align: left;">
                        <h4>{{ \Carbon\Carbon::parse($invoice->invoice_date)->format('F d, Y')}}</h4>
                    </td>
                    <td style="text-align: center;">
                    </td>
                    <td style="width: 40%; text-align: right;">
                    </td>
                </tr>
                <tr>
                    <td style="width: 60%; text-align: left;">
                        <h4>{{$invoice->customer->name}}
                        <br>{{$invoice->customer->address}}</h4>
                    </td>
                    <td style="text-align: center;">
                    </td>
                    <td style="width: 40%; text-align: right;">
                    </td>
                </tr>
            </table>
        </div>
        <br/>
        <div>
            We append a memo of our charges for professional services rendered. We shall be glad
            to receive an early remittance.
            <br>
            <br>
            <br>
            <table style="border-collapse: collapse; width: 100%;">
                <thead>
                <tr>
                    <th style="border: 1px solid gray; width: 80%; background-color: gray; color: white; font-size: 14px;">Description</th>
                    <th style="margin: 10px; padding: 10px; border: 1px solid gray;  text-align: center; width: 20%; background-color: gray; color: white; font-size: 14px;" >Amount</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td style=" border: 1px solid gray; height: 200px;"><strong>{{$invoice->description}}</strong></td>
                    <td style=" margin: 10px; padding: 10px; border: 1px solid gray; text-align: right;"><strong>{{ $fmt->formatCurrency($invoice->amount,'') }}</strong></td>
                </tr>
                <tr>
                    <td style=" border-right: 1px solid gray;  "><strong>Total</strong></td>
                    <td style=" margin: 10px; padding: 10px; border-right: 1px solid gray; border-bottom: 5px double gray; text-align: right;"><strong>{{ $fmt->formatCurrency($invoice->amount,'') }}</strong></td>
                </tr>
                </tbody>
            </table>
            <div style="margin: 18px;">
                <strong>USD {{$words->format($invoice->amount) }} only.</strong></td>
            </div>
        </div>
    </body>
</html>

Now, change routes/web.php as follows;

<?php

use Illuminate\Support\Facades\Route;
use App\Models\Invoice;
use Barryvdh\DomPDF\Facade\Pdf;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/pdf', function () {
    $invoice = Invoice::find(1);
    $pdf = Pdf::loadView('invoice', compact('invoice'));
    return $pdf->stream($invoice->invoice_number.'.pdf');
});

Run PHP development server;

php artisan serv

Enter following URL to get first invoice from invoices table in PDF format;

http://localhost:8000/pdf

Leave a Comment