dompdf: Easy PDF generation with PHP

Posted in: Development

A week and a half ago I started looking into PDF generation with PHP. As my friends pointed out, this can be a somewhat arduous process. Since I was working on a deadline1, I decided the best solution would be to create a nice printable view that I could create PDFs from, then come back to the actual PDF generation if I had time.

Getting the printable view done was pretty easy, and once I had it ready I thought “surely2 someone has to have created an HTML to PDF generator”. A little looking around and I discovered dompdf, an LGPL PHP5 library which does exactly that.

The simple example page worked great, but then I ran into a couple of stumbling blocks early when trying to create a PDF of my printable HTML page:

  1. dompdf doesn’t gracefully handle IMG tags with empty src attributes. I hadn’t gotten around to setting up the images yet, so I just removed the IMG tag and that let me to…
  2. dompdf doesn’t seem to handle TH (table heading) tags properly. I got a DOMText::getAttribute() error that I was unable to find reports of online. Finally I tried removing the TH tags3 and everything worked great.

After that, it was just a matter of tweaking the CSS and HTML until I was happy with it. Total time to get from start to application generated PDFs: 3 days. Day 1: create printable HTML view. Day 2: integrate dompdf and get it all working. Day 3: add the polish of breaking tables at pages, etc.

There are some quirks, but the FAQ and forums have a lot of answers and after a short time digging around you’ll find answers to most of your questions. Here are a couple of tips that should be useful:

  • The page canvas the dompdf uses seems to be 600px wide. I set my BODY tag to width: 560px and padding: 0 20px; and that has worked well.
  • dompdf doesn’t like all CSS shortcuts. In particular, font: 12pt Helvetica; and background: #999; didn’t work as well as explicitly setting the font-family and font-size separately, and setting the background-color.
  • If you want to create page breaks, this is your huckleberry (also in the FAQ, I missed it at first glance).
  • Adding page numbers is pretty easy.

This has been a huge time saver for me, many thanks to Benj Carson and the other contributors to the project.

  1. I needed to be able to generate PDFs of this data by the end of the month. [back]
  2. …and don’t call me Shirley. [back]
  3. I replaced them with TD tags - this HTML is only to print or generate a PDF so validation and semantic HTML goodness take a backseat. Perhaps I’ll see how difficult it is to add TH support and contribute it back (in my copious spare time). [back]

Popularity: 18% [?]

Posted November 25th, 2007 @ 8:41 PM

11 Replies

  1. Geof F. Morris adds this Comment:

    Welcome back to the world of adding value. ;) New shit has come to light!

    Good call on sacrificing the validation and semantics to getting it done. I bet you’ll get around to adding the code back for TH’s, too, because you’re just that kind of itch-scratcher.

    November 25th, 2007 at 9:16 pm

  2. Robert Accettura adds this Comment:

    You can also take a look at FPDF or UFPDF which adds UTF-8 support. Yet another option is TCPDF also based on FPDF.

    Personally I had more success with those since they didn’t have the added funnybusiness of a DOM parser posing as a HTML/CSS parser. HTML/CSS are rather complicated, so there’s a really large margin of error. Not to mention the transformation of that into PDF seemed a bit slow to me.

    Haven’t done PDF in a while though.

    November 25th, 2007 at 10:02 pm

  3. Alex adds this Comment:

    Robert, you’re pointing me to the very thing I was trying to avoid. With dompdf I can create a PDF by running my printable HTML view through it instead of having to code up all the PDF stuff.

    November 25th, 2007 at 10:14 pm

  4. clkao adds this Comment:

    I was solving the very problem a while ago. mozilla2ps came to rescue. it renders with the mozilla engine via xulrunner, so all css / layout are like what you see. the small caveat is that you have to run a Xvfb since it requires an x server but doesn’t actually uses it.

    November 26th, 2007 at 3:52 am

  5. Andreas Gohr adds this Comment:

    Unfortunately all PHP PDF libraries (except the $1000 libpdf) are pretty useless with UTF-8 input data.

    Even though UFPDF and TCPDF promise to handle UTF-8, they do it by just embedding a full TTF font in the PDF. If you have just one language to handle this is fine, but when you need full Unicode range, embedding a 20MB font isn’t feasible.

    The correct answer to this problem would be font subsetting, which would only embed those glyphs which are actually used in the document. But so far no free PHP lib I know of supports this.

    I’d be happy to be proved wrong though.

    November 26th, 2007 at 9:55 am

  6. PHP to PDF | NSLog(); // Erik J. Barzeski adds this Pingback:

    […] Just a link for my own future knowledge. Thanks Alex. […]

    November 26th, 2007 at 11:38 am

  7. Adam adds this Comment:

    I personally use FPDF for PDF creation due to its ease of use. I used to have some problems with the text-wrapping in PDF documents, and FPDF was the one which appeared to have the simplest solution and so I’ve stuck with it ever since!

    November 26th, 2007 at 7:49 pm

  8. Easy PDF generation with PHP(5) « Campai Interactive weblog adds this Pingback:

    December 4th, 2007 at 3:17 pm

  9. VagabondoDigitale adds this Comment:

    Good, i want test this, thx for review.

    December 31st, 2007 at 6:26 am

  10. Ashwin adds this Comment:

    Is there a way you can find out the set of unicode fonts which “cover” a set of given characters? that way, one could only embed these fonts instead of adding the huge large set?

    January 21st, 2008 at 4:31 pm

  11. DeepVoid adds this Comment:

    I’ve just added a few notes about dompdf on my blog:

    http://www.deepvoidlog.com/?p=129

    I think that dompdf is a nice software, perhaps lacking in official support. I had many difficulties in having it showing correctely images in procedurally generated headers, and the official docs are not exhaustive about this matter.

    I recently embedded dompdf into a personal project, and encountered troubles with PHP’s memory limits; there’s nothing about it in the docs, and contacting the author is somewhat an useless job. Is the dompdf’s development suspended?

    June 24th, 2008 at 12:59 am

Add a Comment

Please note: Use of a non-personal web site or blog in the field below and/or comments that are off-topic, personal attacks, or support requests will likely be removed at my discretion.

Note: This post is over 9 months old. You may want to check later in this blog to see if there is new information relevant to your comment.

Introducing Peter Gornell » « Around the web

About This Site

This is the personal web site of Alex King, an independent developer based in Denver, Colorado USA. More...


Crowd Favorite

Crowd Favorite is my software and web development business.

We build web applications, design and develop custom WordPress themes and plugins, and build custom sites using WordPress as a CMS.


I also have a tumblog that aggregates my online content from other services (Twitter, Flickr, del.icio.us. etc.).

I'm voting for Barack Obama

Ads