Maintain CSS styling when converting HTML to PDF in ASP.NET

HTML / CSS support in iText / iTextSharp is very basic. It's just not the right tool to convert html to pdf. Take a look at these solutions instead:

  • Create screenshot of the page with Watin-like tool
  • http://blog.taiki.be/index.php/2008/07/generating-screenshots-of-webpages-using-net/
  • http://www.codegod.de/WebAppCodeGod/Screenshot-of-Webpage-with-ASP-NET-AID398.aspx

These render html to an image. Then you can insert them in your PDF with iTextSharp.

Otherwise you could try converting HTML -> XSL-FO -> PDF, but including CSS there is a whole other thing.


Have a look at WKHTMLTOPDF. It is open source, based on webkit and free.

We wrote a small tutorial here.