0% found this document useful (0 votes)
98 views

Laravel 5.8 - How To Generate HTML To PDF With Laravel domPDF

The document discusses how to generate PDFs from HTML in a Laravel application using the laravel-dompdf package. It provides step-by-step instructions for installing the laravel-dompdf package via Composer, updating the application's configuration files, and setting up a route to trigger PDF generation from a controller method. The method will use the laravel-dompdf package to convert HTML into a PDF that can be printed or downloaded.

Uploaded by

Leslie Bravo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Laravel 5.8 - How To Generate HTML To PDF With Laravel domPDF

The document discusses how to generate PDFs from HTML in a Laravel application using the laravel-dompdf package. It provides step-by-step instructions for installing the laravel-dompdf package via Composer, updating the application's configuration files, and setting up a route to trigger PDF generation from a controller method. The method will use the laravel-dompdf package to convert HTML into a PDF that can be printed or downloaded.

Uploaded by

Leslie Bravo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

23/6/2020 Laravel 5.

8 — How to Generate HTML to PDF with Laravel domPDF

Inicia sesión en medium.com con


Google

Laravel 5.8 — How to Generate HTML to PDF with


Leslie Bravo
Laravel domPDF [email protected]

Continuar como Leslie


Zeeshan Rasool Follow
Apr 13, 2019 · 5 min read Para crear tu cuenta, Google compartirá tu nombre,
tu dirección de correo electrónico y tu foto de perfil
con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.

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.
https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 1/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

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

L
Inicia sesión en medium.com con
et’s start. I am assuming you already have a Laravel project setup on your machine
Google
either its fresh installation or you want to add the dompdf package to your existing
Laravel application. Leslie Bravo
[email protected]

1- Installing Package Continuar como Leslie

If you are familiar with the word “Packages” but


Para aretunot
crear sureGoogle
cuenta, what exactly is that
compartirá thing?
tu nombre,
tu dirección
Read what’s Phil Sturgeon a famous programmer de correo electrónico y tu foto de perfil
says:
con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.
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.

https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 2/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

Inicia sesión en medium.com con


Google

Leslie Bravo
[email protected]

Continuar como Leslie


By default we are into User directory, Let’s move to where our project is installed.
Para crear tu cuenta, Google compartirá tu nombre,
tu dirección de correo electrónico y tu foto de perfil
con medium.com. Consulta la política de privacidad
Type cd.. and hit Enter. y los términos del servicio de medium.com.
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.

https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 3/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

Inicia sesión en medium.com con


Google

Leslie Bravo
[email protected]

Continuar como Leslie

Para crear tu cuenta, Google compartirá tu nombre,


Paste the following command here: tu dirección de correo electrónico y tu foto de perfil
con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.

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.

https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 4/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

2- Update config/app.php
Now open config/app.php file. Inicia sesión en medium.com con
Google

Add the following line to ‘providers’ array:


Leslie Bravo
[email protected]

'providers' => [ Continuar como Leslie


....
Barryvdh\DomPDF\ServiceProvider::class,
Para crear tu cuenta, Google compartirá tu nombre,
],
tu dirección de correo electrónico y tu foto de perfil
con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.
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>

https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 5/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

4- Create Controller
Create new controller in (app\Http\Controllers\) and
Inicia paste
sesión enthe followingcon
medium.com code.
Google

<?php Leslie Bravo


// Our Controller [email protected]
namespace App\Http\Controllers;
Continuar como Leslie
use Illuminate\Http\Request;
Para crear tu cuenta, Google compartirá tu nombre,
// This is important to add here.tu dirección de correo electrónico y tu foto de perfil
use PDF; con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.
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
https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 6/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

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


Inicia sesión en medium.com con
Create a view pdf_view.blade.php in \resources\views\
Google

Leslie Bravo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
[email protected]
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> Continuar como Leslie

<head> Para crear tu cuenta, Google compartirá tu nombre,


<title>{{ $title }}</title> tu dirección de correo electrónico y tu foto de perfil
</head>
con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.
<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>
https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 7/8
23/6/2020 Laravel 5.8 — How to Generate HTML to PDF with Laravel domPDF

<td>&nbsp;</td>
<td>&nbsp;</td>
</tr> Inicia sesión en medium.com con
<tr> Google
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td> Leslie Bravo
</tr> [email protected]
</table>
Continuar como Leslie
</body>
Para crear tu cuenta, Google compartirá tu nombre,
</body> tu dirección de correo electrónico y tu foto de perfil
</html> con medium.com. Consulta la política de privacidad
y los términos del servicio de medium.com.

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.

Laravel Dompdf Composer PHP Development

About Help Legal

Get the Medium app

https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 8/8

You might also like