Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML to PDF in .NET [closed]

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

  1. Download HtmlRenderer.PdfSharp nuget package.
  2. 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.

  1. First of all wkhtmltopdf is C++ implemented not C#, and you will experience various problems embedding it within your C# code, especially while switching between 32bit and 64bit builds of your project. Had to try several workarounds including conditional project building etc. etc. just to avoid "invalid format exceptions" on different machines.
  2. If you manage your own virtual machine its ok. But if your project is running within a constrained environment like (Azure (Actually is impossible withing azure as mentioned by the TuesPenchin author) , Elastic Beanstalk etc) it's a nightmare to configure that environment only for wkhtmltopdf to work.
  3. wkhtmltopdf is creating files within your server so you have to manage user permissions and grant "write" access to where wkhtmltopdf is running.
  4. Wkhtmltopdf is running as a standalone application, so its not managed by your IIS application pool. So you have to either host it as a service on another machine or you will experience processing spikes and memory consumption within your production server.
  5. It uses temp files to generate the pdf, and in cases Like AWS EC2 which has really slow disk i/o it is a big performance problem.
  6. The most hated "Unable to load DLL 'wkhtmltox.dll'" error reported by many users.

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

    • https://www.nuget.org/packages/GemBox.Document/
    • Free (up to 20 paragraphs)
    • $680 - https://www.gemboxsoftware.com/document/pricelist
    • https://www.gemboxsoftware.com/document/examples/c-sharp-convert-html-to-pdf/307
  • PDF Metamorphosis .Net

    • https://www.nuget.org/packages/sautinsoft.pdfmetamorphosis/
    • $539 - $1078 - https://www.sautinsoft.com/products/pdf-metamorphosis/order.php
    • https://www.sautinsoft.com/products/pdf-metamorphosis/convert-html-to-pdf-dotnet-csharp.php
  • HtmlRenderer.PdfSharp

    • https://www.nuget.org/packages/HtmlRenderer.PdfSharp/1.5.1-beta1
    • BSD-UNSPECIFIED License
  • PuppeteerSharp

    • https://www.puppeteersharp.com/examples/index.html
    • MIT License
    • https://github.com/kblok/puppeteer-sharp
  • EO.Pdf

    • https://www.nuget.org/packages/EO.Pdf/
    • $799 - https://www.essentialobjects.com/Purchase.aspx?f=3
  • WnvHtmlToPdf_x64

    • https://www.nuget.org/packages/WnvHtmlToPdf_x64/
    • $750 - $1600 - http://www.winnovative-software.com/Buy.aspx
    • demo - http://www.winnovative-software.com/demo/default.aspx
  • IronPdf

    • https://www.nuget.org/packages/IronPdf/
    • $399 - $1599 - https://ironpdf.com/licensing/
    • https://ironpdf.com/examples/using-html-to-create-a-pdf/
  • Spire.PDF

    • https://www.nuget.org/packages/Spire.PDF/
    • Free (up to 10 pages)
    • $599 - $1799 - https://www.e-iceblue.com/Buy/Spire.PDF.html
    • https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html
  • Aspose.Html

    • https://www.nuget.org/packages/Aspose.Html/
    • $599 - $1797 - https://purchase.aspose.com/pricing/html/net
    • https://docs.aspose.com/html/net/html-to-pdf-conversion/
  • EvoPDF

    • https://www.nuget.org/packages/EvoPDF/
    • $450 - $1200 - http://www.evopdf.com/buy.aspx
  • ExpertPdfHtmlToPdf

    • https://www.nuget.org/packages/ExpertPdfHtmlToPdf/
    • $550 - $1200 - https://www.html-to-pdf.net/Pricing.aspx
  • Zetpdf

    • https://zetpdf.com
    • $299 - $599 - https://zetpdf.com/pricing/
    • Is not a well know or supported library - ZetPDF - Does anyone know the background of this Product?
  • PDFtron

    • https://www.pdftron.com/documentation/samples/cs/HTML2PDFTes
    • $4000/year - https://www.pdftron.com/licensing/
  • WkHtmlToXSharp

    • https://github.com/pruiz/WkHtmlToXSharp
    • Free
    • Concurrent conversion is implemented as processing queue.
  • SelectPDF

    • https://www.nuget.org/packages/Select.HtmlToPdf/
    • Free (up to 5 pages)
    • $499 - $799 - https://selectpdf.com/pricing/
    • https://selectpdf.com/pdf-library-for-net/

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();
    }