63

I asked about getting iTextSharp to render a PDF from HTML and a CSS sheet before here but it seems like that may not be possible... So I guess I will have to try something else.

Is there an open source .NET/C# library out there that can take HTML and CSS as input and render it correctly?

I must reiterate... the library MUST be free and preferably something with a fairly liberal license. I'm working with basically no budget here.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Adam Haile
  • 30,705
  • 58
  • 191
  • 286
  • The only thing I can think of is a python solution : http://www.htmltopdf.org/ :S – bchhun Jan 10 '09 at 05:12
  • Why did you answer in a comment? – bentford May 12 '10 at 21:28
  • 1
    Because there are so many questions similar to this one but not quite the same, I decided to try to collect a complete list of HTML to PDF converters into a community wiki question http://stackoverflow.com/questions/3178448/list-of-html-to-pdf-converters – rjmunro Jul 05 '10 at 12:57
  • 1
    How do we vote to get this question re-opened? @Kev – PeterX May 16 '13 at 23:56
  • This question is off-topic at SO, but on-topic in softwarerecs.SE. See [How can I convert HTML with CSS to PDF?](https://softwarerecs.stackexchange.com/q/45903/1834). – Martin Thoma Sep 21 '17 at 15:22

6 Answers6

41

This command line tool is the business! https://wkhtmltopdf.org/

It uses webkit rendering engine(used in safari and KDE), I tested it on some complex sites and it was by far better than any other tool.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
isaac
  • 631
  • 1
  • 5
  • 10
  • 14
    In fact, it also has library version and I've implemented .NET wrapper for it https://github.com/gmanny/Pechkin (also on [NuGet](https://nuget.org/packages/Pechkin.Synchronized)). – Gman Jul 27 '12 at 14:24
  • I tried the NuGet and it is great. Works perfectly. – Dilhan Jayathilake May 16 '14 at 05:38
  • 1
    Outstanding! I tried this vs iTextSharp and AsposePDF. Aspose rendered horribly for me and iTextSharp just kept throwing exception after exception. wkhtmltopdf worked on the first try and rendered everything perfectly. – Sean Chase Nov 13 '14 at 18:32
  • Does this bring in styling and images? – Al Belmondo Jun 25 '15 at 17:11
  • @AlBelmondo - it definitely honours CSS defined in a header – Peter Wone Aug 10 '15 at 01:11
  • The tuesPechkin library for wkhtmltopdf is great and thread safe, however, cannot process conversions concurrently -- which is my current problem. – VFein Sep 04 '15 at 13:58
  • Also [Rotativa](https://github.com/webgio/Rotativa) is using `wkhtmltopdf` under the hood too, if i understand correctly. – Guru Stron Sep 29 '15 at 12:37
  • Our IronPDF Library builds upon the great work done in wkhtmltopdf. CSS is fully supported. [IronPDF.com](https://ironpdf.com) makes the process of adding PDF functions to a project quick and easily. – Stephanie Apr 25 '18 at 05:28
17

I've always used it on the command line and not as a library, but HTMLDOC gives me excellent results, and it handles at least some CSS (I couldn't easily see how much).

Here's a sample command line

htmldoc --webpage -t pdf --size letter --fontsize 10pt index.html > index.pdf
arauzo
  • 185
  • 1
  • 7
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
2

Do you see :

They look as PrinceXML

FriendFX
  • 2,929
  • 1
  • 34
  • 63
azerttyu
  • 21
  • 1
0

You could try my wkhtmltopdf wrapper: https://github.com/pruiz/WkHtmlToXSharp ;)

Pablo Ruiz García
  • 573
  • 1
  • 5
  • 9
-2

It's not open source, but you can at least get a free personal use license to Prince, which really does a lovely job.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
-3

Try ABCpdf from webSupergoo. It's a commercial solution, not open source, but the standard edition can be obtained free of charge and will do what you are asking.

ABCpdf fully supports HTML and CSS, live forms and live links. It also uses Microsoft XML Core Services (MSXML) while rendering, so the results should match exactly what you see in Internet Explorer.

The on-line demo can be used to test HTML to PDF rendering without needing to install any software. See: http://www.abcpdfeditor.com/

The following C# code example shows how to render a single page HTML document.

Doc theDoc = new Doc();
theDoc.AddImageUrl("http://www.example.com/");
theDoc.Save("htmlimport.pdf");
theDoc.Clear();

To render multiple pages you'll need the AddImageToChain function, documented here: http://www.websupergoo.com/helppdf7net/source/5-abcpdf6/doc/1-methods/addimagetochain.htm

AffineMesh
  • 1,025
  • 8
  • 14
  • Doesn't look like the standard edition is free anymore, "Licenses for ABCpdf .NET from only $329." – Levitikon Oct 13 '11 at 21:51
  • The free license is offered through a promotional scheme. If it disappears, try googling for 'websupergoo free licenses'. Note the offer is for single license only. If you need to purchase a license or additional licenses, the price is as Levitikon has said. – AffineMesh Oct 18 '11 at 10:00
  • Yes, @AffineMesh could you please revise your text above saying the "standard edition can be obtained free of charge". Or simply add an extra note. Incidentally, I find their license somewhat confusing: "The Owner offers you a non-exclusive, non-transferable license on the terms of this Agreement". What does this mean - do I still have to get one? – PeterX May 16 '13 at 23:42