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

by Admin


Posted on August 24, 2014 at 9:00 PM

Tags : : laravel


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.

 

 

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

 


 

 

 

 

 

 

 

Download PDF

 

 

 

 

 

 

 

 

 

 

 

 

 

@foreach ($items as $key => $item)

 

 

 

 

 

 

 

 

 

 

@endforeach

 

No Title Description
{{ ++$key }} {{ $item->title }} {{ $item->description }}

 

 

 

 

 

Now you can run and check, you can get more information about package from here : laravel-dompdf.

 

Source: http://itsolutionstuff.com/post/laravel-5-generate-pdf-from-html-view-file-and-download-using-dompdfexample.html



Comments


You must be loged to add a comment !