Convert HTML to PDF in .NET [closed]

I want to generate a PDF by passing HTML contents to a function. I have made use of iTextSharp for this but it does not perform well when it encounters tables and the layout just gets messy. Is there a better way?

7,586 6 6 gold badges 57 57 silver badges 105 105 bronze badges asked Feb 19, 2009 at 10:21 SandHurst SandHurst

You can use GemBox.Document for this. Also here you can find a sample code for converting HTML file into a PDF file.

Commented Jan 25, 2016 at 7:51 Which version of iTextSharp do you use and could you share your html? Commented Mar 15, 2016 at 17:35

Still no answer to my request for additional information. Please also add if you are using HTMLWorker or XMLWorker.

Commented Jun 10, 2016 at 15:28 What about .net core? Commented Apr 12, 2019 at 9:10

Can we please reopen this one? Many new products provide this functionality, others are out of date. Without new answers, this can not be easily lined out. For 2022 I would recommend: github.com/hardkoded/puppeteer-sharp#generate-pdf-files Is well established, well maintained, simple to use, built on a solid basis etc.

Commented Jan 18, 2023 at 18:56

26 Answers 26

This will need Windows OS.

I have tested with ASP.net Core application and I used .net8.0-windows as target framework in order to use this nuget I also installed webview runtime

Also creator indicated windows desktop runtime as dependency too.

/// /// Return raw data as PDF /// /// [HttpGet("rawpdfex")] public async Task RawPdf() < var file = Path.GetFullPath("./HtmlSampleFile-SelfContained.html"); var pdf = new HtmlToPdfHost(); var pdfResult = await pdf.PrintToPdfStreamAsync(file, new WebViewPrintSettings < PageRanges = "1-10" >); if (pdfResult == null || !pdfResult.IsSuccess) < Response.StatusCode = 500; return new JsonResult(new < isError = true, message = pdfResult.Message >); > return new FileStreamResult(pdfResult.ResultStream, "application/pdf"); > 

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

  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.

or Especially For MVC Web Applications (But I think you may use it in any .net application)

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.

11.6k 14 14 gold badges 84 84 silver badges 116 116 bronze badges answered Aug 11, 2015 at 14:35 Anestis Kivranoglou Anestis Kivranoglou 8,053 5 5 gold badges 46 46 silver badges 48 48 bronze badges Thank you for updating your post. I'm going to give PdfSharp a try. You saved me a lot of time. Commented Aug 17, 2015 at 17:11

PdfSharp is good in terms of performance, but it didn't render floats properly for me. Luckily, I could change the markup to use good old tables, PdfSharp handles them well.

Commented Sep 14, 2015 at 19:12

We tried HtmlRenderer. It was really quick when not loading any CSS. But when we tried to apply CSS (Bootstrap plus some bespoke), the CSS parsing took a while (which we could probably mitigate), and rendering was completely different to the web page.

Commented Nov 10, 2015 at 22:42

BS. This creates an image of the HTML and adds the image into the pdf file. This is not a real PDF at all. Also, PDF is a vector graphics format - you can scroll near infinitely - of course except if the PDF consists of a raster graphic, which is what this library produces.

Commented Jun 6, 2017 at 9:12

@Anestis Kivranoglou i have used pdf sharp on my project. But for html design with css, it cannot render the html. Instead it is only creating a blank page

Commented Jun 21, 2019 at 11:59

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)