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,
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
Have a look at WKHTMLTOPDF. It is open source, based on webkit and free.
We wrote a small tutorial here.
Method 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
Method 4
ABCpdf provides two HTML rendering engines. One is based around the MSHTML version installed on the system. The other is based around the FireFox Gecko rendering engine.
So there’s plenty of room for manouver if you want a particular set of features. It’s very real world.
In terms of quality I would just say that we do get a lot of people settling on ABCpdf after trying a lot of different alternatives.
I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It’s just what I know. 🙂
Method 5
Why not use online API? There are plenty of them available and they do the work well, which let you worry about your core work, not how to render a PDF correctly 🙂
You mention something “free”. It depends on your usage, but most services offer free conversions to start with, ranging from 50 to 250 (and even more). Maybe it would be enough for you?
All you’d have to do then is a basic POST request to the service with your HTML data (or link), and you’ll get a PDF in response.
Here are a list of API to convert HTML to PDF (not exhaustive):
- PDFShift
- HTML2PdfRocket
- Web2PDFConvert
- PDFonline
- … (and many more 🙂 )
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0