How To Generate Invoice PDF In Laravel 10

Spread the love

Hello Guys,

In this article, we’ll explore the process of generate invoice PDF in Laravel, with a focus on the latest version, Laravel 10. Whether you’re using Laravel 7, Laravel 8, Laravel 9, or the most up-to-date Laravel 10, we’ve got you covered. Creating invoice PDF files becomes essential when managing an e-commerce website. Fortunately, Laravel 10 offers the capability to effortlessly produce dynamic invoices from your database.

Let’s delve into the steps for crafting an invoice in Laravel 8, and discover how to generate a PDF invoice in Laravel 9 and Laravel 10. Laravel 8 empowers you to create PDF files for your invoices, and this functionality extends to Laravel 9 as well.

Generate Invoice PDF In Laravel Examples:

Step 1: Install Laravel 10

In this phase, we will employ the following command to install the Laravel 10 application.

Also Read: How to get .env variable in blade and controller in LARAVEL?

composer create-project --prefer-dist laravel/laravel laravel_10_invoice_pdf_example

Step 2: Install Dompdf Package

To enhance your website’s functionality, we’ll proceed to install the “barryvdh/laravel-dompdf” package using the composer command. Simply execute the following command to get started. This will help optimize your content for search engines.

Also Read: How to Create Custom Helper Functions in LARAVEL 10

composer require barryvdh/laravel-dompdf

The app.php configuration file will be updated to include the service provider and alias.

config/app.php
'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
  
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
]

Step 3: Add Route

In this phase, we’ll incorporate a new route into the web.php file.

Also Read: How to Add Foreign Key in MIGRATION using Laravel?

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\InvoiceController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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


Route::get('invoice', [InvoiceController::class, 'Invoice']);

The following command will be employed to generate an InvoiceController at this moment.

php artisan make:controller InvoiceController
app/Http/Controllers/InvoiceController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class InvoiceController extends Controller
{
    public function Invoice()
    {
        $pdf = PDF::loadView('invoice_pdf');

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

Step 4: Create Blade File

Now, we’ll proceed to download the PDF file and create an invoice PDF blade.php file. Additionally, you have the option to utilize HTML code for transmitting dynamic data to a PDF file.

Also Read: How to Use LARAVEL DB Raw Query?

resources/views/invoice_pdf.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>How To Generate Invoice PDF In Laravel 10 - itcodestuff</title>
</head>
<style type="text/css">
    body{
        font-family: 'Roboto Condensed', sans-serif;
    }
    .m-0{
        margin: 0px;
    }
    .p-0{
        padding: 0px;
    }
    .pt-5{
        padding-top:5px;
    }
    .mt-10{
        margin-top:10px;
    }
    .text-center{
        text-align:center !important;
    }
    .w-100{
        width: 100%;
    }
    .w-50{
        width:50%;   
    }
    .w-85{
        width:85%;   
    }
    .w-15{
        width:15%;   
    }
    .logo img{
        width:200px;
        height:60px;        
    }
    .gray-color{
        color:#5D5D5D;
    }
    .text-bold{
        font-weight: bold;
    }
    .border{
        border:1px solid black;
    }
    table tr,th,td{
        border: 1px solid #d2d2d2;
        border-collapse:collapse;
        padding:7px 8px;
    }
    table tr th{
        background: #F4F4F4;
        font-size:15px;
    }
    table tr td{
        font-size:13px;
    }
    table{
        border-collapse:collapse;
    }
    .box-text p{
        line-height:10px;
    }
    .float-left{
        float:left;
    }
    .total-part{
        font-size:16px;
        line-height:12px;
    }
    .total-right p{
        padding-right:20px;
    }
</style>
<body>
<div class="head-title">
    <h1 class="text-center m-0 p-0">Invoice</h1>
</div>
<div class="add-detail mt-10">
    <div class="w-50 float-left mt-10">
        <p class="m-0 pt-5 text-bold w-100">Invoice Id - <span class="gray-color">#1</span></p>
        <p class="m-0 pt-5 text-bold w-100">Order Id - <span class="gray-color">AB123456A</span></p>
        <p class="m-0 pt-5 text-bold w-100">Order Date - <span class="gray-color">22-01-2023</span></p>
    </div>
    <div class="w-50 float-left logo mt-10">
        <img src="/assets/img/logo.png" alt="Logo">
    </div>
    <div style="clear: both;"></div>
</div>
<div class="table-section bill-tbl w-100 mt-10">
    <table class="table w-100 mt-10">
        <tr>
            <th class="w-50">From</th>
            <th class="w-50">To</th>
        </tr>
        <tr>
            <td>
                <div class="box-text">
                    <p>Mountain View,</p>
                    <p>California,</p>
                    <p>United States</p>                    
                    <p>Contact: (650) 253-0000</p>
                </div>
            </td>
            <td>
                <div class="box-text">
                    <p> 410 Terry Ave N,</p>
                    <p>Seattle WA 98109,</p>
                    <p>United States</p>                    
                    <p>Contact: 1-206-266-1000</p>
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="table-section bill-tbl w-100 mt-10">
    <table class="table w-100 mt-10">
        <tr>
            <th class="w-50">Payment Method</th>
            <th class="w-50">Shipping Method</th>
        </tr>
        <tr>
            <td>Cash On Delivery</td>
            <td>Free Shipping - Free Shipping</td>
        </tr>
    </table>
</div>
<div class="table-section bill-tbl w-100 mt-10">
    <table class="table w-100 mt-10">
        <tr>
            <th class="w-50">SKU</th>
            <th class="w-50">Product Name</th>
            <th class="w-50">Price</th>
            <th class="w-50">Qty</th>
            <th class="w-50">Subtotal</th>
            <th class="w-50">Tax Amount</th>
            <th class="w-50">Grand Total</th>
        </tr>
        <tr align="center">
            <td>M101</td>
            <td>Andoid Smart Phone</td>
            <td>$500.2</td>
            <td>3</td>
            <td>$1500</td>
            <td>$50</td>
            <td>$1550.20</td>
        </tr>
        <tr align="center">
            <td>M102</td>
            <td>Andoid Smart Phone</td>
            <td>$250</td>
            <td>2</td>
            <td>$500</td>
            <td>$50</td>
            <td>$550.00</td>
        </tr>
        <tr align="center">
            <td>T1010</td>
            <td>Andoid Smart Phone</td>
            <td>$1000</td>
            <td>5</td>
            <td>$5000</td>
            <td>$500</td>
            <td>$5500.00</td>
        </tr>
        <tr>
            <td colspan="7">
                <div class="total-part">
                    <div class="total-left w-85 float-left" align="right">
                        <p>Sub Total</p>
                        <p>Tax (18%)</p>
                        <p>Total Payable</p>
                    </div>
                    <div class="total-right w-15 float-left text-bold" align="right">
                        <p>$7600</p>
                        <p>$400</p>
                        <p>$8000.00</p>
                    </div>
                    <div style="clear: both;"></div>
                </div> 
            </td>
        </tr>
    </table>
</div>
</html>

Step 5: Run Laravel 10 Application

Today, we’ll harness the power of Laravel 10 as we execute a command to create a PDF invoice file.

Also Read: Ajax Form Validation Example using LARAVEL 10

php artisan serve

Uncover the ultimate insights in our collection, your gateway to the best Laravel blog content.

Leave a Comment