Peter Nguyen Team : Web Development Tags : Technology Web Development Issues Featured

Creating that perfect PDF: Good PDF generators

Peter Nguyen Team : Web Development Tags : Technology Web Development Issues Featured

You want to turn your pretty web design into PDF?

Here is the most important thing to know:

PDF Generation is a pain.

From repeatable headers and footers, to persistent table headers – implementing some form of magically generated PDF isn’t as easy as it sounds.

I recently had to implement the new estimate design for Wiliam, and it allowed me to explore some of the options available in the market. I tried quite a few “HTML to PDF” converters, without much success.

So here’s what I’ve tried:

 

1. Wkhtmltopdf

Wkhtmltopdf is an open source HTML to PDF converter, powered by the QT WebKit rendering engine (the very same one that powers Chrome). Whilst it would have been nice to work on this, unfortunately it lacks proper support for repeatable tables.

Also, whilst working on several homebrew C# ports (Here and Here) I discovered that it doesn’t really handle multi-threading well… well it doesn’t handle multi-threading at all, which is quite difficult as ASP.net is multi-threaded.

 

2. ABCPDF

ABCPDF is a commercially available product, and whilst that itself does have some perks (e.g. support) I found it was fairly disappointing to work with.

The HTML conversion did not like relative URLs, which consequently broke all of the stylesheets, images and everything else that was relatively referenced in my application. What a shame.

When attempting to construct the PDF manually, that was also another world of pain:

  • The PDF is constructed from bottom to top. What?!
  • No native table support, although the documentation specified that it was available in the example solution (No such example solution was available)
  • Custom fonts were not easily available (you had to install it to the server that was running the generation itself, fragmenting the solution  away…)
  • Lack of documentation (All questions on Stack Overflow were left unanswered).

It was a disaster – why would somebody pay for something like this? 

 

3. iTextSharp

After days and days of agony, iTextSharp came to the rescue. A freely available open source product. Whilst it didn’t have HTML to PDF conversions easily available, I was getting pretty sick of having a quick and dirty win.

I wanted to implement it correctly, which meant painfully by hand. Luckily although the documentation was a little lacking, there were heaps of help available on the internet. Win!

All in all, I think that iTextSharp is the winner here. It is well endorsed, widely supported on the internet and… it’s open source! Next time you are assigned to the ‘simple’ task of just generating a PDF for a client, I would suggest skipping the quick win approach of using a HTML to PDF converter and just dive straight into doing it manually.