Laravel Generate PDF from HTML View File and Download Example
Normally, if we work on big ERP level project on laravel, we require to generate PDF file for data from database table. In this tutorial i give you very simple way to create pdf file and give to download that pdf step by step for beginners.
In this post i use laravel-dompdf package for create pdf file and this package also provide to download function. In this example i have a one table "items" and i will download pdf file of items table data with tabular format, You can also write your own css on view file for pdf, so you can make batter pdf file.
So, finally you have to just follow few step and get pdf generate and download, first you have fresh project of Laravel 5 or Laravel 5.3 etc with work.
Step 1: Installation
In first step we have to download laravel-dompdf plugin for generate pdf file from view blade file. So first run bellow command in your terminal:
composer require barryvdh/laravel-dompdf
Now open config/app.php file and add service provider and aliase.
'providers' => [....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
],
Step 2: Add Route
In this is step we need to add route for generate view. so open your app/Http/routes.php file and add following route.
Route::get('pdfview',array('as'=>'pdfview','uses'=>'ItemController@pdfview'));
Step 3: Create Controller
Ok, now we should create new controller as ItemController in this path app/Http/Controllers/ItemController.php. Make sure you should have items table with some data. this controller will manage data and genarate pdf file, so put bellow content in controller file:
app/Http/Controllers/ItemController.php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;
class ItemController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function pdfview(Request $request)
{
$items = DB::table("items")->get();
view()->share('items',$items);
if($request->has('download')){
$pdf = PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf');
}
return view('pdfview');
}
}
Step 4: Create View File
In last step, we have to create view file "pdfview.blade.php" for generate view and also pdf file, so create pdfview file and put bellow code:
resources/view/pdfview.blade.php
<style type="text/css">
table td, table th{
border:1px solid black;
}
</style>
<div class="container">
<br/>
<a href="{{ route('pdfview',['download'=>'pdf']) }}">Download PDF</a>
<table>
<tr>
<th>No</th>
<th>Title</th>
<th>Description</th>
</tr>
@foreach ($items as $key => $item)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $item->title }}</td>
<td>{{ $item->description }}</td>
</tr>
@endforeach
</table>
</div>
Now you can run and check, you can get more information about package from here : laravel-dompdf.
Laravel 5 export to pdf using maatwebsite example
Laravel 5 import export to excel and csv using maatwebsite example.
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Check Query Execution Time in Laravel?
- Laravel Password and Confirm Password Validation Example
- Laravel Remove All Spaces from String Example
- Laravel withCount() with Where Condition Example
- Laravel 10 Image Validation Rule Example
- Laravel 10 Store Backup on Dropbox using Spatie Tutorial
- Laravel - How to Get All Files in a Directory?
- Laravel Cookies - Get, Set, Delete Cookie Example
- How to Check Laravel Version of a Your Project?
- Laravel Migration Enum Default Value Example
- Laravel Telescope Installation and Configuration Tutorial
- How to Generate UUID in Laravel?
- How to Get Last Record from Database in Laravel?