Laravel 12 - How To Generate Invoices
Laravel 12 - How To Generate Invoices
In this tutorial, we will learn how to generate professional PDF invoices in Laravel 12 using the laraveldaily/laravel-invoices package with buyers, sellers, items, custom styles, and filenames.
If you're a video person, feel free to skip the post and check out the video instead!
Quick Overview
This guide walks you through creating an invoice generation system in a Laravel 12 application. Whether you’re starting a fresh Laravel 12 project or working with an existing one, we’ll cover all the key steps. You’ll begin by installing the laraveldaily/laravel-invoices package and publishing its configuration and view files. Then, we’ll create an InvoiceController to handle invoice creation, including setting up seller and buyer details, adding invoice items, and customizing the invoice’s appearance, layout, and filename. Routes are configured to handle invoice generation requests, and we’ll test everything by clearing cached configurations, starting the server, and accessing the invoice URL. Finally, you can make additional tweaks to the invoice content or layout to suit your application’s needs.
Step # 1 : Set Up a Fresh Laravel 12 Project or Use an Existing One.
Let’s start by setting up a Laravel 12 application. If you don’t already have the Laravel installer installed globally, you can do so with this command.
composer global require laravel/installer
This gives you quick access to the laravel CLI tool, making it easier to create new projects. If you already have it installed, you can skip this step and go straight to creating a fresh project using.
laravel new invoice
Follow these selections when going through the setup prompts.
- Would you like to install the starter kit? — Choose None.
- Next, you’ll be asked which testing framework to use. — Select Pest.
- Then, select the database for your application. — Choose MySQL.
- After MySQL, you’ll be asked whether to run the default database migrations. — Type yes.
- Finally, confirm npm install and npm run build to install and compile all frontend assets automatically.
Once the setup is complete, you’ll have a fresh Laravel 12 application with Pest ready for testing, MySQL connected as your database, and all frontend assets fully compiled. Your project is now set up and ready for development.
Step # 2 : Access Your Laravel Project.
Open a terminal (for example, Git Bash) and navigate to the root directory of your Laravel 12 project using the cd command. For example.
cd c:xampp/htdocs/invoice
This will set your terminal to the project folder so you can start running commands and developing your application.
Step # 3 : Add Invoice Capabilities to Your Laravel 12 Project.
To add invoice functionality to your Laravel 12 project, run the following command in your project root.
composer require laraveldaily/laravel-invoices
This command will download and install the laraveldaily/laravel-invoices package along with its dependencies, adding it to your vendor directory and updating the composer.json file. Since this package includes configuration and view files, you’ll need to publish them using Artisan to make them available for customization in your project.
Step # 4 : Publish Configuration and Invoice Views.
To customize the package settings, run the following command to publish the configuration file.
php artisan vendor:publish --tag=invoices.config
This will create the configuration file at config/invoices.php, where you can adjust options such as currency, default invoice format, and other invoice-related settings. Next, to customize the invoice templates, run.
php artisan vendor:publish --tag=invoices.views
This will publish the invoice view files to resources/views/vendor/invoices/, allowing you to modify the layout and design of the invoices generated by your application.
Step # 5 : Generate the Invoice Controller.
Use the following Artisan command to create a new controller called InvoiceController.
php artisan make:controller InvoiceController
After generating the controller, update it with the code below to enable invoice creation using the Laravel Invoices package.
<?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()
{
// Set up buyer details
$customer = new Buyer([
'name' => 'Code Shotcut',
'custom_fields' => [
'email' => 'codeshotcut@gmail.com',
],
]);
// Define items to include in the invoice
$items = [
(new InvoiceItem())->title('Product 1')->pricePerUnit(50),
(new InvoiceItem())->title('Service 1')->pricePerUnit(100),
];
// Create and configure the invoice
$invoice = Invoice::make()
->buyer($customer)
->date(now())
->addItems($items)
->currencySymbol('$')
->currencyCode('USD')
->notes("Thank you for your business!"); // Added a custom note
// Stream the invoice as a PDF in the browser
return $invoice->stream();
}
}
This controller includes a generateInvoice() method that creates a PDF invoice with buyer information and multiple items. It leverages the Buyer class for customer details and the InvoiceItem class for invoice line items. Once generated, the invoice is streamed directly to the browser for viewing and downloading. You can further enhance the controller to include seller details, apply custom formatting, add discounts, invoice numbers, or other features. With this setup, your Laravel 12 application now supports dynamic invoice generation and can be easily extended to suit your specific needs.
Step # 6 : Set Up the Invoice Route.
First, import the InvoiceController at the top of your web.php file.
use App\Http\Controllers\InvoiceController;
Then, create a route to generate and display the invoice.
Route::get('/invoice', [InvoiceController::class, 'generateInvoice']);
Now, when you visit /invoice in your browser, the generateInvoice() method will be triggered, generating and streaming the invoice as a PDF for viewing or download.
Step # 7 : Test the Invoice Generation.
Before testing, clear any cached configurations, views, and routes to ensure the latest changes are applied.
php artisan optimize:clear
Next, start the Laravel development server.
php artisan serve
Now, open your browser and visit: http://127.0.0.1:8000/invoice.
If you see an Internal Server Error: Class 'NumberFormatter' not found, stop the running Laravel server by pressing Ctrl + C in the terminal. Then, open your php.ini file and uncomment the following line.
From:
;extension=intl
To:
extension=intl
Save the changes, restart your Laravel server, and reload the page, your generated invoice should now appear.
You can take your invoices further by customizing item descriptions, applying discounts, adding a unique invoice number, including seller details, specifying payment terms, or adjusting the styling. Here’s an updated example showing multiple items, updated seller info, and a custom invoice filename.
<?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()
{
// Set up buyer details
$customer = new Buyer([
'name' => 'All About Laravel',
'custom_fields' => [
'email' => 'allaboutlaravel@gmail.com',
],
]);
// Set up seller details using Buyer class
$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',
],
]);
// Define invoice items
$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),
];
// Create and configure the invoice
$invoice = Invoice::make()
->buyer($customer)
->seller($seller)
->date(now())
->addItems($items)
->currencySymbol('$')
->currencyCode('USD')
->notes("Thank you for your business!"); // Added a custom note
// Set a custom filename for the downloaded invoice
$invoice->filename('Codeshotcut-invoice');
// Download the invoice as PDF
return $invoice->download();
}
}
Once you visit the URL: http://127.0.0.1:8000/invoice, the invoice will be downloaded as Codeshotcut-invoice.pdf. You can continue enhancing the invoice by updating item descriptions, applying custom styles, or including payment terms and discounts.
If you want to change the layout or modify how the content is displayed, you can edit the default view provided by the package. After publishing the views (if not done already), the template will be available at resources/views/vendor/invoices/default.blade.php, where you can adjust the styling, structure, and layout of the invoice to match your application’s requirements.
Conclusion
By following this guide, you've successfully implemented an invoice generation system in your Laravel application. You now have the ability to create, customize, and download invoices complete with buyer and seller details, multiple items, and tailored layouts. The laraveldaily/laravel-invoices package makes it easy to produce professional-looking invoices, and with a few tweaks, you can adapt the system to your unique requirements. You can further refine the invoice design, apply discounts, include custom invoice numbers, and define payment terms to fully suit your workflow.
For more details, refer to the laravel-invoices package documentation.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments