Laravel 5 Convert HTML To PDF Using Dompdf Library

Laravel 5 Convert HTML To PDF Using Dompdf Library

By : Laravellogic Created : October 14, 2017 View : 1363

In this post, we will tell you how to create pdf file from HTML view in Laravel web aplication by using barryvdh/laravel-dompdf package. The barrevdh/laravel-dompdf package is a open sorce and you can easily set the beautiful layout for pdf file. so this is a packahe very easy way to generate and download PDF file in laravel 5.5 web application.

Generally, a PDF is better than an HTML stylesheet, because it is useful for saving large data and sending email in your laravel wen application.

Dompdf Library doesn't require anything else as its codebase. only install this package in your website and Follow the steps below.

Steo 1 Install Dompdf Package

To use Dompdf Library in Laravel, we are installed to the barryvdh/laravel-dompdf Library in our web application. You can install this Library to your web application adding the following command in your terminal.

composer require barryvdh/laravel-dompdf

Step 2: Configuration

After install, Go to this path config/app.php and set following value for providers array and aliases array

'providers' => [


'aliases' => [

      'PDF' => Barryvdh\DomPDF\Facade::class,


After configure app.php file then Go to bootstrap/app.php and open app.php file than add following below line.


After add above line then You can publish the config dompdf.php file so using command below:

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

Than Go to this link config/dompdf.php and defaults configuration settings are set

Step 3: Create route

Go to routes/web.php file than open web.php file and add below route


Step 4: Create controller

Go To app/Http/Controller/ create BlogController.php file and add below code

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use PDF;

class BlogController extends Controller

	public function PDF(Request $request)
    	$data = DB::table("blogs")->get();

        	// Set more option
        	PDF::setOptions(['defaultFont' => 'sans-serif','dpi' => 100]);

        	// pass view file
            $pdf = PDF::loadView('pdfshow');

            // download pdf file
            return $pdf->download('phperrorcode.pdf');

        return view('pdfshow');

Step : 5 Create view file

Now we are Go to this path resources/views/ than create a pdfshow.blade.php file and here we are generate pdf file layout, so like below layout

	<title>HTML to PDF Using Dompdf</title>
	<link rel="stylesheet" type="text/css" href="">
<div class="container">
	<a href="{{ route('pdfDownload',['download'=>'pdf']) }}">Download PDF</a>
	<table class="table table-bordered">
			@foreach ($blog as $key => $value)
				<td>{{ $key + 1 }}</td>
				<td>{{ $value->title }}</td>
				<td>{{ $value->description }}</td>

If you get a bug like the image below img

html to pdf In Laravel 5.5 using Dompdf error Solution
Solution : go to /storage/ derectory and if not faund fonts derectory, so please create one empty fonts folder ane set the permision-777.

Run :

now you can open terminal, after add follow below command

php artisan serve

Now you can go to url below.


I hope you have found this post useful,