The Easy Way to Convert Html to Pdf Using Php

PDF, invented long ago by Adobe has become a standard in electronic documents exchange. When you convert a document to PDF it is supposed to look the same if you were to print it. This is very important to preserve structure in printable forms and important documents. On the down side, PDF are very bad for on screen reading specially on mobile devices. The fixed formatting of PDF documents means that it never fits well on the mobile screen. Either way, PDF are still in demand so it is important to know how to generate one from HTML. I have put together a simple tutorial that will show how you can convert HTML to PDF using PHP in only a few steps.

I remember when I first had to convert HTML to PDF. I took a side job in which I was required to convert some HTML in a website into a PDF document for users to download. I tried searching online for a simple way to this but it was hard to find anything simple on converting HTML to PDF. I found lots of PHP libraries out there, but most of them were outdated and with very little or no documentation at all. After a while looking for a simple solution I found a really good PHP library with good documentation Dompdf.

Dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes. Features handles most CSS 2.1 and a few CSS3 properties, including @import, @media &

@page rules

@page rules supports most presentational HTML 4.0 attributes

supports external stylesheets, either local or through http/ftp (via

fopen-wrappers)

fopen-wrappers) supports complex tables, including row & column spans, separate & collapsed

border models, individual cell styling

border models, individual cell styling image support (gif, png (8, 24 and 32 bit with alpha channel), bmp & jpeg)

no dependencies on external PDF libraries, thanks to the R&OS PDF class

inline PHP support Requirements PHP 5.0+ (5.3+ recommended)

DOM extension

GD extension is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes. For more information visit http://pxd.me/dompdf/www/index.php

This library is very easy to use, and also very easy to install. Using composer you can set it up pretty fast. Once you have the library installed the coding is pretty simple. Take a look at the piece of code bellow:

Converting HTML to PDF with PHP

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?php // somewhere early in your project's loading, require the Composer autoloader // see: http://getcomposer.org/doc/00-intro.md require 'vendor/autoload.php' ; // disable DOMPDF's internal autoloader if you are using Composer define ( 'DOMPDF_ENABLE_AUTOLOAD' , false ) ; // include DOMPDF's default configuration require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php' ; $htmlString = '' ; ob_start ( ) ; include ( 'html_to_dpf.html' ) ; $htmlString .= ob_get_clean ( ) ; $dompdf = new DOMPDF ( ) ; $dompdf -> load_html ( $htmlString ) ; $dompdf -> render ( ) ; $dompdf -> stream ( "sample.pdf" ) ;

The first three:

Disable dompdf’s default auto-loader Include the Composer autoloader Load the dompdf configuration file

What the rest of the code is doing is creating an HTML string to later be converted to PDF. We do this by using ob_start() which allows you to capture all output. Then using ob_get_clean() you can get it and place it inside a variable $htmlString. After that, all that is left to do is send that string to dompdf, and we do this by calling load_html($htmlString) and passing the HTML string as a parameter. Dompdf takes care of the rest. When the conversion is done, a file name sample.pdf will be downloaded. This file will contain the HTML content as a PDF.

Creating the HTML

I deiced to use ob_start() and ob_get_clean() so that I could take the content of a HTML file easily. Otherwise, I would have to place all HTML to be converted to PDF into a single line and assign it to a variable, which will makes it almost impossible to modify. Using ob_start() and ob_get_clean() you can place the content of any HTML into a variable by including the file in between the two functions using include().

HTML to be converted to PDF

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 <!DOCTYPE html> <html> <body> <h3>HTMl to PDF using PHP</h3> <a href="http://webtricksandtreats.com/">http://webtricksandtreats.com/</a> <p>Author:</p> <p>Enmanuel Corvo</p> <hr> <label>Heading:</label> <h1>Lorem ipsum dolor sit amet</h1> <h2>Lorem ipsum dolor sit amet</h2> <h3>Lorem ipsum dolor sit amet</h3> <h4>Lorem ipsum dolor sit amet</h4> <label>Paragraph:</label> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi quis sollicitudin tortor, nec tempor nibh. Sed efficitur diam neque, at fringilla augue varius nec. Proin sit amet accumsan elit. Ut arcu enim, efficitur a purus nec, cursus elementum velit. Praesent sodales dolor ac dui pharetra malesuada.</p> <label>Unordered List:</label> <ul> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> <li>List item 4</li> </ul> <label>Ordered List:</label> <ol> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> <li>List item 4</li> </ol> <label>Simple Table:</label> <table style="width:100%"> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> <tr> <td>Mark</td> <td>Paterson</td> <td>26</td> </tr> <tr> <td>Tyler</td> <td>Durden</td> <td>13</td> </tr> <tr> <td>John</td> <td>Johnson</td> <td>78</td> </tr> </table> <br> <label>Image:</label> <br> <br> <img src="sample.png" /> </body> </html>

As you can see Dompdf provides an easy way to convert HTML to PDF in PHP. Unlike many other HTML to PDF PHP libraries out there dompdf is well documented and also well maintained, two things that are very important if you want to avoid headaches.