How to convert html to pdf laravel 5.4

  245722 views   5 years ago Laravel

Today laravelcode share with you how to convert html to pdf in laravel and how to download pdf file which generate fron html layout. in many big laravel project like e-commerce, erp and industrial relations related project you required this type functionality in laravel.

In this tutorials we are using on of the best package barryvdh/laravel-dompdf it provide very easy way to generate html to pdf in laravel.

We are in this tutorials create one static html pdf with table. but you are create one also dynamic data. like you can get data from your database and also make some action on it. we are also explain in this tutorials how to padd data in pdf view it is very simple and stupid way.

We are explain with all code step by step.

Step : 1 Install package

First we need to install package in our laravel application using following command

composer require barryvdh/laravel-dompdf

Step : 2 Configure package

After install pdf package for laravel then we must be configure it. just following littel thing. oprn your config/app.php file and set following value for providers array and aliases array

'providers' => [
'aliases' => [
	'PDF' => Barryvdh\DomPDF\Facade::class,

After configure app.php file then open your bootstrap/app.php file and add following line like that.

Please make sure add following line above the return $app;


After adding above line then run following command in your terminal/cmd

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

Step : 3 Create route

After done configuration then create one route in your routes/web.php file

Route::get('generate-pdf', '[email protected]')->name('generate-pdf');

Step : 4 Create controller

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use PDF;

class PdfGenerateController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function pdfview(Request $request)
        $users = DB::table("users")->get();

        	// Set extra option
        	PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
        	// pass view file
            $pdf = PDF::loadView('pdfview');
            // download pdf
            return $pdf->download('pdfview.pdf');
        return view('pdfview');

Step : 5 Create view


Now we are create one simple blade file in resources/views/pdfview.blade.php file and here we are make very simple html layout for generate pdf file.

<!DOCTYPE html>
	<title>User list - PDF</title>
	<link rel="stylesheet" type="text/css" href="">
<div class="container">
	<a href="{{ route('generate-pdf',['download'=>'pdf']) }}">Download PDF</a>
	<table class="table table-bordered">
			@foreach ($users as $key => $value)
				<td>{{ $value->name }}</td>
				<td>{{ $value->email }}</td>

Note : if you show following error when generate pdf

ErrorException in AdobeFontMetrics.php line 45:
fopen(/var/www/Laravel/LaraDemo/storage/fonts//c47afe5539ba1b2094563d54dce2def7.ufm): failed to open stream: No such file or directory

Solution : please create one empty fonts folder in storage derectory and give it permision 777.

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:


I hope it can help you...

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]