7

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,

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185

5 Answers5

7

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:

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.

Community
  • 1
  • 1
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
5

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

We wrote a small tutorial here.

Mic
  • 24,812
  • 9
  • 57
  • 70
  • 3
    Is this by any chance programmable with c# ... I only see command line usages ?! – Jalal El-Shaer Mar 29 '10 at 22:37
  • There is a C bindings library, which should in theory be usable from C# using P/Invoke, but I've had no luck succeeding yet: http://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: http://code.google.com/p/wkhtmltopdf/issues/detail?id=614. – bambams May 17 '11 at 19:10
  • This 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). – Matt Mitchell May 23 '13 at 01:12
  • 1
    @MattMitchell On which OS are you running it? We do not see this issue on Ubuntu Server. – Mic May 23 '13 at 13:21
  • @Mic - Win7 + IIS7(.5?). I overlooked mentioning that I'm using the Pechkin wrapper (https://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. – Matt Mitchell May 24 '13 at 02:25
  • @Mic -- The like is dead, and a search on the site turns up absolutely nothing. – MicroservicesOnDDD Mar 09 '20 at 19:42
1

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

Constantine
  • 228
  • 1
  • 2
  • 10
0

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. :-)

  • 1
    Probably 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 May 23 '13 at 01:11
0

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):

Cyril N.
  • 38,875
  • 36
  • 142
  • 243