Laravel – generate PDF from html view file and download using dompdf

Laravel – generate PDF from html view file and download using dompdf

In this post we will give you information about Laravel – generate PDF from html view file and download using dompdf. Hear we will give you detail about Laravel – generate PDF from html view file and download using dompdfAnd how to use it also give you demo for it if it is necessary.

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' => [

....

BarryvdhDomPDFServiceProvider::class,

],

'aliases' => [

....

'PDF' => BarryvdhDomPDFFacade::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 AppHttpControllers;


use AppHttpRequests;

use IlluminateHttpRequest;

use DB;

use PDF;


class ItemController extends Controller

{


/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

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 >


<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.

Hope this code and post will helped you for implement Laravel – generate PDF from html view file and download using dompdf. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

17  +    =  24

We accept paid advance and paid guest Posting on our Site : Contact US