In this article, i will let you explain how to generate pdf file from html view in laravel 5.5 application. We mostly require to generate PDF file from html view for invoice, receipt, products etc.
There are several composer packages available for pdf generate in laravel 5.5 application, However we can use "laravel-dompdf" library. laravel-dompdf provide to generate very easily using laravel balde file.
So in this example i will install laravel-dompdf package and then we will generate file using dompdf in laravel 5.5 project. So let's simply follow bellow step.
Step 1: Install laravel-dompdf Package
First of the thing is we have to install barryvdh/laravel-dompdf composer package for generate pdf file. So simply run bellow composer command for install package.
composer require barryvdh/laravel-dompdf
After install successfully above dompdf package we need to add into providers and aliases array of configuration file. So let's add following way:
config/app.php
<?php
return [
....
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 2: Add Test Route
Now we will create new test route for download pdf and we will write code for download pdf using DomPDF library. So let's add following route:
routes/web.php
Route::get("download-pdf","HomeController@downloadPDF");
Step 3: Add Controller Method
In third step we need to add downloadPDF() on HomeController file. So there we will write generate pdf file code, so add bellow method on home controller:
app/Http/Controllers/HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class HomeController extends Controller
{
public function downloadPDF()
{
$pdf = PDF::loadView('pdfView');
return $pdf->download('invoice.pdf');
}
}
Step 4: add pdfView.blade.php file
In the last step, we have to create pdfView.blade.php view blade file. In this file we have to just write some html table code. So let's create blade file and put bellow code:
resources/views/pdfView.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Load PDF</title>
<style type="text/css">
table{
width: 100%;
border:1px solid black;
}
td, th{
border:1px solid black;
}
</style>
</head>
<body>
<h2>Load PDF File</h2>
<table>
<tr>
<th>No.</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>Vimal Kashiyani</td>
</tr>
<tr>
<td>2</td>
<td>Hardik Savani</td>
</tr>
<tr>
<td>1</td>
<td>Harshad Pathak</td>
</tr>
</table>
</body>
</html>
Now you are ready to run full example.
I hope you found your best solution...
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example