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

39,599

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:

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

Share:
39,599
Matt Mitchell
Author by

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, 2022

Comments

  • Matt Mitchell
    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 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,

  • Jalal El-Shaer
    Jalal El-Shaer over 14 years
    Is this by any chance programmable with c# ... I only see command line usages ?!
  • bambams
    bambams about 13 years
    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: 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
    Matt Mitchell about 11 years
    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
    Matt Mitchell about 11 years
    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).
  • Mic
    Mic about 11 years
    @MattMitchell On which OS are you running it? We do not see this issue on Ubuntu Server.
  • Matt Mitchell
    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
    MicroservicesOnDDD over 4 years
    @Mic -- The like is dead, and a search on the site turns up absolutely nothing.
  • MicroservicesOnDDD
    MicroservicesOnDDD over 4 years
    PDFShift link appears dead.
  • Cyril N.
    Cyril N. over 4 years
    Thank you, I've fixed the link!