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 web applications, sometimes you may want to create PDF out of HTML. It can be an invoice, installation guide, resume, etc. In all these cases, you need to convert HTML to PDF using some kind of tool. And if you are able to achieve it through programming it will be much better as it saves your time and work.
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 a PDF from HTML. To generate a PDF, you can either use the raw HTML or the HTML file.
Getting Started
To begin with, you need to install a Dompdf library into the applications. The recommended way of installing the Dompdf package is through a Composer.
composer require dompdf/dompdf
After installing a library, include the Dompdf environment in your project using the code below.
<?php
require_once "vendor/autoload.php";
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
Convert HTML to PDF in PHP
The process of creating PDF from HTML through Dompdf is super easy. Nothing complex in it. All you need to do is pass the HTML to the loadHtml
method.
$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();
Run this code on a browser and you will get the PDF file containing your HTML. You can even give a name to the downloaded file as follows.
$dompdf->stream('site-document.pdf');
The Dompdf provides support for inline style tags and the style attributes of individual HTML elements. Have a look at the examples below which explains using inline style tags and the style attribute.
inline style with Dompdf
As an inline styling, I am applying red color to the H1 tag.
$dompdf->loadHtml('<h1 style="color:red;">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();
style tag with Dompdf
Here, I write the HTML with a style tag and assign HTML to the $html
variable.
$html = '
<style>
h1{
color:yellow;
}
</style>
<h1>hello world</h1>
';
$dompdf->loadHtml($html);
// (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();
Give it a try and you will get the PDF with the provided styling.
Create PDF from HTML File
If you have written the HTML in a separate file, you still can generate a PDF by passing a path of your file. In this case, the file path will be passed to the loadHtmlFile
method.
$dompdf->set_option('enable_remote', TRUE);
$dompdf->loadHtmlFile('URL_OF_HTML_FILE');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
Pass the URL path of your HTML file to the loadHtmlFile
method.
In order to make it work, you need to enable remote file downloading by giving a TRUE value to the enable_remote
.
The user can generate PDF from the HTML file with the loadHtml
method also. It involves 2 steps:
- Load content of HTML file using the
file_get_contents()
function. - Pass this loaded content to the
loadHtml
method.
$html = file_get_contents('test.html'); // here test.html is the file name
$dompdf->loadHtml($html);
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
Save the Generated PDF File
All the 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:
$html = file_get_contents('test.html'); // here test.html is the file name
$dompdf->loadHtml($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 in PHP using a Dompdf library. You may read more about the library on their GitHub page.
Related Articles
- 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.
Very informative and its really working. But somehow PDF size is very high. Any suggestion to reduce it.
Very concise article, which helped me a lot.
Thank you for this contribution.