DEV Community

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

Posted on

2

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay