Maintain CSS styling when converting HTML to PDF in ASP.NET
Solution 1
Have a look at WKHTMLTOPDF. It is open source, based on webkit and free.
We wrote a small tutorial here.
Solution 2
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.
Solution 3
Try PDF Duo .NET converter. You can apply for support if you need a special feature.
http://www.duodimension.com/html_pdf_asp.net/downloads/html_pdf_net.zip
Matt Mitchell
You can email me at [email protected]. I also keep a technical log at http://MattMitchell.com.au/Technical
Updated on July 17, 2022Comments
-
Matt Mitchell almost 2 years
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
andcolor
but notbackground-color
).Is there something I am missing with ITextSharp, or is there a better (and free) way of doing this conversion?
Thanks in advance,
-
Jalal El-Shaer over 14 yearsIs this by any chance programmable with c# ... I only see command line usages ?!
-
bambams about 13 yearsThere is a C bindings library, which should in theory be usable from C# using P/Invoke, but I've had no luck succeeding yet: github.com/bamccaig/WkHtmlToPdfDflat. I've also tried writing a plain C program (from Linux, even) to directly test the C bindings, but even that has failed: code.google.com/p/wkhtmltopdf/issues/detail?id=614.
-
Matt Mitchell about 11 yearsProbably worthwhile mentioning that it's a commercial component. In fact the website is confusing on that matter - says it's free then says it's shareware.
-
Matt Mitchell about 11 yearsThis is what I went with. It has some serious performance issues though (seems to slow the entire AppDomain so needs short-lived applications on a schedule or regular restarts).
-
Mic about 11 years@MattMitchell On which OS are you running it? We do not see this issue on Ubuntu Server.
-
Matt Mitchell about 11 years@Mic - Win7 + IIS7(.5?). I overlooked mentioning that I'm using the Pechkin wrapper (github.com/gmanny/Pechkin) which may contribute (others are reporting similar issues there, but I haven't looked heavily into the issue to isolate the cause). Over time rendering slows down considerably, but as rendering isn't the fastest to start with (and seems to either fail completely or timeout intermittently) I''m now using a cached rendered report in my database and having a scheduled job generate reports periodically. Despite a few gotchas here and there the rendering is high quality.
-
MicroservicesOnDDD over 4 years@Mic -- The like is dead, and a search on the site turns up absolutely nothing.
-
MicroservicesOnDDD over 4 yearsPDFShift link appears dead.
-
Cyril N. over 4 yearsThank you, I've fixed the link!