Docs / Guides

HTML to PDF

HtmlConverter turns HTML and CSS into PDF pages. The HTML parser, the CSS engine and the layout engine are SoftZenPdf's own, with no browser and no dependencies, so it runs anywhere .NET runs.

Converting a document

Pass an HTML document (or a fragment) to Convert. The document's title becomes the PDF title, headings become bookmarks, and links stay clickable. Warnings lists anything that could not be rendered exactly, so you can check a template once:

Program.cs
using SoftZenPdf;
using SoftZenPdf.Html;

var html = """
    <html>
    <head>
      <title>Invoice 1042</title>
      <style>
        @page { size: A4; margin: 2cm }
        body { font-family: Helvetica, sans-serif; font-size: 11pt }
        h1 { color: #1a3c7a }
        table { width: 100%; border-collapse: collapse }
        th { background: #1a3c7a; color: white; text-align: left; padding: 4px }
        td { border-bottom: 1px solid #ccc; padding: 4px }
        .amount { text-align: right }
      </style>
    </head>
    <body>
      <h1>Invoice 1042</h1>
      <p>Thank you for your order. <a href="https://example.com/terms">Payment terms</a> apply.</p>
      <table>
        <thead><tr><th>Item</th><th class="amount">Price</th></tr></thead>
        <tr><td>Consulting</td><td class="amount">1,200.00</td></tr>
        <tr><td>Support</td><td class="amount">300.00</td></tr>
      </table>
    </body>
    </html>
    """;

var converter = new HtmlConverter();
var pdf = converter.Convert(html);
pdf.Save("invoice.pdf");

Console.WriteLine($"{pdf.Info.Title}: {pdf.Pages.Count} page(s), {converter.Warnings.Count} warning(s)");
Output
Invoice 1042: 1 page(s), 0 warning(s)

ConvertFile converts a file and reads its images and style sheets from its folder; RenderTo appends the pages to an existing document; ToFlowDocument returns the laid-out content as a FlowDocument you can change before it is drawn.

Images, style sheets and fonts

Relative paths in img, link rel="stylesheet", @import and @font-face are read from BaseDirectory, and only from inside it, so untrusted HTML cannot pull in other files. data: URIs always work. Nothing is fetched from the network unless you supply a ResourceLoader. Fonts come from the options' FontProvider: register font files, or add the fonts installed on the computer:

Program.cs
using SoftZenPdf;
using SoftZenPdf.Html;

var options = new HtmlConverterOptions
{
    // Relative paths (images, style sheets, @font-face fonts) are read from this folder, and only from it.
    BaseDirectory = ".",
};
// Use the fonts installed on this computer for font-family names such as Arial or Segoe UI.
options.Fonts.AddSystemFonts();

var converter = new HtmlConverter(options);
var pdf = converter.Convert("""
    <h1>Colour chart</h1>
    <p><img src="color-chart.jpg" style="width: 50%"></p>
    <p style="font-family: 'Noto Sans'">Rendered with the fonts on this computer.</p>
    """);
pdf.Save("html-images.pdf");

Without registered fonts, text uses the 14 standard fonts: Arial and sans-serif become Helvetica, Times New Roman and serif Times, monospace Courier. They cover Western European characters only; register TrueType fonts for other scripts.

What is supported

  • HTML: HTML5 documents parsed forgivingly, as browsers do (implied end tags, unclosed elements, entities).
  • CSS: style elements, linked style sheets, @import, style attributes and presentational attributes; the cascade with specificity, !important and inheritance; @media print; selectors with classes, ids, attributes, the four combinators and pseudo-classes such as :first-child, :nth-child(), :not() and :is(); ::before and ::after with content.
  • Boxes: margins, padding, borders and background colours; width and max-width; centring with margin: auto.
  • Text: fonts and font lists, sizes in every common unit, bold and italic, colour, text-align (including justify), text-indent, line-height, white-space, text-decoration, text-transform, vertical-align super and sub, direction: rtl.
  • Lists: bullets, numbers, letters and Roman numerals, start, value and reversed, nested lists.
  • Tables: automatic, fixed and percentage column widths, colspan and rowspan, thead rows repeated on every page, cell padding, borders, backgrounds and vertical alignment; CSS tables (display: table).
  • Images: JPEG, PNG, GIF, BMP and TIFF, sized by attributes or CSS.
  • Pages: @page size and margins, break-before, break-after and break-inside (and the older page-break-*), headings kept with the text that follows.

What is not supported

  • JavaScript: pages are converted as served, so content built by scripts is missing.
  • Flexbox and grid are laid out as ordinary blocks; floats and absolute or fixed positioning are laid out in the normal flow.
  • Transforms, gradients, background images, box shadows, rounded corners and opacity.
  • SVG and canvas, iframes, video and audio; form controls show their value as text.
  • Web fonts in WOFF and WOFF2 formats (use .ttf files); CSS counters; ::first-letter and ::first-line.

Each of these is reported in Warnings when a document uses it, instead of failing the conversion.

API: HtmlConverter, HtmlConverterOptions.