Dompdf, HTML to PDF for PHP5

We’ve recently had a first approach to PDF on-the-fly generation, using a nice and efficient free class for PHP, FPDF.

With this short review we’ll deal with another nice piece of software, designed to translate entire HTML documents into PDF files: dompdf.

To make things short, dompdf is somewhat a rendering engine written in PHP5 (no chance to see it run under PHP4, sorry folks!), and designed to operate on HTML document to be converted procedurally into PDF documents, with a nice level of compliance with the CSS 2.1 rules. It’s a so called style-driven engine: it downloads and reads external style sheets, inline style tags and most of the html’s presentation attributes.

Dompdf can operate through PDFLib if available on your system with a working licence. As alternative, dompdf is bundled with the R&OS CPDF class written by Wayne Munro, a slightly slower but free class with no dependencies on external libraries.

To summarize dompdf’s features, we can read straight from the official documentation and know that:

  • handles most CSS2.1 properties, including @import, @media & @page rules
  • supports most presentational HTML 4.0 attributes
  • supports external stylesheets, either on the local machine or through http/ftp (via fopen-wrappers)
  • supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling, multi-page tables (no nested tables yet however)
  • image support (png, gif & jpeg)
  • no dependencies on external PDF libraries, thanks to the R&OS PDF class
  • inline PHP support.

The last one is a very interesting features for us, because is the key to versatile implementations such as real-time reporting, database-to-pdf charts, and so on. To embed PHP code into the HTML document to be translated, you must enclose the scripts within a script declaration tags, as following:



Dompdf can be used through an usual PHP script, maybe summoned to translate a dinamically generated HTML document, or via command line if you have rights to access your host’ shell.

The PHP script must invoke the dompdf program by passing its parameters via one of the following GET variables:

input_file required a rawurlencoded() path to the HTML file to process. Remote files (http/ftp) are supported if fopen wrappers are enabled.
paper optional the paper size. Defaults to ‘letter’ (unless the default has been changed in dompdf_config.inc.php). See include/pdf_adapter.cls.php, or invoke dompdf.php on the command line with the -l switch for accepted paper sizes.
orientation optional ‘portrait’ or ‘landscape’. Defaults to ‘portrait’.
base_path optional the base path to use when resolving relative links (images or CSS files). Defaults to the directory containing the file being accessed. (This option is useful for pointing dompdf at your CSS files even though the HTML file may be elsewhere).
output_file optional the rawurlencoded() name of the output file. Defaults to ‘dompdf_out.pdf’.
save_file optional If present (i.e. isset($_GET["save_file"]) == true');), output_file is saved locally, Otherwise the file is streamed directly to the client.

Here is a little snippet:

$file_to_pdf = rawurlencode("http://www.mysite.com/docs/$new_html_file");
$destination = "http://www.mysite.com/dompdf/dompdf.php?input_file=" . $file_to_pdf . "&output_file=" . $custom_filename . ".pdf";
header("Location:$destination");

For a quick usage, just the input_file and the save_file parameters must be passed, the first one to locate the actual file to be translated, the second one to set up a right filename, thus avoiding the default which is mostly useless in a customized environment. Don’t forget to apply the rawurlencode() PHP’s function to the paths passed to dompdf as GET variables!

Dompdf distribution is bundled also with many samples you can easily disassemble in order to see how the program works.

If you like this software, as I do, take a minute and drop a line or two to the Author, just to make him know your mind. Perhaps he’ll find a new start in development of this beautiful PDF rendering engine.

This entry was posted in PHP Programming and tagged , , , , , , , , , . Bookmark the permalink.

2 Responses to Dompdf, HTML to PDF for PHP5

  1. zara says:

    it can not run on my project which is online…and when i entered the large html then its not woked.plz help me

  2. zaira says:

    does it really support css? i tried using background in div it isnt working? Do i still need to configure somewhere else? please help..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>