PDF(Portable Document Format) is a file format that includes text formatting and images in a fixed-layout. The PDF is independent of application software, hardware, and operating system. That means you can use the same PDF on any operating system.
While working on PHP applications, we sometimes want to create PDF out of HTML. It may be an invoice, installation guide, resume, etc. In order to make it dynamic, we need to generate PDF using HTML on the fly.
In this article, I show you how to convert HTML to PDF in PHP using the Dompdf library.
Dompdf is an open-source HTML to PDF converter library. Using Dompdf, you can easily create PDF from raw HTML or using HTML file.
For getting started, you first need to install the Dompdf library to your application. I recommend using Composer for the installation of the library.
Open the terminal in your project root directory and run the command:
composer require dompdf/dompdf
After installing a library, you can include the Dompdf environment to your project using the code below.
<?php require_once "vendor/autoload.php"; use Dompdf\Dompdf;
Convert HTML to PDF in PHP
The code requires to generate PDF is simple and straight-forward. As an example, let’s say you have a raw HTML which should be converted in PDF. Create a
index.php file and add write the code as follow.
<?php require_once "vendor/autoload.php"; use Dompdf\Dompdf; // instantiate and use the dompdf class $dompdf = new Dompdf(); $dompdf->loadHtml('<h1>hello world</h1>'); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream();
Here, we are passing a raw HTML to the method
loadHtml(). When we run this file it will generate PDF and render on a browser.
We can also give a name to the PDF which is going to download.
You learned about generating PDF from raw HTML. Now, let’s see how to create PDF using HTML file.
There are 2 ways for it. In the first way, we get the content from HTML file and pass it to the
loadHtml() method. Another way is using
loadHtmlFile() function and pass the path of HTML file to it.
Let’s see both ways one by one.
$html = file_get_contents('test.html'); $dompdf->loadHtml($html); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream();
In this method, you should first set the ‘chroot’ of directory where your HTML file is located. If your HTML file is in the root directory then directly use the
getcwd() method. You should adjust the path as per your requirement.
$dompdf->set_option('chroot', getcwd()); //assuming HTML file is in the root folder $dompdf->loadHtmlFile('test.html'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream();
Save the Generated PDF File
All above codes we have written will render the generated PDF in a browser. If someone wants to save generated PDF to a disk then use the code as follows:
$dompdf->set_option('chroot', getcwd()); //assuming HTML file is in the root folder $dompdf->loadHtmlFile('test.html'); // Render the HTML as PDF $dompdf->render(); //Save to disk $output = $dompdf->output(); file_put_contents("test.pdf", $output);
It’s all about converting HTML to PDF using the Dompdf library. I would like to hear your thoughts and suggestions in the comment section below.
- Using Google reCAPTCHA On Your Website Forms With PHP
- Upload Files To Amazon S3 Using AWS PHP SDK
- Upload Image Using Bulletproof Library In PHP
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.