Converting an HTML to PDF is mainly required to download a bunch of data or text content via the web application. In this tutorial, we bring an example to convert HTML to PDF with the Dompdf library. Dompdf is a useful library that generates PDF from the HTML document in PHP. This provides a simple way to convert HTML to PDF files. We have published another post that converts HTML to PDF using fpdf.
Steps to Convert HTML to PDF using Dompdf:
- Configure Dompdf library
- Create a basic HTML code
- Use Dompdf class to convert HTML to PDF
Let’s start the process to write a PHP code that converts HTML to PDF using Dompdf.
Step 1: Configure Dompdf Library:
First, download the latest dompdf library release from Github and extract it on your directory where you want to use this functionality.
Configure Dompdf Class:
Now, use the below code to your PHP file to instantiate the Dompdf class.
1 2 3 4 5 6 7 |
namespace Dompdf; // Include autoloader require_once 'dompdf/autoload.inc.php'; // Instantiate and use the dompdf class $dompdf = new Dompdf(); |
Step 2: Create a basic HTML code
Now create a simple HTML code, which we use to convert into a PDF file. This is just a demo HTML code to show an example. You can use any HTML file to render a PDF document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html> <head> <title>Convert HTML to PDF</title> <style> body { background-color: lightblue; } .container { width: 90%; max-width: 600px; margin: 0 auto; } </style> </head> <body> <div class="container"> <h1>About CodeFixUp.com</h1> <p>CodeFixUp has been started by the author with a purpose to share his knowledge to outer world and thus helping others to find API solutions without messing with the code complexities. The site is enriched with exclusive list of various popular API implementation details along with the code and self descriptive steps, users have to follow to help its integration for their projects.</p> <h3>We do accept paid work. Write to us to start working on your dream project or just say hi!</h3> <p>Contact us at: <strong>Codefixup@gmail.com</strong></p> <div align="center"> <a href="pdf-demo.php" target="_blank" style="background-color: #fff;padding: 10px;font-size: 20px;font-weight: 600;">Click Here to Convert Html To PDF</a> </div> </div> </body> </html> |
Step 3: Use Dompdf class to convert HTML to PDF
Now, we do some basic configuration to generate PDF using Dompdf class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Load HTML content $dompdf->loadHtml($htmldata); // (Optional) Setup the paper size and orientation $customPaper = array(0,0,900,1330); $dompdf->set_paper($customPaper); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream("",array("Attachment" => false)); exit(0); |
Some Useful Dompdf Methods
Some useful dompdf methods given below to enhance your HTML to PDF integration functionality.
loadHtml(): This will load your HTML content as string.
loadHtmlFile(): This is use to load HTML content as a file.
output(): Returns the PDF as a string.
render(): Renders the HTML to PDF.
setBasePath(): Sets the base path to include external stylesheets and images.
setPaper(): use to define paper size & orientation.
stream(): Streams the PDF to the client.