Laravel 5.8 - How To Generate HTML To PDF With Laravel domPDF
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.
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]
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
Leslie Bravo
[email protected]
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
Leslie Bravo
[email protected]
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
'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:
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.
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
$data = [
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
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
</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.
<td> </td>
<td> </td>
</tr> Inicia sesión en medium.com con
<tr> Google
<td> </td>
<td> </td>
<td> </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.
https://medium.com/@zeeshan_rasool/laravel-5-8-how-to-generate-html-to-pdf-with-laravel-dompdf-676f021bf29f 8/8