66

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.

5
  • The only thing I can think of is a python solution : htmltopdf.org :S Commented Jan 10, 2009 at 5:12
  • Why did you answer in a comment? Commented May 12, 2010 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 stackoverflow.com/questions/3178448/… Commented Jul 5, 2010 at 12:57
  • 1
    How do we vote to get this question re-opened? @Kev Commented May 16, 2013 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?. Commented Sep 21, 2017 at 15:22

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

8 Comments

In fact, it also has library version and I've implemented .NET wrapper for it github.com/gmanny/Pechkin (also on NuGet).
I tried the NuGet and it is great. Works perfectly.
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.
Does this bring in styling and images?
@AlBelmondo - it definitely honours CSS defined in a header <style> block. There is a setting web.loadImages that takes a true/false value but I haven't managed to get images to load yet.
|
18

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

7 Comments

will you please explain how to use that library. any code in either C# / VB.net
@Bilal, as it says in my answer, I've always used it on the command line and not as a library.
just write that line that you use for conversion from html to pdf.
@NormanRamsey does it support RTL language based Custom TTF fonts by using @fontface?
@mauvm fixed the link
|
2

Do you see :

They look as PrinceXML

Comments

0

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

Comments

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

4 Comments

Yeah, and they put their logo on the output
This answer should be deleted. The asker is clearly asking for an open source renderer.
Yes dear, let's start fretting about seven-year-old answers to closed questions.
Good, let's fret about TEN YEAR OLD questions now.
-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

3 Comments

Doesn't look like the standard edition is free anymore, "Licenses for ABCpdf .NET from only $329."
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.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.