DEV Community

Cover image for How to Generate Invoice PDF in Laravel?
Msh Sayket
Msh Sayket

Posted on

How to Generate Invoice PDF in Laravel?

In this tutorial, I will show you How to Generate Invoice PDF in Laravel. We will use laravel generate invoice pdf. you can understand a concept of how to generate invoice pdf in laravel using dompdf. This tutorial will give you a simple example of laravel dompdf invoice pdf design. Alright, let’s dive into the steps. You Can Learn Laravel Blade Check If Variable Exists or Not Example

In this guide, I’ll walk you through the process of creating a PDF invoice template design within a Laravel application. We’ll utilize the dompdf composer package to generate the PDF file. The next steps involve crafting straightforward HTML and CSS code to establish a clean and standardized layout for the invoice PDF. Let’s proceed by following the outlined steps:

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Step 1: Install Laravel 11

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Controller

In this step, we will create InvoiceController with index() where we write code to generate pdf. so let’s create a controller using the bellow command.

php artisan make:controller InvoiceController
Enter fullscreen mode Exit fullscreen mode

Now, update the code on the controller file.

app/Http/Controllers/InvoiceController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;



class InvoiceController extends Controller
{

    /**
     * Write code on Method
     *
     * @return response()
     */

    public function index(Request $request)

    {

        $data = [
            [

                'quantity' => 2,
                'description' => 'Gold',
                'price' => '$500.00'

            ],

            [

                'quantity' => 3,
                'description' => 'Silver',
                'price' => '$300.00'

            ],

            [

                'quantity' => 5,
                'description' => 'Platinum',
                'price' => '$200.00'

            ]

        ];



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

    }

}
Enter fullscreen mode Exit fullscreen mode

Step 4: Add Route

Furthermore, open the routes/web.php file and update the code on it.

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('invoice-pdf', [InvoiceController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

Read More

Top comments (0)