Home Bootstrap How to convert HTML file to PDF using Dompdf library in PHP?

How to convert HTML file to PDF using Dompdf library in PHP?

by Shahriar Sagor

Today now in this post i will show you How to convert HTML file to PDF using Dompdf library in PHP?When we are working on big PHP application, then we generally need to generate the PDF of invoice, data, information, subscription etc. If we are use any PHP framework as like laravel, codeigniter etc then we have option to do other package for generate HTML file into a PDF. But if we are working on Core PHP then we are fetch a problem to how to do it. But now in this tutorial i am going to give you a very simple example about html to pdf convert by using DomPDF library.

By using DomPDF library we can simply render html layout into the PDF file. For DomPDF library we need to use write external stylesheets, inline style tags, font size, font color. We know DomPDF will help us to do the customize PDF file. We know that Dompdf library is available on the composer package. So if we are working with the composer then also we can use it.

So, today i will going to share with you an example of how we can generate PDF file from the HTML layout by using DomPDF library in php. If you don’t know about how we implement it then no worry because here i am doing this from the scratch and we can also free for download whole script code from here.

Now in this example i will do the main three things as like as bellow:

1) Setup dompdf library

2) index.php file

3) pdf_generate.php file

Ok, So We need to just following the bellow step and we will get the source code of html to pdf by using PHP dompdf library. So let’s see the follow bellow steps.

Step 1: Installation & Setup

At the first step we need to download one library and two dependency for dompdf. So follow the bellow things.

1) Dompdf: we need to download the dompdf library from GitHub, So first let’s download this from here : Click Here to download dompdf. After the download extract it to your root folder and then rename it to “dompdf”.

2) php-font-lib: Ok, now need to Download php-font-lib from the GitHub, So let’s download this from here : Click Here to download php-font-lib. After download and extract it to “dompdf/lib/” folder and then rename it to “php-font-lib”.

3) php-svg-lib: At last Download the php-svg-lib from GitHub, So at first download from the here : Click Here to download php-svg-lib. After the download need to extract it to “dompdf/lib/” folder and then rename it as “php-svg-lib”.

Step 2: Create index.php file

Now in this step we need to create index.php file in our root directory, in this file i will created the simply form by using bootstrap, on this way we need to just feel this form and click to the “Generate PDF” button. we will simply download the pdf with all filled details.

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


<!DOCTYPE html>
	<title>PHP - Convert HTML to PDF using DomPDF Library</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<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

At last step we need to create “pdf_generate.php” file, on this file i will get the post data and then generate pdf file for the download.

So, let’s start and create “pdf_generate.php” file and put the bellow code on it.


/* include autoloader */
require_once 'dompdf/autoload.inc.php';

/* reference the Dompdf namespace */
use Dompdf\Dompdf;

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

$html = '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		<h1>Welcome to ItSolutionStuff.com</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 */

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook


You may also like