So… This post spun a little out of control. What turned into a hunt for a simple use case (Turn an HTML string into a PDF) turned into a full blown thesis. So here’s some quick links to jump to various parts of the article if you are coming here straight from Google, it may make things a bit easier. But I do highly recommend reading from the start to get an idea of what PDF Generation really looks like on .NET Core.
- Introduction
- What I Was Looking For
- PDF Sharp
- SelectPDF
- IronPDF
- The Rest (WKHTMLTOPDF, Spire PDF, EO PDF, Aspose PDF, ITextSharp)
- Summary
Need a more powerful PDF generator?
Our sponsor, the DocRaptor HTML-to PDF API, lets you create complex PDFs from HTML, CSS, and JavaScript. Only DocRaptor’s API supports advanced features such as varying headers and footers, page break fine-tuning, accessible PDFs, and more. Pricing starts at just $15/mo.
Introduction
It’s a pretty common use case to want to generate PDF’s in C# code, either to serve directly to a user or to save locally. When I came to do it recently, suddenly I was over(and under)whelmed with the options available to actually achieve this. Certainly natively, there is nothing in C# or .NET Core that can generate PDF’s for you. And when it came to actually looking at feature sets of third party libraries (e.g. I want to use HTML as a template option), there were all sorts of problems.
So with that being said, instead of giving you code to generate a PDF with a particular PDF library, I’ll talk about how I evaluated each option.
What I Was Looking For
Before I set out on my journey, I wrote down 3 really crucial points that I would judge the libraries on. These aren’t too complex or even that much of an ask (Or so I hoped).
Price
Obviously free is ideal. Something open source that I can debug myself is even better. But if I do have to pay for a “premium” library. I’m looking for a one time fee that isn’t some stupid “per user/seat/machine/server” model. If I’m looking at this library as a company, I don’t want future architecture or decisions to be made based on the pricing of a library.
If there is some sort of freemium model in play, then I also wanted to make sure that the limitations weren’t too crazy (e.g. single pages only, set number of images allowed per PDF). Freemium is OK as long as the free version is actually useable.
HTML Templating (Or something close)
I had already decided that I wanted to use HTML as my templating mechanism. I was open to using some other reasonable alternative (e.g. HTML with some XSLT engine), but ideally I just want to feed an HTML file to the library and out comes my PDF. What I really don’t want to do is have to place each element manually on the PDF like we had to back in the day when printing a document from a WinForms application.
Ease Of Use/All In One
This is probably a pretty subjective one, but when you start seeking out libraries from the corners of the web or that stackoverflow answer from 3 years ago, you often end up getting some really half baked library that just doesn’t work. Whether it’s some C++ library converted to C#, a library that needs X number of other libraries to actually function, or things just plain don’t work how they should, I’m probably going to see it all. So above else, I just want to be up and running in minutes, not hours.
PDF Sharp
First up is PDF Sharp, I feel like I’ve used this one previously but I’m not entirely sure. The whole ____Sharp thing was all the rage in the early days of C#. Anyway straight off the bat PDFSharp does not work with .NET Core. There may be ported versions floating around but the version on Nuget does not support .NET Core. So it’s pretty much dead in the water. But this one was suggested to me over and over so I still want to do a quick write up about it.
Price
PDF Sharp is free and open source. They do offer paid support but don’t give any specifics on how this works and how much it costs. Free is free though so you can’t complain too much.
HTML Templating
Oh boy. HTML Templating doesn’t make it into the PDF Sharp library unfortunately. Instead we are stuck with writing syntax like we are back in the GDI+ dark days of C# :
// Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); // Create a font XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic); // Draw the text gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
I get it that this was what you had to do in the year 2000, but it’s just not going to fly right now. There are ways around this using another library that extends PDFSharp… But that’s still another library that you have to grab and work with. So basically, in terms of a decent template engine out of the box, it’s a fail.
Ease Of Use
Well. It doesn’t work with .NET Core which is probably going to be a blocker. The last published version was 2013. And it doesn’t have HTML rendering packaged (And the library that currently does extend it to do HTML templating also doesn’t support .NET Core). So all in all. Forget about it.
SelectPDF
I got pushed to SelectPDF by a tonne of stackoverflow answers (Kinda felt like maybe they were astroturfing a little…). It’s a paid library (Which made it all the more annoying they were commenting on stackoverflow answers with “Sure just use this great library called SelectPDF” and not mentioning the cost), But regardless I wanted to check them out and see what they offered.
Price
It ain’t free, that’s for sure. Licensing starts at $499 and goes up to $1599. Interestingly they do offer an online version where they can generate the PDF for you by sending a URL or HTML code. Honestly I think that’s way more interesting as a business model, but the pricing again starts at $19 but goes all the way up to $449 a month so I can’t really see many people taking this option.
EDIT : So weirdly enough, there seems to be a “hidden” free community edition that you can only find via a tiny footer link. You can read more about the Community Edition here. It seems like the only limitation is that you can’t generate PDFs over 5 pages. Doesn’t seem to be any limitation on commercial use. But again, seems strange to only make this a tiny footer link and not sure it anywhere else…
HTML Templating
HTML Templating is definitely here. And it seems to work well. In just a few lines we are up and running. While documentation is pretty good, I found it sort of all over the place. Loading HTML from a string versus loading HTML from a URL was all mixed together and so when reading examples, you had to check the code example to see what it was actually used for. But that being said, HTML Templating works so tick!
Ease Of Use
Here’s the code to render HTML :
var myHtml = "<style>h1 {font-size:12px;}</style><h1>Test</h1><p style='font-weight:bold'>Test Bold</p>"; HtmlToPdf converter = new HtmlToPdf(); PdfDocument doc = converter.ConvertHtmlString(myHtml); doc.Save("output.pdf"); doc.Close();
So on that level, ease of use seems good. I would say that many code examples were done using old school .NET WebForms which made me uneasy. Not sure why but using such an old technology like that doesn’t make me feel like I’m using the best in class.
It’s hard to fault SelectPDF on the actual bones of what it’s doing. It generates PDF’s and does what it says on the tin. But I can’t help but feel like it’s a “budget” option for the price – It’s probably the logo not being transparent.
IronPDF
Last up, IronPDF. These guys create a bunch of libraries called IRON______ so you may have run into them before. I’ve run into them a few times for their OCR library, and that’s unfortunate because as soon as they came up, I knew the pricing was gonna be out of this world.
Price
For starters they have all these tiers. Like you can get a single project license, but you have to pay per developer (ugh). But.. If you are a SAAS company, then you have to pay per user of your SAAS product. If you are developing desktop software or something distributed, well that’s a new license again. But let’s just stick with being a SAAS company for now, for 1000+ users I’m going to be paying $1599USD for the library and a years worth of support.
In fairness, most large companies aren’t going to bat an eye at that. It’s not super crazy, and it is one off, but it’s still pretty pricey.
HTML Templating
Yes and yes. In IronPDF it just works. What impressed me the most about IronPDF was their documentation. It wasn’t just “Here’s a snippet, good luck!”. They even went as far to show you examples of how you might use Handlebars as a templating engine, implementing page breaks in your HTML, and even watermarking. To say that it’s feature rich would be an understatement.
Ease Of Use
Again, IronPDF comes up trumps when it comes to ease of use. In literally 4 lines everything just works and the output PDF is exactly as it should be :
var myHtml = "<style>h1 {font-size:12px;}</style><h1>Test</h1><p style='font-weight:bold'>Test Bold</p>"; var htmlToPdf = new HtmlToPdf(); var pdf = htmlToPdf.RenderHtmlAsPdf(myHtml); pdf.SaveAs("output.pdf");
Now here I’m using inline HTML, but I can load an HTML file or load a remote URL even and it still just works. Compared to the other libraries we’ve tried, this one is certainly the best when it comes to ease of use.
The Rest
Here are the other libraries I looked at but didn’t even bother going past the first hurdle (I sort of learnt my lesson after PDFSharp didn’t even work on .NET Core).
WKHTMLTOPDF
This shouldn’t really even be considered on the face of it. WKHTMLTOPDF is actually a binary that can generate PDF’s from HTML files, but there are a bunch of .NET wrappers to make use of it. Just felt odd to me and didn’t feel like a solution as much as it was a bunch of pieces hobbled together that should get you there in the end. Even if you do want to go through that pain, features are rather limited. But it’s free!
Spire PDF
Ridiculous pricing. Starts at $599 for a single developer and goes up to $9000 for over 10 developers. Might be OK for people who hate money but I skipped this one.
EO PDF
Another one for haters of money. Pricing starts at $749 and goes up to $4000. Support for .NET Core also seemed extremely sketchy. With the documentation half saying they support .NET Core, but to target Full Framework.
Aspose PDF
You guessed it, another one with ridiculous pricing. Starts at $999 and goes up to $14000 (Fourteen thousand) for an OEM license that still only allows 10 developers. Of course if you want the enterprise support edition you’re going up to $22000 (Twenty two thousand). Pass.
ITextSharp/IText 7
Hidden pricing which typically means “enterprise” priced where some salesman will call you to sell you on the library. Seems to work on AGPL license for free applications but rather not get into that.
Summary
So after all of that. Where does it leave us? Honestly in my opinion the only valid option right now to generate PDF’s on .NET Core is IronPDF. I know it costs but relatively speaking, $1500 is actually nothing compared to some of these other libraries. And again in my opinion, it seemed to have the most fully featured API along with the most up to date documentation. If you have another PDF option in .NET Core, feel free to drop a comment below!
EDIT : It’s been pointed out that SelectPDF does have a free community so that’s worth a crack too if you really want something out and out free. But it’s definitely still a two horse race.
There’s another option here: https://github.com/VahidN/iTextSharp.LGPLv2.Core
Also there are some .NET Core wrappers for https://github.com/topics/wkhtmltopdf?l=c%23
there are mono builds for pdfsharp, also pdfsharp is a low level api, not meant for templating, you have to look into migradoc for templating
You could call out to ghostscript too. You can render (i.e. print) the html to pdf.
Whilst ghostscript is a binary, it’s available on probably every platform ever created…including the kitchen sink.
WKHTMLTOPDF is the best option for .net core.. its great for html to pdf conversion directly and its free..
You forgot the most important thing when addressing a PDF library for .Net Core, it should be compatible to work cross platform on Windows and Linux as well.
I’ve used all of the libraries you’ve mentioned and even more.
SelectPDF does not work on Linux, they specifically say that it only work on Windows.
IronPDF, they say that it works on Linux, they even have an answer for that in their FAQ’s where they even give the command line to run in terminal to install the relevant binaries, BUT their binaries are no where to be found and the command line fails to install them. I’ve contacted their support, sent them the full error message and they haven’t replied for over a month now.
All other libraries, including PDFSharp, doesn’t work on Linux.
The ONLY library that currently works on Linux is iText7 which is the newer version of iText5 (iTextSharp).
Great comment Liran! Thanks for that. Also sad to see that IronPDF hasn’t responded to you given it’s supposed to be a “premium” library.
Very useful comment, thank you. I too have been plugging away at finding a cross platform html2pdf library. I’ve evaluated over 10 already, and IronPdf seemed like the only one to support Linux (based only on their documentation).
Is itext7.html2pdf free?
The other option is that you deploy a (web)service with your application that can execute JavaScript.
Then you can request a PDF from the service, with a free JS pdf library like https://parall.ax/products/jspdf.
We ran through the same comparison, and ended up with the same frustration (we are software vendor with both SAAS and on-prem customers). We ended up with JSReports. Maybe not a very intuitive decision – but what about thinking out of the box 🙂 JSReports runs as a separate NodeJS application; our .Net Core generates HTML string and POSTs it to JSPreport that generates PDF. Works pretty neat
FWIW – There is a FreeSpire .NET version which has some restrictions in terms of the number of pages, etc. I haven’t tried to see if it run on Linux, however.
There are also other libraries, for example syncfusions: https://www.syncfusion.com/sales/products/fileformats
there is also a free community license: https://www.syncfusion.com/products/communitylicense that includes all their products! 🙂
This, SyncFusion is the way to go, I’ve used their PDF generator a few times and it just works. Plus they offer a community license.
I’ve used syncfusion pdf generator also, works great, although their html formatting in the pdf is not so good yet. they provide a HTMLElement but it does not work properly
I am surprise you did not mention https://www.evopdf.com/. They have .NET and .NET Core versions. They have a great and easy to implement HTML to PDF converter for multiple platforms. It has Nuget packages available. While they have a deployment license which is single server, single application, they have a company license available. The company license is unlimited devs, unlimited apps, unlimited deployments. The bad part is you can’t just purchase for a single platform. For $200 more, you get other tools also like PDF Merge and PDF Split. They actually give you a lot of bang for the buck, but most of it will probably go unused.
SpirePDF has a free edition,
free edition is limited to 10 pages of pdf!
I made a wrapper around wkhtmltopdf that includes all binaries, it works on windows, linux and mac.
https://github.com/pug-pelle-p/pugpdf
wkhtmltopdf is workable solution, but it doesn’t support password protection
If you have case like my (.net core, free library, ubuntu container and password protection is needed) for now good solution is using nuget packages:
-PdfSharpCore for creating pdf
-HtmlRendererCore.PdfSharpCore for rendering pdf from html
Example of usage:
var document = PdfGenerator.GeneratePdf(html, PageSize.Letter)
RUN apt-get update && apt-get install -y libgdiplus
is needed for docker
Did anyone try Adobe’s library?
https://www.datalogics.com/products/libraries/adobe-pdf-library/
That’s not Adobe, it’s DataLogics
I did on this very blog! https://dotnetcoretutorials.com/2020/11/12/reading-and-writing-pdfs-in-c-with-datalogics-pdf-sdk/
It’s pricey but I had to say, it has some really really advanced features in OCR and redacting text etc. I think you would want to be doing more with PDF’s than just generating them from a HTML file, but it’s got a crazy level of power.
And @Alan Bourke, true that it is DataLogics but DataLogics is Adobe’s official partner for the C# SDK (e.g. You won’t find any other C# SDK given out by Adobe).
I too have conducted similar research, also with disappointing results. We are happy to pay for the license providing it is reasonable. Aspose and Syncfusion pricing is simply crazy, so I have not even evaluated them.
We are using the Mvc razor engine for templating, and extract the Html directly from that. We therefore needed a library to convert Html strings or streams to Pdf documents.
For us, a crucial component was that we could specify the header in Html. This eliminated any wkhtmltopdf wrapper library, since wkhtmltopdf only supports a header as a document or url. Writing the header to a temp file and deleting afterwards smelled too bad for my liking. If this is not crucial for you, then one of the wrappers for wkhtmltopdf could work very well, DinkToPdf is probably the best bet.
Another crucial for us was that the css style “page-break-inside: avoid;” must actually work. IronPdf was one of the few libraries that seemed to completely ignore this style and would split a table row in half at the page break. Wow! This completely ruled out IronPdf for us.
Two libraries not mentioned above are HiQPdf and Winnovative. These are both reasonably priced, and in my test use case worked the best. Page headers were simple and css was very well honoured. However, as with most of these libraries, they don’t support Linux directly!
So, after 2 weeks of research we still don’t really have a solution.
Have you looked at Puppeteer Sharp? That uses Headless Chromium to fetch your website and create a pdf from it. We are using that with MVC Razor to create pdfs – by simply hosting the pages and pointing puppeteer at it. It may sound like overkill, but when you have set it up, it works fine.
Puppeteer Sharp did not work on Azure/ windows as it use GDI and GDI is restricted on Azure for security reasons
Hello there, what did you come up with at the end? After 2 weeks of searching, I could not find any tool that suits my purposes.
HiQPdf dosn’t work on linux !
I always used FONET, which is simple to use and open-source (Apache V2). You can import it to your project through Nuget. It works with XML and XSL stylesheet. You can also embed images.
Well, here’s another idea. Why not run your own ‘SaaS’ PDF generator thingy. Just build a simple web API with .NET Framework (which will be supported for many years) and an open-source package that does HTML to PDF, and host it on a VM somewhere.
> PDFSharp does not work with .NET Core
It’s open source and can be trivially built for .NET Core. Use migrate-2019 + System.Drawing.Common NuGet package.
We did have to wait for some updates to .net core for fonts, but Scryber now works on 3.1 and open-source with Mac and Linux support (adding GDI+). Hope it helps someone, without being a selfless plug.
https://github.com/richard-scryber/scryber.core
Has anyone tried PdfSharpCore?
https://github.com/ststeiger/PdfSharpCore
Can anyone please tell which is the best option for a PDF viewer that toolbar can be customized. My need is to open a PDF file without download option. Mine is a .net core project.
For *displaying* a PDF, you need a JS option such as PDF.js https://mozilla.github.io/pdf.js/
no one mentioned pdftron – https://www.pdftron.com/documentation/linux/guides/
apparently you don’t pay until you ship.
plus foxit has an SDK and they have linux version
To be honest, any library with “secret pricing” shouldn’t really need to be mentioned.
Has anyone tried DevExpress Office File API Version 20.1?
We’re a bit too heavily invested in wkhtmltopdf to switch now, at least for existing projects. I kinda miss Crystal Reports, it had issues and quirks, but worked well for labels with a specific fixed layout. Getting that kind of layout correct in html can be tricky.
Can confirm that a .NET Core port of (free) PdfSharp worked for me in .NET 5, after several days of trying various solutions. I can now batch WPF layouts into a multi-page XpsDocument and save it as PDF. The one that worked for me is https://www.nuget.org/packages/PdfSharp.Xps.dotNet.Core with less than 2,000 downloads
A little bit of my own experiences…
My use case is pretty strictly HTML to PDF, with a little manipulation of existing PDFs like deleting pages.
I tried IronPDF back in July 2018 and had a horrible experience both technically and personally. The font rendering just didn’t seem to work right. It ignored any fonts that were in the style sheets and everything was blocky. Their customer support was slow and couldn’t provide a way to fix the problem. They then argued profusely with me when I asked them for a refund. Definitely not “customer oriented” folks over there at the time. Luckily the reseller (MyCommerce) quickly supported me and refunded me without delay.
So my goto remains Eo.PDF. I’ve used that successfully for many, many years. It has its limitations and annoyances, yes. It’s not cheap, and costs $240/year to renew for updates; it doesn’t really support .NET Core; and it doesn’t work in a shared web environment like Azure App Service, it has to run on a machine. But it works very, very well with all the HTML I throw at it.
It’s funny that I found this post because I was looking to see if there was anything new, cheaper or better before I renew Eo.PDF again. And there may be? But this has actually convinced me to just renew. 🙂
Docati supports .Net core ans runs on Windows and Linux. It uses Word documents as templates and requires an add-in for Word to edit the templates. The idea behind using Word is that business people can maintain the templates and that Word is a better document design
This is their sample code: https://github.com/kwaazaar/docati.api.demo
Thank you for the tutorial. But you forgot to mention PDFFlow library (https://www.pdfflow.io) for C#. It also creates PDF documents from scratch. But it supports repeating headers, automatic page numeration, multi-page spread tables and many other automatic layouting options which are not described in your tutorial regarding other libraries.
Also, there are samples of different business documents in repo (https://github.com/gehtsoft-usa/PDF.Flow.Examples), I haven’t seen that much from other PDF generating libraries.
A bit late to the party, but removing the PDFSharp package from my solution and using NuGet to install the PDFSharpCore version solved the issue as of 7/2021
What about https://playwright.dev/dotnet/ ?
Not tried it yet myself but it seems to support printing to PDF (as well as all the automation things etc) and you would get something like the Chromium engine for rendering.
Microsoft supported as well and Linux support etc.
I tried all the options you mentioned and some others in a .Net 5 / Docker environment, and became convinced that your conclusion about IronPDF is right (SelectPDF works very well too, but is Windows only, despite claiming .net core compatibility).
But IronPDF does its job by downloading and using chrome (or chromium), and there is now a free and open source way to do exactly the same thing, which is to use Puppeteer-Sharp. See https://www.kambu.pl/blog/how-to-generate-pdf-from-html-in-net-core-applications/
Did similar research and was disappointed by the lack of good solutions at affordable prices. However, head and shoulders above all other solutions, was GemBox which has a fairly restrictive free version but uses c# only implementation of word/ excel/ pdf templating and the word/ excel fidelity is excellent for most use cases. The paid for versions are not eye wateringly expensive and but have relatively competitive prices on all solutions. Samples are easy to follow and you will be able to get an example up and running very quickly. Other than being a customer, for a company who have implemented in an enterprise solution, I have no association with the company but think it is an excellent product. see https://www.gemboxsoftware.com/
https://github.com/pug-pelle-p/pugpdf – made a .net 6 app that creates PDF and it runs/works on MAC, fyi (so I assume it’ll work on Linux) – free library
If you don’t need advanced pdf features but want perfect html rendering, I can recommend PuppeteerSharp (or Playwrigth). The best is that you can easily tune your html and try to print it as pdf in Chrome – you’ll get exactly the same result from PuppeteerSharp. We’re creating 200+ pages pdfs and it works well.
I’m really surprised that no-one has mentioned XSL-FO which is a robust templating approach to rendering PDFs and Apache FOP is a free rendering engine. It may not account for all of the HTML to PDF requirements out there since XSL-FO is it’s own templating language (Xml based), and modern HTML and CSS wouldn’t translate too wee because Table Layouts are the best way to get what you are looking for. But it wouldn’t be unheard of to apply some XSLT to convert simple HTML to XSL-FO… I think IBM used to host a set of XSLTs that did that in a tutorial somewhere.
But, for those wanting great control and flexibility then any XSLT templates or Razor templates work great to render the output and then ApacheFOP runs awesome in a serverless setup for a Pdf-as-a-Service capability…. and ApacheFOP.Serverless is a ready-to-go project for Azure Functions; of which I’m the owner the ApacheFOP.Serverless project on GitHub.
Some working examples are also in my .NET Repo: https://github.com/cajuncoding/PdfTemplating.XslFO
I think DevExpress PDF report generating library is not so bad and also pricing is manageable.
https://www.devexpress.com/subscriptions/reporting/
Hi, from which year is this article?