Convert HTML to PDF in C#

HtmlToPdfConverter turns HTML into a PDF without a browser: it builds a Word document and renders that. What comes through is what Word can express, so text, tables, lists and images survive, and flexbox and grid layouts do not.

using DocToolkit;

string html = "<h1>Invoice 2026-114</h1><p>Due on receipt</p>";
byte[] pdf = await HtmlToPdfConverter.ConvertAsync(html);
File.WriteAllBytes("output.pdf", pdf);

What it needs

  • No browser, headless or otherwise.
  • No Office install, and no LibreOffice.
  • No native files: managed code only.
  • No network at runtime, unless you ask for it.

Good to know

  • Asynchronous: ConvertAsync takes the HTML as a string and returns a Task<byte[]>.
  • Flexbox, grid, floats and absolute positioning are not applied, and unsupported features are dropped silently. The PDF is valid either way.
  • A4 by default; ConvertAsync(html, page) takes a PageSetup for another size.
  • Remote images are off by default. RemoteImageOptions turns them on per call, and by default still refuses loopback and private addresses (AllowPrivateAddresses opts in).

Install

dotnet add package Ank.DocToolkit

NuGetDocumentationGitHub