Sometimes we need to convert a html file into a downlodable or readable format for use. So today PHP Hurdles is here with a tutorial where we will Convert HTML to Pdf in PHP Using DOMpdf.At the time of download web page content as PDF file, we needed to convert HTML to PDF. This tutorial shows the easiest way for convert HTML to PDF using PHP.
Dompdf is a PHP library that used to convert HTML to PDF file. Using Dompdf library files you can easily implement the generate PDF feature in the web application. Here we’ll provide the simple script to Convert HTML to PDF in PHP with Dompdf.
So lets start with the tutorial
Installation & Setup
Download stable release of dompdf from GitHub . Extract the downloaded dompdf package and rename it with “dompdf”.
Download php-font-lib from GitHub, extract it and place the entire folder into the dompdf/lib/ directory.
Download php-svg-lib from GitHub, extract it and place the entire folder into the dompdf/lib/ directory.
Now Create a index.html file which will have a convert to pdf link and our content to be converted into pdf
Now just pass the index.html to dompdf in pdf.php and generate pdf of index.html
// (Optional) Setup the paper size and orientation
// Render the HTML as PDF
// Output the generated PDF (1 = download and 0 = preview)
This is all we have to do to convert HTML to pdf using PHP with DOMpdf.
For demo Download Here
If you have any query about this tutorial and scripts, feel free to comment us.
Hardwork doesn’t guarentee success but improves its chances so keep working hard
because Hurdles aren’t really hurdles