A tutorial on how to use the laravel-dompdf package in a Laravel project to create a PDF file from HTML code or a view (Blade template).
The French version of this tutorial : Laravel : Générer un fichier PDF avec laravel-dompdf
Introduction to laravel-dompdf
The laravel-dompdf package is a Laravel wrapper for the Dompdf library which allows to convert HTML code into PDF (Portable Document Format) files.
Laravel-dompdf generates PDF files with support for the following features:
- Most CSS 2.1 and CSS 3 properties, internal and external style sheets
- Most HTML 5 attributes
- Provides support for images (GIF, PNG, JPG, etc.) and SVG
- Tables (rows, columns, table borders, cell styles, etc.)
- Fonts
- ...
In this guide, we want to see how to install, configure and use the laravel-dompdf package in a Laravel project. At the time of writing this guide, I am using version 9.42.2 of Laravel.
Install and configure laravel-dompdf
To download (1), integrate (2) and configure (3) the laravel-dompdf package in your Laravel project, we will proceed as follows:
1. Download laravel-dompdf
To import laravel-dompdf and its dependencies, open the console (command prompt) at the root of your project and run the following composer command:
composer require barryvdh/laravel-dompdf
2. Integrate laravel-dompdf
Once laravel-dompdf is imported, you can load it into your application by adding the service provider to the $providers
array and the facade ("PDF" for short) to the $aliases
array in your config/app.php
file :
'providers' => [
// Service Provider DomPDF
Barryvdh\DomPDF\ServiceProvider::class
],
'aliases' => Facade::defaultAliases()->merge([
// Facade Laravel-dompdf
"PDF" => Barryvdh\DomPDF\Facade::class
])->toArray(),
3. Configure laravel-dompdf
To create (copy) the configuration file config/dompdf.php
in which you can modify the default options of dompdf , run the following artisan
command:
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
We have the following options:
-
orientation
(string) : the layout in portrait ("portrait") or landscape ("landscape") -
default_paper_size
(string) : the paper size ("a3", "a4", ...) -
dpi
(int): "Dots per Inch", the number of dots or pixels that will be rendered in one inch on the interface -
default_font
(string) : The default font to use ("sans-serif", "courier", "helvetica", "dejavu sans", ...) - ...
You can also change these options in the code before generating the PDF file using the setOptions()
method :
PDF::setOptions([
"defaultFont" => "Courier",
"defaultPaperSize" => "a4",
"dpi" => 130
]);
Using laravel-dompdf
After installing and configuring laravel-dompdf , you can create a new Barryvdh\DomPDF\PDF
instance and specify the contents of the PDF file as follows:
1. A view (Blade template) with the method loadView($view, $data)
:
use PDF;
use App\Models\Post;
public function getPostPdf (Post $post)
{
// The PDF instance with the view resources/views/posts/show.blade.php
$pdf = PDF::loadView('posts.show', compact('post'));
}
2. A path to a file with the method loadFile($path)
:
$pdf = PDF::loadFile(public_path("documents/file.html"));
3. An HTML string with the method loadHTML($html)
:
$pdf = PDF::loadHTML("<p>HTML content here</p>");
From the Barryvdh\DomPDF\PDF
instance, you can use the method download($file)
to force the browser to start downloading the PDF file where $file
represents the name and extension of the PDF file :
use PDF;
use App\Models\Post;
public function getPostPdf (Post $post)
{
// The PDF instance with the view resources/views/posts/show.blade.php
$pdf = PDF::loadView('posts.show', compact('post'));
// Start downloading the PDF file
return $pdf->download(\Str::slug($post->title).".pdf");
}
Or use the stream()
method to display the PDF file in a browser :
return $pdf->stream();
Or use the save($path)
method to save the PDF file to $path
:
$pdf->save(public_path("storage/documents/file.pdf"));
Tips
1. You can combine methods:
return PDF::loadView('posts.show', compact('post'))
->setPaper('a4', 'landscape')
->setWarnings(false)
->save(public_path("storage/documents/file.pdf"))
->stream();
2. Add a meta tag to your template to support UTF-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
3. To create a page break, you can use the page-break-before
or page-break-after
CSS properties :
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My page</title>
</head>
<body>
<h1>Title of page 1</h1>
<div>Contents of page 1</div>
<!-- Page break -->
<div style="page-break-after: always;" ></div>
<h1>Title of page 2</h1>
<div>Contents of page 2</div>
</body>
</html>
Be well! đŸ˜‰