Laravel 13 - How to Generate Custom PDF Invoices.

Touseef Afridi
23 Sep 26

Laravel 13 - How to Generate Custom PDF Invoices.

In this tutorial, we will learn how to generate custom PDF invoices in Laravel 13 using laraveldaily/laravel-invoices, add buyer and seller details, include multiple items, and customize the invoice template.

Quick Overview

This guide walks you through building an invoice generation feature with Laravel 13. We’ll begin with a new Laravel project and add the laraveldaily/laravel-invoices package to handle invoice creation. From there, we’ll publish the package resources, set up an InvoiceController, and configure the required buyer and invoice details. You’ll also learn how to connect the controller to a browser route and verify the generated PDF locally. Once the basic invoice is working, we’ll expand it with seller information, multiple products and services, a custom PDF filename, and a dedicated invoice template that you can modify to match your application’s design.

Step # 1 : Create a New Laravel 13 Application.

Let’s begin by setting up a fresh Laravel 13 project named invoice. Before creating the application, make sure Composer is installed and accessible from your terminal, as Laravel uses it to manage PHP packages and project dependencies. For this guide, we’ll use the Laravel Installer. If it isn’t installed on your machine yet, install it globally with.
composer global require laravel/installer
Once the Installer is ready, create a new Laravel application by running.
laravel new invoice  
During the installation process, Laravel will ask you to configure a few options. Use the following choices for this project.
  • Starter Kit: Select No. We don’t need any starter kit or pre-built authentication scaffolding for this application.
  • Frontend Stack: Choose Blade. This gives us a simple and lightweight frontend setup using Laravel’s built-in Blade templating engine.
  • Boost Features: Keep the default option and press Enter. There’s no need to add any extra Boost features at this stage.
  • Boost Integrations: Press Enter again to continue with the default configuration and skip additional Boost integrations.
  • AI Agents: When Laravel asks you to select an AI agent, choose Amp. This will include the required Amp configuration as the project is generated.

Once these options are selected, Laravel will create the project with No Starter Kit, Blade as the frontend stack, the default Boost settings, no Boost integrations, and Amp as the selected AI agent.

Step # 2 : Navigate to the Laravel Project.

To work with the newly created application, open your preferred terminal and navigate to the invoice project directory. You can use PowerShell, Command Prompt, Git Bash, or the integrated terminal in VS Code. Run the following command.
cd invoice
This command takes you directly to the project folder, allowing you to work with your Laravel files and run Artisan commands from there.

Step # 3 : Add Invoice Functionality to Your Laravel 13 Project.

For invoice generation, we’ll use the laraveldaily/laravel-invoices package. It provides the functionality needed to create invoices and can be customized according to the requirements of our application. Run the following command from the project root.
composer require laraveldaily/laravel-invoices
This installs the package along with its required dependencies and updates your composer.json file. The package is also added to the vendor directory. Since the package includes configuration and view files, we’ll publish them through Artisan so they’re available within the application and can be customized when needed.

Step # 4 : Publish the Invoice Configuration and Views.

The package provides both configuration and view files that can be published into your application, giving you full control over how the invoice functionality works and looks. Start by publishing the configuration file.
php artisan vendor:publish --tag=invoices.config
This creates config/invoices.php, where you can fine-tune the package settings, including the default currency, invoice format, and other available options. Next, publish the invoice views.
php artisan vendor:publish --tag=invoices.views
This will publish the invoice template to resources/views/vendor/invoices/, where you can customize its layout and styling to match your application.

Step # 5 : Generate the Invoice Controller.

Let’s create a dedicated controller to handle the invoice generation. Run the following Artisan command to create InvoiceController.
php artisan make:controller InvoiceController
After creating the controller, add the following code to InvoiceController to handle the invoice generation.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
class InvoiceController extends Controller
{
    public function generateInvoice()
    {
        // Add the customer information
        $customer = new Buyer([
            'name' => 'Code Shotcut',
            'custom_fields' => [
                'email' => 'codeshotcut@gmail.com',
            ],
        ]);
        // Add the products and services to the invoice
        $items = [
            (new InvoiceItem())->title('Product 1')->pricePerUnit(50),
            (new InvoiceItem())->title('Service 1')->pricePerUnit(100),
        ];
        // Build the invoice with the required details
        $invoice = Invoice::make()
            ->buyer($customer)
            ->date(now())
            ->addItems($items)
            ->currencySymbol('$')
            ->currencyCode('USD')
            ->notes("Thank you for your business!"); // Add a custom message to the invoice
        // Display the generated invoice PDF in the browser
        return $invoice->stream();
    }
}
The generateInvoice() method takes care of building the invoice from start to finish. The Buyer class is used for customer details, while InvoiceItem lets us add individual products or services. The generated invoice uses the current date, USD currency, and a custom message before being streamed as a PDF in the browser. Additional details such as seller information, invoice numbers, discounts, and custom formatting can be added later as needed.

Step # 6 : Add a Route for Invoice Generation.

To make the invoice accessible through the browser, we need to add a route that points to the generateInvoice() method in InvoiceController. First, import the controller in routes/web.php.
use App\Http\Controllers\InvoiceController;
Next, define the route that will handle invoice generation.
Route::get('/invoice', [InvoiceController::class, 'generateInvoice']);
With this route in place, visiting /invoice in your browser will call the generateInvoice() method and generate the invoice as a PDF. The resulting invoice will be streamed directly to the browser, where it can be viewed or downloaded.

Step # 7 : Verify the Invoice Generation.

It’s time to test the invoice and make sure everything is working as expected. Start by clearing Laravel’s cached configuration, views, and routes.
php artisan optimize:clear
Once the cache has been cleared, start the Laravel development server.
php artisan serve
Open your browser and visit http://127.0.0.1:8000/invoice to test the invoice route.

If you see the “Internal Server Error: Class 'NumberFormatter' not found” message instead, stop the Laravel server by pressing Ctrl + C. Then open your php.ini file and enable the intl extension by removing the semicolon from the following line.
From:
;extension=intl
To:
extension=intl
Save the file and start the Laravel server again. Reload the /invoice page in your browser, and the generated invoice should now be displayed.

The invoice can be extended with more practical details, such as seller information, additional items, a custom filename, discounts, payment terms, and styling. The following example adds seller details, several invoice items, and a custom name for the generated PDF.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
class InvoiceController extends Controller
{
    public function generateInvoice()
    {
        // Provide the customer's information
        $customer = new Buyer([
            'name' => 'All About Laravel',
            'custom_fields' => [
                'email' => 'allaboutlaravel@gmail.com',
            ],
        ]);
        // Add the seller information
        $seller = new Buyer([
            'name' => 'Code Shotcut',
            'address' => '123 Main St, City, State, ZIP',
            'email' => 'codeshotcut@gmail.com',
            'phone' => '123-456-7890',
            'custom_fields' => [
                'website' => 'https://codeshotcut.com',
                'SWIFT' => 'BANKABCXYZ',
            ],
        ]);
        // List the products and services included in the invoice
        $items = [
            (new InvoiceItem())->title('Product 1')->pricePerUnit(50),
            (new InvoiceItem())->title('Service 1')->pricePerUnit(100),
            (new InvoiceItem())->title('Product 2')->pricePerUnit(75),
            (new InvoiceItem())->title('Service 2')->pricePerUnit(150),
            (new InvoiceItem())->title('Product 3')->pricePerUnit(200),
            (new InvoiceItem())->title('Service 3')->pricePerUnit(125),
            (new InvoiceItem())->title('Product 4')->pricePerUnit(300),
        ];
        // Build the invoice with customer, seller, and item details
        $invoice = Invoice::make()
            ->buyer($customer)
            ->seller($seller)
            ->date(now())
            ->addItems($items)
            ->currencySymbol('$')
            ->currencyCode('USD')
            ->notes("Thank you for your business!"); // Include a custom message
        // Give the generated PDF a custom filename
        $invoice->filename('Codeshotcut-invoice');
        // Download the invoice as a PDF
        return $invoice->download();
    }
}
When you visit http://127.0.0.1:8000/invoice, the invoice will now be downloaded as Codeshotcut-invoice.pdf. From here, you can further customize the invoice by adding discounts, payment terms, detailed item descriptions, or your own styling.


To change the invoice layout or control how its content is presented, you can also customize the package’s default view. If the views haven’t been published yet, publish them first. The template will be available at resources/views/vendor/invoices/default.blade.php, where you can modify the invoice’s structure, styling, and overall appearance to suit your application.

Conclusion

By following this guide, you've successfully set up invoice generation in your Laravel 13 application. You’ve learned how to install and configure the laraveldaily/laravel-invoices package, create an invoice controller, add customer and seller information, and generate invoices as PDF files. The setup also gives you the flexibility to add more invoice items, customize the downloaded filename, and modify the default invoice template to fit your application’s design. You can continue improving the invoice by adding features such as discounts, payment terms, custom invoice numbers, and additional styling. 
For more details, refer to the laraveldaily/laravel-invoices package documentation: https://github.com/LaravelDaily/laravel-invoices.
Share this with friends!


"Give this post some love and slap that 💖 button as if it owes you money! 💸😄"
0

0 Comments

To engage in commentary, kindly proceed by logging in or registering