Jeb Wilkins

Coding and climbing in Cumbria.

Converting HTML to PDF With PHP

php’s got a nice simple pdf generation library called fpdf, this works well and has no external dependancies but its still a faff to have to write code to generate and format pdfs, particularly if you’ve already got the content in HTML, complete with formatting. A while back someone (not sure who) forked off fpdf and hacked in HTML parsing support to produce html2fpdf ( So now instead of writing

$pdf->write(‘foo bar’); you can write $pdf->writeHTML(‘




This is great but it has a few bugs and limitations, its also based on an older copy of fpdf. Diffing between the hacked version of fpdf.php and the original 1.52 version it seems theres been a lot of spurious whitespace and code formatting changes, in addition to those actually needed. Initially I attempted to hand edit the diff to reduce unnecessary changes, but gave up after an hour of swearing. Enter the wonders of Meld, which in half an hour allowed me to just merge in only those changes that were needed, reducing the patch from about 1700 lines to about 900. Next up I was able to to a 3 way merge with meld between the latest version of fpdf, the original, the hacked htmlised copy. Net result was a new fpdf file which should hopefully fix some of the bugs and will work from command line scripts as well web scripts (feature of newer mainline releases of fpdf). There seems to be a lot of patches on the sf site which aren’t integrated so hopefully over the coming weeks I’ll have a look at integrating some of those for things like better handling of css.