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.
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.
What you use today, what to move to.
| You use | Move to | Code changes |
|---|---|---|
| DinkToPdf | SelectPdf.Compat.DinkToPdf | None: swap the package |
| Haukcode.WkHtmlToPdfDotNet | SelectPdf.Compat.WkHtmlToPdfDotNet | None: swap the package |
| Rotativa, Wkhtmltopdf.NetCore, NReco.PdfGenerator, TuesPechkin | SelectPdf Community Edition (free) or SelectPdf.Universal | Replace the conversion call (sample below) |
| The wkhtmltopdf command line | SelectPdf Community Edition (free) or SelectPdf.Universal | Replace 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 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.
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:
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.
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.
| wkhtmltopdf | SelectPdf |
|---|---|
--page-size A4 | PdfPageSize = PdfPageSize.A4 |
--orientation Landscape | PdfPageOrientation = PdfPageOrientation.Landscape |
--margin-top 10mm | MarginTop = 28 |
--javascript-delay 2000 | ConversionDelay = 2 |
--disable-javascript | JavaScriptEnabled = false |
--print-media-type | CssMediaType = HtmlToPdfCssMediaType.Print |
--grayscale | ColorSpace = PdfColorSpace.GrayScale |
--title "Report" | PdfDocumentInformation.Title = "Report" |
--footer-center "[page] / [topage]" | DisplayFooter = true and a footer text with {page_number} and {total_pages} (guide) |
toc | TableOfContents.Mode = PdfTableOfContentsMode.GeneratePages (SelectPdf.Universal) |
Every option is in the documentation.
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.
Questions, answered.
Something else? Write to support@selectpdf.com with your settings and, if you can, the HTML.