3 min read

3 Best PDF Generation Tools For C# ASP.NET

3 Best PDF Generation Tools For C# ASP.NET
Photo by Austin Distel / Unsplash

Many people ask “What is the best tool for generating PDFs?”. You’d be surprised, but this can become a difficult decision to make especially if you’ve been away from .NET for a while. What’s more, there are tons of frameworks and tools out there and the process is even more overwhelming.

So, which one should you choose when your business asks you to implement PDF Generation or you just want to plug it into your app to fill in the gaps?

Well, here are top 3 most appreciated tool on the market. At the end of which, there will be a list of honorable mentions, that people have also used and enjoyed.

1. Puppeteer-Sharp 3

Puppeteer Sharp is an open-source browser automation tool used for generating PDFs out of the DOM. It is developed and maintained by Google. It is a framework written in C# and according to the Google, “a Node library which provides a high-level API to control headless Chrome and Chromium” (source). The engine is similar to how Selenium works.

This framework can be used for a multitude of reasons, some of which being:

  • Generating PDFs, of course
  • Automating form submissions: useful especially for UI/E2E (end-to-end) testing.
  • Test Chrome Extensions: should you roll like that.

Here is a nice video explaining how this is used. And here is their official GitHub page.

Here is an example of setting it up and generate a PDF in a C# ASP.NET application:

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.google.com");
await page.PdfAsync(outputFile);

2. Playwright (sharp)

Playwright is the Microsoft’s version of a browser automation tool built also in C# used for the same purposes as Puppeteer, including but not limited to, PDF generation and automation UI testing by crawling your SPA.

Installation is quite straight forward. It doesn’t have any dependencies other than the Playwright driver and the actual browsers, and so, here is how to install it from the package manager console:

dotnet add package PlaywrightSharp

And here is how to use it (source):

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GoToAsync("http://www.bing.com");
await page.ScreenshotAsync(path: outputFile);

As stated previously the implementation is similar to Puppeteer, the difference being that Playwright works in Edge.

The API documentation can get you up and running in no time as people love the fact that this is easy to set up. Engineers also stated that Playwright is more stable than Puppeteer Sharp.

However, the downside being that if your project requires a more sophisticated PDF being generated, you might look into tailored (even premium) solutions, depending on your use case.

3. Wkhtmltopdf

Wkhtmltopdf is a popular open-source command line tool used to generate PDFs out of HTML. It runs “headless”, thus not requiring a display service.

Their download page can be found here. It is compatible with Windows, macOS and the major Linux distros, including Debian, Ubuntu and even Arch Linux. Feel free to see the full list here.

To use it, just download and install the tool on your machine/server. Then, in order to use it in a .NET application (i.e. .NET Core) you’ll need a wrapper, such as the Wkhtmltopdf.NetCore NuGet Package. Install it using the following command in your package manager console:

Install-Package Wkhtmltopdf.NetCore -Version 3.0.2

Usage:

var html = await _engine.RenderViewToStringAsync(View, model);
var byteArray = GetPDF(html);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");

You can find the entire example in this GitHub repo. Or in this short YouTube video that captures the essence of what’d you potentially need.

Finally, this blog post couldn’t be finished without the following honorable mentions:

  • JSReports
  • IronPdf
  • NReco
  • PDF Sharp Core
  • iText Sharp
  • Select PDF
    PDFSharpCore
  • DomPDF
  • Aspose
  • Gembox document
  • CefSharp
  • Syncfusion
  • PDF

If you want more content like this, sign up to my weekly emails with posts just like this. No spam, unsubscribe anytime.