Table of Contents
Laravel is a powerful web application framework built in PHP that cando anything beyond your imagination. Today I have written a tutorial on how you can generate a pdf invoice in your laravel application. Well, in lots of websites like ecommerce, it's required to make a systemof downloading invoice(bill) in pdfformat.We will be using a package called 'laravel-dompdf' .
This is the package url on github

https://github.com/barryvdh/laravel-dompdf
Steps:
1) Install this Package
execute below command to install this package composer require barryvdh/laravel-dompdf
After updating composer, add the ServiceProvider to the providers array in config/app.php Barryvdh\DomPDF\ServiceProvider::class,
You can optionally use the facade for shorter code. Add this to your facades: 'PDF' => Barryvdh\DomPDF\Facade::class,
2) Put inside routes file routes/web.php
inside admin middlewareRoute::get('/admin/order/download_invoice/{id}','Admin\OrderController@download_invoice');
3) Put below function inside OrderController.php of App\Http|Controllers\Admin
function download_invoice($oid){
$order=Order::find($oid);
$invoice_date = date('jS F Y', strtotime($order->invoice_date));
$pdf = PDF::loadView('includes.invoice_template',array('order'=>$order));
return $pdf->download('Invoice_'.config('app.name').'_Order_No # '.$oid.' Date_'.$invoice_date.'.pdf');
}
also inject use PDF;
4) Now create Invoice template 'invoice_template.blade.php' inside resources/views/includes
and put below inside this file <?php
$admin_email=config('app.admin_email');
$admin_mobile=config('app.admin_mobile');
$shop_address=config('app.shop_address');
?> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <link href="{{asset('public/css/custom.css')}}" rel="stylesheet"> <!------ Include the above in your HEAD tag ----------> <style type="text/css"> body{
width:100%!important;
font-size:12px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif!important;
}
*{
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif!important;
}
.container{
width: 700px;
}
.outer_border{
border:1px solid #999999!important;
padding:4%!important;
margin-bottom:2%!important;
}
.top_box{
width:47%; padding:0%
}
.table_pad{
padding:0% 2%;
}
.border{
border:1px solid #CCCCCC!important;
}
.small_text{
font-size:10px!important;
}
.bg_color1{
background:#3a5082;
color: #fff;
}
.text_color1{
color:#3a5082;
}
td{
padding:4px;
} </style> <?php
$OrderController=new App\Http\Controllers\Admin\OrderController;
?><div class="container"><div class="outer_border"> <div class="row"> <div class=" pull-left top_box p-4"> <h 2 class="text_color1" style="font-size:30px">{{config('app.name')}}</h 2> {{$shop_address}}
Phone : {{$admin_mobile}}
Email : {{$admin_email}}
Website : {{config('app.url')}} </div> <div style="" class=" pull-right top_box p-4"> <h2 style="color:#687cbf;font-weight: bold;font-size:30px; text-align:right; padding-right: 30px;" style="color:#687cbf;font-weight: bold;font-size:30px; text-align:right; padding-right: 30px;" id="invoice">INVOICE</h2> <table width="100%" height="70" border="0" class="table_pad"> <tr> <td > Date</td> <td> <?php $invoice_date = date('jS F Y', strtotime($order->created_at));
echo $invoice_date;
?> </td> </tr> <tr> <td width="50%">Invoice #</td> <td width="50%">{{$order->invoice_no}}</td> </tr> <tr> <td>Customer ID</td> <td>{{$order->id}}</td> </tr> </table></div> </div> <div class="row"> <div class=""> <table width="100%" border="0"> <tr> <td colspan="2"><div class="bg_color1" style="text-indent:10px;font-size: 14px;width: 50%;height: 26px;line-height: 24px; ">BILL TO </div> <table width="100%" border="0"> <tr> <td width="18%">Name</td> <td width="82%"> {{$order->billing_name}} </td> </tr> <tr> <td>Phone</td> <td>{{$order->billing_phone}}</td> </tr> <tr> <td> Address</td> <td> {{$order->billing_address}} ,
{{$order->billing_city}},
{{$order->billing_state}},
{{$order->billing_zip}}, </td> </tr> </table> </td> </tr> <tr> <td colspan="2"> </td> </tr> </table> </div> </div> <dd style="clear:both;"></dd><div class="row"> <table height="82" class=" " style=" width:100%;"> <tr class="bg_color1"> <td width="58%" height="12" style="padding-left: 10px;">DESCRIPTION</td> <td width="13%"> </td> <td style="padding-right: 10px;" width="15%" align="right">AMOUNT</td> </tr> @foreach($order->order_items as $order_item) <tr class=" "> <td> {{$order_item->title}}
Rs. {{$order_item->cost }} X {{$order_item->qty}} </td> <td> </td> <td align="right">Rs. {{$order_item->sub_total}}</td> </tr> @endforeach <tr class=" "> <td> </td> <td> <strong>Total</strong> </td> <td align="right">Rs. {{$order->grand_total}}</td> </tr> </table></div> <div class="row"> <div style="text-align:center"> If you have any question about this invoice, please contact <br /> {{config('app.name')}}, {{$admin_mobile}}, {{$admin_email}}<br /> <b>Thank You For Your Business!</b> </div> </div> </div> </div>
5) Add below inside config/app.php for other global settings
'admin_email'=>'abc@domain.com',
'admin_mobile'=>'9999999999',
'shop_address'=>'WZ 12 B , abcd phase2, New Delhi 110092', <pre><code class="language-php"><script type="prism-html-markup"><h 3>6) Put below link inside admin's order listing view file</h 3> i.e resources/views/admin/order/index.blade.php <pre><code class="language-php"><script type="prism-html-markup"> <td><a href="{{url('/admin/order/download_invoice/'.$value->oid.'/')}}"><i class="fa fa-download" aria-hidden="true"></i> </a></td>
We have done everything for this. Now click this download button to test the invoice pdf.
In this way you can create a download pdf invoicelaravel , for more tutorials on laravel, visit our laravel tutorial blog section.
I am a website developer having 12+ years experience. If you have any query, you can put this in the comment section below. I will try my best to reply soon.You can also visit our website www.unitedwebsoft.in for website designing development related requirements.