Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

In the era of open source software development platform like GitHub and Bitbucket, developers love to use already built-in open source solutions rather than developing their own.

Recently for a custom Laravel based CRM project (which I am developing for a Fin-Tech startup), I needed the dynamic PDF generation feature for the customer’s leads table so they could easily print the basic lead information on pdf. After googling for sometime, I stumbled upon a very nice DOMPDF Wrapper for Laravel at GitHub, laravel-dompdf.

Here I am going to share how can you easily install that package and generate your PDF with dynamic data and in very nice formatting.

I bet you, its easy !! I have made it very simple and step by step.

Let’s start. I am assuming you already have a Laravel project setup on your machine either its fresh installation or you want to add the dompdf package to your existing Laravel application.

1- Installing Package

If you are familiar with the word “Packages” but are not sure what exactly is that thing? Read what’s Phil Sturgeon a famous programmer says:

A package is a piece of reusable code that can be dropped into any application and be used without any tinkering to add functionality to that code. You don’t need to know what is happening inside, only what the API for the class(es) are so that you can archive your goal

You got it now, Right?

So we don’t have to worry about that package which has some magical files and code inside it but one thing is for sure it will solve our problem quicker than you might think.

FYI, there is a huge packages database at https://packagist.org/ which you can browse and use.

To install a package you need to first have Composer on your machine. A Composer is a package manager which is used to download and install packages to our projects.
Its not difficult, just download file from https://getcomposer.org/download/ and install it.

Open the Command interface, I am using Windows, So press WinKey+R and in run dialogue box, type cmd, hit Enter.

By default we are into User directory, Let’s move to where our project is installed.

Type cd.. and hit Enter. 
Repeat this until you come to your main drive in my case C:/

Now let’s move to our project directory, mine is projectone where I want to install the package.

Paste the following command here:

composer require barryvdh/laravel-dompdf
Package will start downloading.

Wait for the installation until you see the cursor start blinking again at the end of the command interface.

2- Update config/app.php

Now open config/app.php file.

Add the following line to ‘providers’ array:

'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],

Now add this line to ‘aliases’:

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

Done? You are just doing great. Let me clap for you :)

3- Update routes/web.php

Set the route:

Route::get('/customer/print-pdf', [ 'as' => 'customer.printpdf, 'uses' => 'CustomerController@printPDF']);

In above, you see I have a function in PrintPDF in Customer controller which will handle the PDF generation. Now add that route to any anchor or button you want to use for PDF generation.

<a href="{{route('customer.printpdf')}}">Print PDF</a>

4- Create Controller

Create new controller in (app\Http\Controllers\) and paste the following code.

<?php
// Our Controller
namespace App\Http\Controllers;

use Illuminate\Http\Request;
// This is important to add here. 
use PDF;

class CustomerController extends Controller
{
public function printPDF()
{
// This $data array will be passed to our PDF blade
       $data = [
          'title' => 'First PDF for Medium',
          'heading' => 'Hello from 99Points.info',
          'content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.'        
];

$pdf = PDF::loadView('pdf_view', $data);
return $pdf->download('medium.pdf');
}
}

You can add the above function printPDF() into any of your existing controller or you can create new one with any other name (I am using Customer controller as example in this post). But make sure if you have different controller name then you should update above route lines (Step 3) accordingly.

5- Create Blade

A blade view is something which will be rendered to our final PDF.

Create a view pdf_view.blade.php in \resources\views\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{ $title }}</title>
</head>
<body>
<h1>{{ $heading}}</h1>
<div>
<p>{{$content}}</p>
</div>
</body>
</body>
</html>

That’s it. You are done. Now hit the button to generate your PDF.

Extra Tip:

To apply formatting on a PDF you need to add inline css to the elements and to make the elements fit to the PDF use HTML tables instead Div, Span or other markups tags.

Don’t forget to give table 100% width if you want to create PDF with edge to edge layout.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{ $title }}</title>
</head>
<body>
<h1>{{ $heading}}</h1>

<table width="100%" style="width:100%" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</body>
</html>

I hope this example has made your work easier. I really enjoyed writing my almost first post here on Medium. You can follow my articles over my blog 99Points.info or on add me on LinkedIn.

If you too enjoyed reading this, then its your turn to clap for me :)

Cheers.