So... this article is getting a little out of hand. Searching for a simple use case (converting HTML strings to PDF) turned into a full-fledged thesis. So here are some quick links to jump to sections of the article that might make things a little easier if you came here directly from Google. But I highly recommend it.Read from the ground up to understand what PDF generation actually looks like in .NET Core.
- what am i looking for
- Clear PDF
- select PDF
- Hierro PDF
- El resto (WKHTMLTOPDF, Spire PDF, EO PDF, Aspose PDF, ITextSharp)
Do you need a more powerful PDF generator?
It's a fairly common use case to want to generate PDF files in C# code, either serving them directly to the user or saving them locally. When I started doing this recently, I was suddenly overwhelmed with the options available to implement it. Of course, there's nothing in C# or .NET Core that will generate a PDF for you. And when it comes to looking at feature sets from third-party libraries (for example, I want to use HTML as a templating option), all sorts of problems arise.
So, having said that, instead of giving you code to generate PDFs using a specific PDF library, I'll talk about how I evaluated each option.
what am i looking for
Before I start my journey, I jotted down 3 very critical points by which I judge a library. None of this is too complicated or even a huge problem (or so I hope).
Obviously free is ideal. somethingopen sourceIt's even better if I can debug myself. But if I have to pay for the "premium" library. I'm looking for a one-time fee, not some stupid "per user/seat/machine/server" model. If I treat this library as a business, I don't want future architecture or decisions to be based on the library's price.
If there's some sort of freemium model in play, I'd also like to make sure the limits aren't too crazy (e.g. single page only, setting the number of images allowed per PDF). Freemium is fine as long as the free version is actually available.
HTML template (or something similar)
I've decided to use HTML as my templating mechanism. I'm open to some other reasonable alternative (such as HTML with some XSLT engine), but ideally I'd just like to send the HTML file to the library and output my PDF. What I really don't want to do is have to manually put each element into the PDF like I used to do when printing documents from WinForms applications.
Ease of use / All in one
This can be very subjective, but when you start looking for libraries in corners of the web or 3 year old stackoverflow answers, you usually end up with a really half-baked library that just doesn't work. Whether it's a C++ library that converts to C#, or a library that requires X amount of other libraries to work, or just doesn't work, I'll probably see it all. So, bottom line, I just want to be up and running in minutes, not hours.
First up is PDF Sharp, I think I've used this before but I'm not entirely sure. The whole ____Sharp thing was all the rage in the early days of C#. anyway straight from the beginningPDFSharp doesn't work with .NET Core.A ported version may exist, but the version on Nuget is not compatible with .NET Core. So it's pretty much dead in the water. But this one has been recommended to me over and over, so I thought I'd write a quick article about it anyway.
PDF Sharp is free and open source. They offer paid support, but don't provide details on how it works and how much it costs. Free is free, so you can't complain too much.
Good guy. Unfortunately, HTML templates don't make it into the Sharp PDF library. Instead, we've been writing syntax like we're back in the dark ages of C#'s GDI+:
// Get the XGraphics object to be drawn XGraphics gfx = XGraphics.FromPdfPage(page);// Create XFont font font = new XFont("Verdana", 20, XFontStyle.BoldItalic);// Draw text gfx.DrawString("Hello, World !", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
I know that's what you had to do in 2000, but it doesn't work now. There are ways around this by using another library that extends PDFSharp...but that's another library you have to acquire and use. Basically, it's a dud as far as a decent templating engine comes out of the box.
easy to use
OK It doesn't work with .NET Core, which can be a hindrance. The last version was released in 2013. It doesn't have an HTML rendering package (currently the library that extends it to make HTML templates doesn't support .NET Core either). In general. forget it.
they pushed me toselect PDFResponding through a ton of stack overflow (feeling like they might be messing around with astroturf a bit...). It's a paid library (which makes it even more annoying that they comment on stackoverflow answers with "of course, just use this great library called SelectPDF" without mentioning the cost), but nonetheless, I Still want to check to see what they have got, they offer.
It's not free, that's for sure. Licenses start at $499 and go up to $1599. Interestingly, they offer an online version where they can generate a PDF for you by submitting a URL or HTML code. I honestly think it's more interesting as a business model, but pricing starts at $19 again but goes up to $449 per month, so I don't really see many people choosing that option.
edit: Strangely, there seems to be a "hidden" free community edition that you can only find via a small footer link. You can read more aboutThe community edition is here.The only limitation seems to be that you cannot generate PDF files larger than 5 pages. Commercial use does not appear to have any restrictions. But then again, it seems weird to make this a small footer link and not sure where else...
HTML templates are definitely here. It seems to work fine. In a few lines, we're up and running. While the documentation is pretty good, I'm finding it all over the place. Loading HTML from a string is confused with loading HTML from a URL, so while reading the examples, you'll have to look at the code samples to see what it's actually used for. But having said that, HTML templates do work, so bookmark them!
easy to use
Here is the code to render the HTML:
var myHtml = "
test bold";Convertidor HtmlToPdf = new HtmlToPdf();PdfDocument doc = convertidor.ConvertHtmlString(myHtml);doc.Save("output.pdf");doc.Close();
So on that level, the ease of use seems to be good. I will say that it bothers me that a lot of the code examples are done using old style .NET web forms. I'm not sure why, but using such an ancient technology doesn't make me feel like I'm using top-notch technology.
It's hard to blame SelectPDF for what it really does. It generates PDFs and does what it says on the can. But I can't help feeling that this is a "budget" option for the price: the logo may not be transparent.
last untilHierro PDF.these guys make a bunch of libraries called IRON______ , so you've probably come across them before. I've come across them a few times because of their OCR library, which is unfortunate because as soon as they came out I knew the price would be outrageous.
For starters, they have all of these levels. For example, you can license individual projects, but you have to pay per developer(er). But...if you are a SAAS company, then you have to pay for each user of your SAAS product. If you're developing desktop software or distributed software, that's another new license. But let's keep a SAAS company for now, for 1000+ users I'll pay $1599 for the library and a year of support.
To be fair, most big companies aren't going to turn a blind eye to this. It's not super crazy, it's unique, but it's still very expensive.
Yes Yes. In IronPDF it works just fine. What impressed me most about IronPDF isyour document.It's not just "here's a snippet, good luck!". They even show you examples of how to use Handlebars as a templating engine, implement page breaks and even watermarks in HTML. To say it's feature-rich is an understatement.
easy to use
When it comes to ease of use, IronPDF wins again. In literally 4 lines, everything works and the output PDF is exactly as it should be:
var myHtml = "
bold test";var htmlToPdf = new HtmlToPdf();var pdf = htmlToPdf.RenderHtmlAsPdf(myHtml);pdf.SaveAs("output.pdf");
Right now I'm using inline HTML here, but I can upload an HTML file or even a remote URL and it will still work. Compared to other libraries we tested, this is the best in terms of ease of use.
Here are other libraries I've looked at, but didn't even bother to get over the first hurdle (learned my lesson after PDFSharp didn't even work on .NET Core).
At first glance, this really shouldn't be considered. WKHTMLTOPDF is actually a binary that can generate PDF from HTML files, but there are many .NET wrappers to use it. This seems odd to me, and doesn't feel like a solution, but rather a bunch of pieces limping together that should get you there in the end. Even if you wanted to go through that pain, the functionality is very limited. But it's free!
Ridiculous prices. Pricing starts at $599 for a single developer and $9,000 for 10+ developers. Might be fine for someone who hates money, but I'm skipping that.
The other is for people who hate money. Pricing starts at $749 and goes up to $4,000. Support for .NET Core also appears to be extremely patchy. Half the docs say they support .NET Core, but the target isFull frame.
You guessed it, another ridiculous price. It starts at $999 and goes up to $14,000 (fourteen thousand dollars) for an OEM license that still only allows 10 developers. Of course, if you want the enterprise support version, it will cost you a whopping $22,000 (twenty-two thousand dollars). approve.
Hidden prices usually mean "corporate" prices that some salesperson will call you to sell to you at the library. It appears to be available under the AGPL license for free applications, but I don't want to go into that.
So after all. Where does it leave us? Honestly, it seems to me that currently the only valid option for generating PDF files in .NET Core isHierro PDF.I know how much it costs, but relatively speaking, $1500 is really nothing compared to some other libraries. Also, it seems to have the most comprehensive API with up-to-date documentation in my opinion. If you have other PDF options in .NET Core, please feel free to comment below!
edit :It's been noticed that SelectPDF has a free community, so if you really want something free, it's worth checking out as well. But it's definitely still a two-horse race.