EDIT: New Suggestion HTML Renderer for PDF using PdfSharp
(After trying wkhtmltopdf and suggesting to avoid it)
HtmlRenderer.PdfSharp is a 100% fully C# managed code, easy to use, thread safe and most importantly FREE (New BSD License) solution.
Usage
Use Example Method.
public static Byte[] PdfSharpConvert(String html)
{
Byte[] res = null;
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
return res;
}
A very Good Alternate Is a Free Version of iTextSharp
Until version 4.1.6 iTextSharp was licensed under the LGPL licence and versions until 4.16 (or there may be also forks) are available as packages and can be freely used. Of course someone can use the continued 5+ paid version.
I tried to integrate wkhtmltopdf solutions on my project and had a bunch of hurdles.
I personally would avoid using wkhtmltopdf - based solutions on Hosted Enterprise applications for the following reasons.
--- PRE Edit Section ---
For anyone who want to generate pdf from html in simpler applications / environments I leave my old post as suggestion.
TuesPechkin
https://www.nuget.org/packages/TuesPechkin/
or Especially For MVC Web Applications (But I think you may use it in any .net application)
Rotativa
https://www.nuget.org/packages/Rotativa/
They both utilize the wkhtmtopdf binary for converting html to pdf. Which uses the webkit engine for rendering the pages so it can also parse css style sheets.
They provide easy to use seamless integration with C#.
Rotativa can also generate directly PDFs from any Razor View.
Additionally for real world web applications they also manage thread safety etc...
Last Updated: October 2020
This is the list of options for HTML to PDF conversion in .NET that I have put together (some free some paid)
GemBox.Document
PDF Metamorphosis .Net
HtmlRenderer.PdfSharp
PuppeteerSharp
EO.Pdf
WnvHtmlToPdf_x64
IronPdf
Spire.PDF
Aspose.Html
EvoPDF
ExpertPdfHtmlToPdf
Zetpdf
PDFtron
WkHtmlToXSharp
SelectPDF
If none of the options above help you you can always search the NuGet packages:
https://www.nuget.org/packages?q=html+pdf
Most HTML to PDF converter relies on IE to do the HTML parsing and rendering. This can break when user updates their IE. Here is one that does not rely on IE.
The code is something like this:
EO.Pdf.HtmlToPdf.ConvertHtml(htmlText, pdfFileName);
Like many other converters, you can pass text, file name, or Url. The result can be saved into a file or a stream.
I highly recommend NReco, seriously. It has the free and paid version, and really worth it. It uses wkhtmtopdf in background, but you just need one assembly. Fantastic.
Example of use:
Install via NuGet.
var htmlContent = String.Format("<body>Hello world: {0}</body>", DateTime.Now);
var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);
Disclaimer: I'm not the developer, just a fan of the project :)
You can use Google Chrome print-to-pdf feature from its headless mode. I found this to be the simplest yet the most robust method.
var url = "https://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net";
var chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
var output = Path.Combine(Environment.CurrentDirectory, "printout.pdf");
using (var p = new Process())
{
p.StartInfo.FileName = chromePath;
p.StartInfo.Arguments = $"--headless --disable-gpu --print-to-pdf={output} {url}";
p.Start();
p.WaitForExit();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With