In this article, we will see how to generate an invoice pdf in laravel 8/9/10. Here, we will learn about how to create invoice pdf files in laravel 7, laravel 8, laravel 9, and laravel 10. Sometimes we are working on an e-commerce website at that time we need to generate invoices for items.
So, you can create dynamic invoices from the database in laravel 9 and laravel 10
So, let's see how to create an invoice pdf in laravel 9/10, laravel 9 generate an invoice pdf file, generate an invoice pdf in laravel 8, and how to make an invoice in laravel 8 and laravel 9.
We will install the laravel 9 application using the following command in this step.
composer create-project --prefer-dist laravel/laravel laravel_9_invoice_pdf_example
Now, we will install barryvdh/laravel-dompdf package using the composer command. So, run the following command.
composer require barryvdh/laravel-dompdf
After that, we will add the service provider and alias to the app.php config file.
config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
In this step, we will add a route to the web.php file.
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']);
Now, we will create an InvoiceController using the following command.
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('techsolutionstuff.pdf');
}
}
Now, we will create an invoice_pdf.blade.php file and download the pdf file. Also, you can pass dynamic data to a pdf file to the HTML code.
resources/views/invoice_pdf.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How To Generate Invoice PDF In Laravel 9 - Techsolutionstuff</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="https://techsolutionstuff.com/frontTheme/assets/img/logo_200_60_dark.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>
Now, we will run laravel 9 to create an invoice pdf file using the following command.
php artisan serve
Output:
You might also like: