Laravel 5 - generate PDF from html view file and download using dompdf

By Hardik Savani | July 15, 2016 | | 79494 Viewer | Category : Laravel


Share this post:

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.



We are Recommending you: