↔WKHTMLTOPDF ALTERNATIVE · C# · .NET

A wkhtmltopdf alternative for .NET.

wkhtmltopdf's repository is archived and its last release was in 2020. If you use DinkToPdf or Haukcode.WkHtmlToPdfDotNet, a package swap keeps your code. For any other wrapper or the command line, here is the SelectPdf code that replaces it, rendered with Chromium.

Program.cs
using SelectPdf.Universal;

HtmlToPdf converter = new HtmlToPdf();
converter.Options.PdfPageSize = PdfPageSize.A4;
converter.Options.MarginTop = 28;     // points: 28 pt is about 10 mm

PdfDocument doc = converter.ConvertUrl("https://example.com");
doc.Save("page.pdf");
doc.Close();

A web page to PDF with SelectPdf, A4 with a 10 mm top margin.

§PICK YOUR PATH

What you use today, what to move to.

You useMove toCode changes
DinkToPdfSelectPdf.Compat.DinkToPdfNone: swap the package
Haukcode.WkHtmlToPdfDotNetSelectPdf.Compat.WkHtmlToPdfDotNetNone: swap the package
Rotativa, Wkhtmltopdf.NetCore, NReco.PdfGenerator, TuesPechkinSelectPdf Community Edition (free) or SelectPdf.UniversalReplace the conversion call (sample below)
The wkhtmltopdf command lineSelectPdf Community Edition (free) or SelectPdf.UniversalReplace the process call (sample below)

The compatibility packages are open source under the MIT license, on GitHub. They keep the wrapper's namespaces and types, scale CSS sizes the way wkhtmltopdf's smart shrinking did, and place headers and footers where wkhtmltopdf put them.

§CONVERT WITH SELECTPDF

Convert HTML to PDF with SelectPdf.

Install one package for the platform you run on. This is the free Community Edition for Linux x64; the others are SelectPdf.HtmlToPdf.Windows, SelectPdf.HtmlToPdf.Linux.Arm64 and SelectPdf.HtmlToPdf.MacOS (Apple Silicon), and SelectPdf.Universal.Windows, .Linux, .Linux.Arm64 and .MacOS for the full library.

terminal
dotnet add package SelectPdf.HtmlToPdf.Linux

The sample at the top converts a web page by URL. ConvertHtmlString converts HTML you build in code, the way wkhtmltopdf read it from a file or standard input:

Program.cs
using SelectPdf.Universal;

HtmlToPdf converter = new HtmlToPdf();

// the base URL resolves relative images, styles and links in the HTML
string html = "<h1>Invoice #1042</h1><p>Issued to Contoso Ltd.</p>";
PdfDocument doc = converter.ConvertHtmlString(html, "https://example.com/");
doc.Save("invoice.pdf");
doc.Close();

Both editions use the SelectPdf.Universal namespace, so moving from the free edition to the full library needs no code change. The HTML to PDF in C# guide walks through a complete program.

§WKHTMLTOPDF OPTIONS

Common wkhtmltopdf options in SelectPdf.

Each option is a property of converter.Options. SelectPdf margins are in points (1 mm is about 2.83 pt) and delays in seconds.

wkhtmltopdfSelectPdf
--page-size A4PdfPageSize = PdfPageSize.A4
--orientation LandscapePdfPageOrientation = PdfPageOrientation.Landscape
--margin-top 10mmMarginTop = 28
--javascript-delay 2000ConversionDelay = 2
--disable-javascriptJavaScriptEnabled = false
--print-media-typeCssMediaType = HtmlToPdfCssMediaType.Print
--grayscaleColorSpace = PdfColorSpace.GrayScale
--title "Report"PdfDocumentInformation.Title = "Report"
--footer-center "[page] / [topage]"DisplayFooter = true and a footer text with {page_number} and {total_pages} (guide)
tocTableOfContents.Mode = PdfTableOfContentsMode.GeneratePages (SelectPdf.Universal)

Every option is in the documentation.

§LINUX · DOCKER · ARM64

Linux, Docker and ARM64.

The engine package bundles the Chromium libraries, so a Docker image built from the official .NET images needs no apt-get lines and no wkhtmltopdf files. Linux x64 and ARM64 each have their own package. Linux needs glibc 2.30 or later (Debian 11+, Ubuntu 20.04+, RHEL 9+); on Alpine, the conversions run in the SelectPdf engine container instead.

Step by step: HTML to PDF in Docker on Linux and HTML to PDF on Alpine Linux with the remote engine.

?FAQ

Questions, answered.

Something else? Write to support@selectpdf.com with your settings and, if you can, the HTML.

No. Its GitHub repository is archived, and its last release, 0.12.6, dates from June 2020. It renders with an old Qt WebKit that does not support flexbox, grid or current JavaScript.
DinkToPdf and Haukcode.WkHtmlToPdfDotNet. SelectPdf.Compat.DinkToPdf and SelectPdf.Compat.WkHtmlToPdfDotNet keep their namespaces and types, so existing converter code builds as it is. For Rotativa, Wkhtmltopdf.NetCore, NReco.PdfGenerator, TuesPechkin and the command line, replace the conversion call with the SelectPdf code on this page.
Yes. The SelectPdf Community Edition and the compatibility packages built on it are free for commercial use for documents up to 5 pages. Longer documents need a SelectPdf license, which also covers the full SelectPdf.Universal library.
No. The engine package bundles the Chromium libraries it needs, so a Docker image built from the official .NET images needs no apt-get lines. Linux needs glibc 2.30 or later (Debian 11+, Ubuntu 20.04+, RHEL 9+).
SelectPdf.Universal and the Community Edition do, from version 26.5, through the remote engine: the application sets RenderingEngine.ChromiumRemote and the conversions run in the ghcr.io/selectpdf/engine container. The native engine packages need glibc, which Alpine doesn't have.
No. SelectPdf renders with its own Chromium-based engine. Nothing from wkhtmltopdf is installed or loaded.