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

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.

index.php

<!DOCTYPE html>
<html>
<head>
	<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">
</head>
<body>


<div class="container">
	<h2>Information Form For Generate PDF</h2>
	<form action="pdf_generate.php" method="POST">
		<div class="form-group">
			<label>Name:</label>
			<input type="text" name="name" class="form-control" placeholder="Enter Name" required>
		</div>
		<div class="form-group">
			<label>Email:</label>
			<input type="email" name="email" class="form-control" placeholder="Enter Email" required>
		</div>
		<div class="form-group">
			<label>Website URL:</label>
			<input type="url" name="url" class="form-control" placeholder="Enter URL" required>
		</div>
		<div class="form-group">
			<label>Say Something:</label>
			<textarea name="say" class="form-control" placeholder="Say Something"></textarea>
		</div>
		<div class="form-group">
			<button class="btn btn-success">Generate PDF</button>
		</div>
	</form>
</div>


</body>
</html>
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.

pdf_generate.php

<?php
/* 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">
			<tr>
				<th colspan="2">Information Form</th>
			</tr>
			<tr>
				<th>Name</th>
				<td>'.$_POST['name'].'</td>
			</tr>
			<tr>
				<th>Email</th>
				<td>'.$_POST['email'].'</td>
			</tr>
			<tr>
				<th>Website URL</th>
				<td>'.$_POST['url'].'</td>
			</tr>
			<tr>
				<th>Say Something</th>
				<td>'.nl2br($_POST['say']).'</td>
			</tr>
		</table>';


$dompdf->loadHtml($html);


/* Render the HTML as PDF */
$dompdf->render();


/* Output the generated PDF to Browser */
$dompdf->stream();
?>

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

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.