Skip to content

HtmlConverter class

View as Markdown

Namespace: PdfNetStack.Html
Assembly: PdfNetStack.Html.dll
Package: PdfNetStack.Html

Converts HTML and CSS to PDF without a browser: the document is parsed as browsers do, styled with the cascade, laid out in CSS boxes and split into pages per the paged media rules.

public static class HtmlConverter

Inheritance

object ← HtmlConverter

byte[] pdf = HtmlConverter.ConvertToPdf("<h1>Invoice</h1><p>Total: 42 EUR</p>");
PdfDocument doc = HtmlConverter.ConvertFileToDocument("report.html");
doc.Save("report.pdf");

ConvertFileToDocument(string, HtmlConverterOptions?)

Section titled “ ConvertFileToDocument(string, HtmlConverterOptions?)”

Converts an HTML file. Relative URLs resolve against the file’s folder, and files in that folder may load, unless options sets its own base or loader.

public static PdfDocument ConvertFileToDocument(string path, HtmlConverterOptions? options = null)

path string

The HTML file, read as UTF-8 unless it starts with a byte order mark.

options HtmlConverterOptions?

Page size, fonts and how resources load; defaults when null.

PdfDocument

HtmlConversionException

The document needs more pages than MaxPages.

ConvertToDocument(string, HtmlConverterOptions?)

Section titled “ ConvertToDocument(string, HtmlConverterOptions?)”

Converts an HTML string to a PDF document.

public static PdfDocument ConvertToDocument(string html, HtmlConverterOptions? options = null)

html string

The HTML. Malformed markup is repaired as browsers repair it.

options HtmlConverterOptions?

Page size, fonts and how resources load; defaults when null.

PdfDocument

HtmlConversionException

The document needs more pages than MaxPages.

ConvertToPdf(string, HtmlConverterOptions?)

Section titled “ ConvertToPdf(string, HtmlConverterOptions?)”

Converts an HTML string to PDF bytes.

public static byte[] ConvertToPdf(string html, HtmlConverterOptions? options = null)

html string

The HTML.

options HtmlConverterOptions?

Page size, fonts and how resources load; defaults when null.

byte[]

HtmlConversionException

The document needs more pages than MaxPages.

ConvertToPdf(string, Stream, HtmlConverterOptions?)

Section titled “ ConvertToPdf(string, Stream, HtmlConverterOptions?)”

Converts an HTML string and writes the PDF to a stream.

public static void ConvertToPdf(string html, Stream output, HtmlConverterOptions? options = null)

html string

The HTML.

output Stream

Where the PDF is written.

options HtmlConverterOptions?

Page size, fonts and how resources load; defaults when null.

HtmlConversionException

The document needs more pages than MaxPages.

ConvertToPdfAsync(string, Stream, HtmlConverterOptions?, CancellationToken)

Section titled “ ConvertToPdfAsync(string, Stream, HtmlConverterOptions?, CancellationToken)”

Converts an HTML string and writes the PDF to a stream, asynchronously: no thread waits while the document is laid out, and output is only written with asynchronous calls, as response bodies in ASP.NET Core and Azure Functions require.

public static Task ConvertToPdfAsync(string html, Stream output, HtmlConverterOptions? options = null, CancellationToken cancellationToken = default)

html string

The HTML.

output Stream

Where the PDF is written, from its current position. It is flushed, not closed.

options HtmlConverterOptions?

Page size, fonts and how resources load; defaults when null.

cancellationToken CancellationToken

Observed before layout starts and while the PDF is saved; layout itself, bounded by MaxPages, runs to its end. When it fires, the call ends with OperationCanceledException and what reached the stream is not a complete file.

Task

Layout runs on a thread of its own, as ConvertToDocument does, and the PDF is then saved with SaveAsync, with SaveOptions.

HtmlConversionException

The document needs more pages than MaxPages.