Generate pdf file with laravel dom-pdf laravel 9
Wed Mar 01 2023 09:56:00 GMT+0000 (Coordinated Universal Time)
Saved by @ahmad007
1. Install barryvdh/laravel-dompdf Package In Your Application composer require barryvdh/laravel-dompdf 2. Add Service Provider And Aliase, After package installation, we will add service provider and aliase in config/app.php. 'providers' => [ Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ 'PDF' => Barryvdh\DomPDF\Facade::class, ], 3. Now create a new controller and writhe the following command in header of that controller use PDF; Now make a function to generate pdf and enter the following code $customer = Part::find($id); $pdf = PDF::loadView('customers.partPDF', compact('customer'))->setPaper('a4'); return $pdf->stream('customerPDF.pdf'); 4. Now make a blade file and then wite the following smaple code <style> td, th { border: 1px solid gray; padding: 8px; } table{ border-collapse: collapse; width: 100%; } .heading { text-align: left; } td, th{ font-size: 12px; } th{ background-color: black; color: white; } .center{ margin-left: auto; margin-right: auto; } .main-border{ border: 2px solid gray; margin: 10px; padding: 20px; } </style> <div class="main-border"> <div> <img src="assects/images/leaf2.png" width="30px" alt=""> <span class="heading" style="font-size: 20px">Sardar Rice Mills</span> <div style="text-align: center"><b>Sale Invoice</b> [{{$mytime}}]</div> </div> <div style="text-align: right">Sardar Rice Mills <br> Dinga Road, Near Keeranwala <br> Tehsiel and District Gujrat <br> 03001234xxx </div> <span>To, <br> {{$customer->name}}<br> {{$customer->contact}}<br> {{$customer->address}}</span> <br> <br> <table class="center"> <thead> <tr> <th>Sale Id[Date]</th> <th>Description</th> <th>Rate(/40kg)(Rs.)</th> <th>Quantity(kg)</th> <th>Amount(Rs.)</th> </tr> </thead> <tbody> <tr> <td rowspan="5">{{$sale->id}} [{{$sale->created_at}}]</td> <td>{{$sale->product->name }} (Id.{{$sale->product_id}})</td> <td>{{number_format($sale->product_rate, 2)}}</td> <td>{{number_format($sale->sale_quantity, 2)}}</td> <td>{{number_format($sale->sub_total, 2)}}</td> </tr> <tr> <td>Extra (Rs.)</td> <td></td> <td></td> <td>{{number_format($sale->extra, 2)}}</td> </tr> <tr> <td>Discount ({{$sale->discount}}%)</td> <td></td> <td></td> <td>{{number_format((($sale->sub_total*$sale->discount)/100), 2)}}</td> </tr> <tr> <td>Comission ({{$sale->comission}}%)</td> <td></td> <td></td> <td>{{number_format((($sale->sub_total*$sale->comission)/100), 2)}}</td> </tr> <tr> <td>Total (Rs.)</td> <td></td> <td></td> <td>{{number_format($sale->total, 2)}}</td> </tr> </tbody> <tfoot> <tr> <td id="total" colspan="4" style="text-align: right; border:none">Total :</td> <td><b>{{number_format($sale->total, 2)}}</b></td> </tr> <tr> <td id="total" colspan="4" style="text-align: right; border:none">Paid Amount :</td> <td>{{number_format($sale->paid_amount, 2)}}</td> </tr> <tr> <td id="total" colspan="4" style="text-align: right; border:none">Pending Amount :</td> <td>{{number_format($sale->pending_amount, 2)}}</td> </tr> </tfoot> </table> <div style="text-align: center; margin-top: 40px">Thankyou!</div> </div>
Comments