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:
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)");
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:
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:
styleelements, linked style sheets,@import,styleattributes and presentational attributes; the cascade with specificity,!importantand inheritance;@media print; selectors with classes, ids, attributes, the four combinators and pseudo-classes such as:first-child,:nth-child(),:not()and:is();::beforeand::afterwithcontent. - Boxes: margins, padding, borders and background colours;
widthandmax-width; centring withmargin: 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-alignsuper and sub,direction: rtl. - Lists: bullets, numbers, letters and Roman numerals,
start,valueandreversed, nested lists. - Tables: automatic, fixed and percentage column widths,
colspanandrowspan,theadrows 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:
@pagesize and margins,break-before,break-afterandbreak-inside(and the olderpage-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
.ttffiles); CSS counters;::first-letterand::first-line.
Each of these is reported in Warnings when a document uses it, instead of failing the conversion.