HTML to PDF Converter

The HTML to PDF Converter turns web pages and HTML strings into PDF documents. It renders the HTML with a Chromium engine that ships inside the NuGet package, so HTML5, CSS3, JavaScript, web fonts and SVG come out as in the Chrome browser, and there is no browser to install on the server. The same component converts HTML to images with the HtmlToImage class.

A conversion takes three steps: create an HtmlToPdf object, optionally set its options, and call one of its methods with a URL or an HTML string. The result is a PDF document in memory or in a file. A new converter already produces an A4 document with the page laid out as in a desktop browser, so the options are needed only to change that result or to add features to the document.

This topic describes the packages, the conversion methods, the page layout and the features of the converter, with links to the topics that describe each of them in detail.

NuGet Packages

The component is distributed as a NuGet package for each platform. Each package contains the same .NET Standard 2.0 library, used from .NET 6 to .NET 10 and from .NET Framework 4.6.2 to 4.8.1, and the native rendering engine for its platform.

Platform

HTML to PDF package

Windows x64

HiQPdf.Next.HtmlToPdf.Windows

Windows ARM64

HiQPdf.Next.HtmlToPdf.Windows.Arm64

Linux x64

HiQPdf.Next.HtmlToPdf.Linux

Linux ARM64

HiQPdf.Next.HtmlToPdf.Linux.Arm64

macOS on Apple Silicon

HiQPdf.Next.HtmlToPdf.MacOS

The HiQPdf.Next.Windows and HiQPdf.Next.Linux metapackages reference the HTML to PDF package of their platform together with all the other components of the library. The HiQPdf.Next.HtmlToPdf and HiQPdf.Next metapackages reference both the Windows x64 and the Linux x64 packages, for applications built once and deployed on either platform. Installing and running the packages on each platform is described in Getting Started on Windows, Getting Started on Linux and Getting Started on macOS.

Converting HTML to PDF

The HtmlToPdf class converts a URL, which can also be a local file, or an HTML string. An HTML string is converted with a base URL, used to resolve the relative URLs of the images, style sheets and scripts it references. The PDF document is returned in a memory buffer or saved to a file:

Source

PDF in memory

PDF saved to a file

URL or local file

ConvertUrlToMemory(String)

ConvertUrlToFile(String, String)

HTML string

ConvertHtmlToMemory(String, String)

ConvertHtmlToFile(String, String, String)

Each method has an asynchronous variant with the Async suffix, following the Task-based Asynchronous Pattern, with an optional System.ThreadingCancellationToken to cancel the conversion. Converters can run in parallel, one per conversion, as shown in Convert Multiple HTML Pages to PDF in Parallel; how the rendering engine processes are started and reused is described in HTML to PDF Rendering Modes and the Persistent Rendering Engine. The methods and the options of the converter, with the code of the demo, are described in Convert URLs and HTML Strings to PDF.

Page Size and Layout

The size of the PDF pages, the width at which the HTML is laid out and the scale at which it is drawn are set together by a layout method of the converter. A new converter uses FitBrowserWindowToPage(PdfPageSize, PdfPageOrientation, Int32, Boolean) with an A4 page: the page is laid out as in a 1024 pixel browser window and scaled to the page width. HTML templates designed for the paper size use LayoutAtPageWidth(PdfPageSize, PdfPageOrientation, Boolean, String, Double, Boolean), and other methods cover the output of Chrome, receipts and pages as wide as the browser window. The methods and the settings for the usual cases are described in HTML to PDF Page Setup and Scaling.

Features of the Converter

Beyond the conversion itself, the converter adds structure, interactivity and security to the generated document, and controls how the page is loaded before it is converted.

Page structure

Navigation and content

Interactivity, standards and security

Loading the page

See Also