Convert html to pdf using dompdf in php - html to pdf converter using dompdf in php

1.Introduction  2.Code and Explanation  3.Download Source Code  4.Comments


This document is about converting an html page with images to a pdf file. You have to provide the content of the html file in this code and the code will convert the html content to a pdf content and either you can download it or you can save or you can do both. Images and css will also be included in the output pdf as it were in the source html page. This html to pdf conversion is done using the code from dompdf. This is very simple and straight forward. Yet you can follow certain instructions for additional functionality which is well explained in the main site dompdf.

Besides, i have just used this code and i did not yet mastered. My purpose was to convert an html page including image and css to a pdf file and this code really worked well. My purpose was to generate a bill of orders with the company logo and the order listing with different colors. dompdf supports certain fonts which you can find in the download section or in the actual file which you can download directly from dompdf.

Code and Explanation

In the current folder(that is the folder where this html file is) i have two subfolders (cppdf and html) and a file named demo.php. cppdf is the folder which has all the files needed to convert html to pdf and the html folder is having sample html file with css and images. so the following code includes all the needed files like i had said above.

Here i give few lines of code which is present in the demo.php. When you download the complete sources you can find the file in the zip file.

			global $_dompdf_show_warnings;
			global $_dompdf_debug;
			$outfile = 'test.pdf';
			$save_file = TRUE; // Save the file or not
			$buff = file_get_contents('html/index.html');			

Hope the above code is self explanatory. We include the main config file and include few globals.
Give a name to the output file and set $save_file to true which will mean that the file will be saved in the current working directory.
And then we read the total content of the html file which may have link attributes to include styles and image tags.

And here is the main picture.

			$dompdf = new DOMPDF();
			$base_path = $_SERVER['DOCUMENT_ROOT'].'/html/';

			if ( isset($base_path) ) {

We create an object dompdf.
And we set the base path.

What is this base path?
The base path parameter is used to resolve relative urls. So, if you have an image tag like so:

<img src="images/pretty.png"/>

and you set the base_path like so:


dompdf will look for the image at /var/www/images/pretty.png on the webserver.
If you don't set the base_path parameter, and you run dompdf from the /var/www/dompdf-0.5 directory, dompdf will look for the image source at /var/www/dompdf-0.5/images/pretty.png, because the default base_path is the directory of the script being executed.

Hope this makes sense
Reference for this paragraph is taken from


				file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));

				if (!headers_sent() )

The first line will save the converted pdf in the current directory and the next like will try to download the file if you have uncommented it like now. Else the file will be only saved in the disk. To make the download dialog appear you need the use the $dompdf->stream function.
Make sure your root folder have enough permission if you want to test the demo which will save a pdf file in that location.

Download Source Code

The zip file contains a demo.php, cppdf folder and html folder. Just extract it to your localhost root folder with any name and execute the demo.php file. You can see that the html in the html folder will be converted to a pdf file and will be saved in the folder where the demo.php file exists and also you will get the download dialog box using which you can save the pdf file where ever you want. I have enabled both the options and it is left to you to use which option you want either the save option or the download option.
Click here to download the html to pdf converter source code zip file

That is all folks. Enjoy.

By -

Comments, Suggestions, Objections, ...