Convert HTML to PDF in PHP with Dompdf Example

By Hardik Savani May 14, 2024 Category : PHP

Hello Friends,

This example is focused on convert html to pdf in php using dompdf. I explained simply step by step php generate pdf from html . This article goes in detailed on simple html to pdf php script. you can understand a concept of html 2 pdf converter php source code tutorial. Alright, let’s dive into the details.

Whenever we work on big PHP application, then we generally require to generate PDF of invoice, data, information, subscription etc. If you use any PHP framework like laravel, codeigniter etc then you have option to other package for generate HTML file into PDF. But if you are working on Code PHP then you fetch problem to how to do it. But in this tutorial i am going to give you very simple example to html to pdf convert using DomPDF library.

DomPDF library through we can simply render html layout into PDF file. DomPDF library through we can use write external stylesheets, inline style tags, font size, font color etc. DomPDF will help to do customize PDF file. Dompdf library is available on composer package so if you are working with composer then also you can use it.

So, today i am going to share with you example of how to generate PDF file from HTML layout using DomPDF library, If you don't know how to implement it then no worry because i am doing from scratch and you can also free download whole script code from here.

In this example i will do main three things like as bellow:

1) Setup dompdf library

2) index.php file

3) pdf_generate.php file

Ok, So you have to just following bellow step and you will get source code of html to pdf using PHP dompdf library. So let's follow bellow step.

Step 1: Installation & Setup

In first step we have to download one library and two dependency of dompdf so follow bellow things.

1) Dompdf: we have to download dompdf library from GitHub, So first let's download from here : Click Here to download dompdf. After download extract it to your root folder and rename it to "dompdf".

2) php-font-lib: Ok, now Download php-font-lib from GitHub, So first let's download from here : Click Here to download php-font-lib. After download extract it to "dompdf/lib/" folder and rename it to "php-font-lib".

3) php-svg-lib: last Download php-svg-lib from GitHub, So first let's download from here : Click Here to download php-svg-lib. After download extract it to "dompdf/lib/" folder and rename it to "php-svg-lib".

Step 2: Create index.php file

In this step i am going to create index.php file in your root directory, in this file i created simply form using bootstrap, this way you have to just feel this form and click to "Generate PDF" button. You will simply download pdf with filled details.

So, let's create index.php file and put bellow code on it.


<!DOCTYPE html>



<title>PHP - Convert HTML to PDF using DomPDF Library</title>

<link rel="stylesheet" href="">



<div class="container">

<h2>Information Form For Generate PDF</h2>

<form action="pdf_generate.php" method="POST">

<div class="form-group">


<input type="text" name="name" class="form-control" placeholder="Enter Name" required>


<div class="form-group">


<input type="email" name="email" class="form-control" placeholder="Enter Email" required>


<div class="form-group">

<label>Website URL:</label>

<input type="url" name="url" class="form-control" placeholder="Enter URL" required>


<div class="form-group">

<label>Say Something:</label>

<textarea name="say" class="form-control" placeholder="Say Something"></textarea>


<div class="form-group">

<button class="btn btn-success">Generate PDF</button>






Step 3: Create pdf_generate.php file

In last step we will create "pdf_generate.php" file, in this file i will get post data and generate pdf file for download. So, let's create "pdf_generate.php" file and put bellow code on it.



/* include autoloader */

require_once 'dompdf/';

/* reference the Dompdf namespace */

use Dompdf\Dompdf;

/* instantiate and use the dompdf class */

$dompdf = new Dompdf();

$html = '<link rel="stylesheet" href="">

<h1>Welcome to</h1>

<table class="table table-bordered">


<th colspan="2">Information Form</th>











<th>Website URL</th>




<th>Say Something</th>





/* Render the HTML as PDF */


/* Output the generated PDF to Browser */



Ok, now you are ready to use and check it. If you want to download source code script then you can download it.

I hope Maybe it can help you...