Convert HTML to PDF in PHP with Dompdf

PDF is a file format created by Adobe Systems for illustrating text and images in a fixed-layout document. PDF is used to downloading the bunch of data in the web application. 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.

Before begin take a look at the folders and files structure.


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.

Instantiate Dompdf Class

Use the following PHP code to instantiate and use the dompdf class.

// include autoloader
require_once 'dompdf/';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

Basic Use

Just pass the HTML content to dompdf and genarate the PDF.

->loadHtml('<h1>Welcome to</h1>');

// (Optional) Setup the paper size and orientation

// Render the HTML as PDF

// Output the generated PDF to Browser

Advanced Use

The following code will generate PDF from HTML file (pdf-content.html). Also, it will provide the preview or download option.


// (Optional) Setup the paper size and orientation

// Render the HTML as PDF

// Output the generated PDF (1 = download and 0 = preview)


Dompdf has many options for setting the PDF generation. You can see the lists of available options from here – Dompdf\Options


This article provides the simple script to convert HTML to PDF using Dompdf. Dompdf is a very useful library and you can use it in PHP script easily. Dompdf provides many advanced features which help a lot on PDF generation in PHP. Download our source code package to get all the files together.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You


  1. Shelu Nagori Said...
  2. Md Hoq Said...

Leave a reply