Laravel Generate PDF from HTML View File and Download Example

By Hardik Savani November 5, 2023 Category : 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.

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




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


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:


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();



$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:


<style type="text/css">

table td, table th{

border:1px solid black;



<div class="container">


<a href="{{ route('pdfview',['download'=>'pdf']) }}">Download PDF</a>







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


<td>{{ ++$key }}</td>

<td>{{ $item->title }}</td>

<td>{{ $item->description }}</td>





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.

Tags :