Convert HTML to PDF using PHP Dompdf Library
In this tutorial, we will learn about how to convert HTML to PDF using PHP Dompdf Library.
PDF (Portable Document Format) is a very popular file format which retains file layout format as intended. It contains both text and images in it.
PDF file format is independent of hardware, application software, and operating systems. This makes it the best choice to keep and share file over the internet.
What is DOMPDF?
Dompdf is a PHP library which enables to convert HTML to PDF file format and it is the easiest way to generate a PDF file using PHP.
We will share the simple and easy examples to implement PDF generation from HTML document using PHP Dompdf library.
It required minimum PHP version 7.1 or higher to use Dompdf and convert HTML to PDF document.
How to Install Dompdf in Application?
You can easily install Dompdf via composer, github or you can simply download this tutorial code. Dompdf library code is available in download file of this tutorial.
Install Dompdf using Composer
Go to your application directory and run the below command either in terminal or command prompt to install Dompdf via composer.
composer require dompdf/dompdf
This will install Dompdf library in your application directory. Make sure that you already have installed composer in your system.
If you do not have composer in your system, you can easily install composer on windows.
Convert HTML to PDF with Basic Example
// The Composer autoloader
require_once 'vendor/autoload.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// Load HTML content to generate a PDF
$dompdf->loadHtml('<h1 style="color:blue;">AllPHPTricks.com</h1>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Download the generated PDF
$dompdf->stream();
The above code will generate a PDF file from HTML code using Dompdf library / package.
This will download the file with default name as a document.pdf. However, you can easily define the PDF file name as a first argument in stream() method.
You can also pass the array as the second argument to your stream() method to define the file display as preview or download.
Generate PDF with Custom Name & Preview/Download PDF
// Define the name of a PDF file &
// Preview/Download the generated PDF file
// (0 = Preview on Browser & 1 = Download PDF)
$dompdf->stream("All PHP Tricks", array("Attachment" => 0));
Load HTML File Content into Dompdf
You can also load the HTML file content to your Dompdf to generate a PDF from HTML document.
Create a file with any name such as html-to-pdf.html and write any HTML code in it such as headings and paragraph in it.
First load HTML file content via file_get_contents() function then pass it to Dompdf loadHtml() method.
// Load PDF content from an HTML file
$html_file = file_get_contents("html-to-pdf.html");
$dompdf->loadHtml($html_file);
Convert HTML to PDF & Save PDF File in Directory
Sometimes, you just need to generate a PDF file, so you can simply convert HTML to PDF using Dompdf library and save that PDF file on your server, so that you can use it later for any purpose.
It is not necessary that you always want to preview or download the PDF file after creating, so you can use the below code to do it.
// The Composer autoloader
require_once 'vendor/autoload.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// Load HTML content to generate a PDF
$dompdf->loadHtml('<h1 style="color:blue;">AllPHPTricks.com</h1>');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Returns the PDF file as a string.
$pdf_string = $dompdf->output();
// PDF file name and location to store file
$pdf_file_loc = 'files/allphptricks.pdf';
// Save generated PDF to the desired location with custom name
file_put_contents($pdf_file_loc, $pdf_string);
Attached Generated PDF File to Email
You may also sometimes needs to attach the generated PDF file to your email. For example if you are generating a payment receipt in this case, you may need to send a generated PDF to customer via email.
You will need to attached the generated PDF file to email which you created by converting HTML to PDF via Dompdf.
You can simply pass the above created PDF file URL to your PHP Mailer. Like below:
$mail->addAttachment($pdf_file_loc);
Readers Also Read: Send Email in PHP Using PHPMailer
Dompdf Methods with Explanation
PHP Dompdf library provides different methods and options to customize the PDF file as per your need.
We will share usage of some important methods that will help you to generate your desired customized PDF file from HTML.
1. loadHtml() – Required
This method loads HTML content to generate PDF.
2. render() – Required
The render() simply render the HTML to PDF, it is required.
3. stream() – Required
Use to preview and download the generated PDF.
To define custom name, pass first argument as a string without .pdf.
Accept second argument as an array, you can use either Attachment or compress and pass the following values.
Attachment => 1=Download PDF, 0=Preview PDF.
compress => 1 = Enable content stream compression, 0 = Disable content stream compression.
4. setPaper() – Optional
This method is optional which accepts two parameters to set paper size and orientation.
You can pass the first argument to set the paper size such as A4, letter, legal, Tabloid and more.
And to set the orientation, pass the second argument either landscape or portrait.
5. output() – Optional
You can use the output() method to return the PDF file as a string.
6. setBasePath() – Optional
Use to set the base path to includes external images and stylesheets to generate customized PDF.
Conclusion
I hope by now you learnt how to convert HTML to PDF using PHP Dompdf Library. We have tried to show different examples which will help you to achieve your goal to generate a PDF using PHP.
If you found this tutorial helpful, share it with your friends and developers group.
I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook, Twitter and share it.
Facebook Official Page: All PHP Tricks
Twitter Official Page: All PHP Tricks
Hi Javed. Thanks for your great tutorials.
If I want to convert HTML to PDF on my published Website, do I have to install Dompdf on the server?
Dear Geoffrey,
Yes you will need to either install it via Composer or copy paste the files I provided in Download.