Generate awesome PDF files with Laravel

Andrea Pollastri
3 min readJun 7, 2023

If you are working on a Website or on Software, maybe you could need to generate PDF documents. In this quick tutorial, I will show you a simple way to create awesome PDF files with Laravel and DOMpdf.

The first step is to install this package:

composer require barryvdh/laravel-dompdf

Then we have to create a PDF layout such as:

<!-- /resources/views/componenents/pdfLayout.blade.php -->

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
@page {
margin: 120px 30px;
}
header {
position: fixed;
top: -60px;
left: 0px;
right: 0px;
height: 50px;
color: gray;
text-align: center;
}
footer {
position: fixed;
bottom: -60px;
left: 0px;
right: 0px;
height: 50px;
color: gray;
text-align: center;
line-height: 10px;
font-size: 10px;
font-family: Arial, Helvetica, sans-serif;
}
main {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
}
table {
font-size: 12px;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
.page-break {
page-break-after: always;
}
</style>
</head>
<body>
<header>
<img src="https://site.example/images/logo.png" alt="logo" height="50">
</header>
<main>
{{ $slot }}
</main>
<footer>
<hr>
This is the footer!
</footer>
</body>
</html>

Now we can create the PDF content (e.g. with a table and page breaks):

<!-- /resources/views/pdf/document.blade.php -->

<x-pdfLayout>

<h2>Page number One</h2>
<p>Hello <b>{{ $name }}</b>,<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a porta dui. In facilisis nulla eget mauris faucibus, eu feugiat quam lobortis. Duis at lorem posuere, hendrerit augue vel, gravida sapien. Donec eu faucibus massa. Vestibulum ac leo sed libero scelerisque sollicitudin nec vitae dolor. Suspendisse accumsan ante risus, sit amet tempor mauris volutpat nec. Proin varius placerat nisi vitae viverra. Ut quam massa, congue vel auctor a, fringilla nec orci. Duis viverra sem malesuada pulvinar volutpat. Mauris egestas sodales quam ut fermentum. Pellentesque porttitor mauris leo, sed mattis purus fringilla ac. Etiam non faucibus erat.</p>
<div class="page-break"></div>
<h2>Page Number Two</h2>
<p>Etiam quis odio non ex fermentum rutrum sit amet non orci. Morbi non metus et leo viverra convallis. Integer faucibus non felis id egestas. Aliquam erat volutpat. Phasellus gravida augue vitae rutrum pellentesque. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi ligula nibh, vehicula et nisl non, interdum vehicula ipsum. Nullam posuere lectus sed lorem luctus, sed rutrum neque iaculis. Sed a tempor est, at vestibulum dolor. Proin id nunc lectus. Aenean sollicitudin congue nisi, quis varius ipsum varius ac. Fusce eget eros ac erat tempor sollicitudin sed eget ligula. Fusce porta vel erat nec iaculis.</p>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>

</x-pdfLayout>

The last step is to insert this snippet into a Route or Controller Method:

$info = [
'name' => 'John Doe',
'email' => 'john.doe@gmail.com'
];

$pdf = Pdf::loadView('pdf.document', $info);
return $pdf->download('document_'.uniqid().'.pdf');

Discover more about the package features visiting:
https://github.com/barryvdh/laravel-dompdf

That’s all! Now it’s your turn! :)

--

--

Andrea Pollastri

I'm a Web Artisan, Open Source supporter, and OWASP member based in Milan, Italy.