Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using ITextSharp to convert an HTML page to PDF.

However, ITextSharp prints the CSS in the STYLE declaration straight out, ignores stylesheets even when added programmatically and only listens to some inline styles (e.g. font-size and color but not background-color).

Is there something I am missing with ITextSharp, or is there a better (and free) way of doing this conversion?

Thanks in advance,

like image 815
Matt Mitchell Avatar asked Jan 06 '09 05:01

Matt Mitchell


2 Answers

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.

like image 88
Mauricio Scheffer Avatar answered Sep 27 '22 23:09

Mauricio Scheffer


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

We wrote a small tutorial here.

like image 34
Mic Avatar answered Sep 27 '22 22:09

Mic