HtmlConverter class
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 HtmlConverterInheritance
Examples
Section titled “Examples”byte[] pdf = HtmlConverter.ConvertToPdf("<h1>Invoice</h1><p>Total: 42 EUR</p>");
PdfDocument doc = HtmlConverter.ConvertFileToDocument("report.html");
doc.Save("report.pdf");
Methods
Section titled “Methods”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)Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”html string
The HTML. Malformed markup is repaired as browsers repair it.
options HtmlConverterOptions?
Page size, fonts and how resources load; defaults when null.
Returns
Section titled “Returns”Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”html string
The HTML.
options HtmlConverterOptions?
Page size, fonts and how resources load; defaults when null.
Returns
Section titled “Returns”byte[]
Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”html string
The HTML.
output Stream
Where the PDF is written.
options HtmlConverterOptions?
Page size, fonts and how resources load; defaults when null.
Exceptions
Section titled “Exceptions”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)Parameters
Section titled “Parameters”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.
Returns
Section titled “Returns”Remarks
Section titled “Remarks”Layout runs on a thread of its own, as ConvertToDocument does, and the PDF is then saved with SaveAsync, with SaveOptions.
Exceptions
Section titled “Exceptions”The document needs more pages than MaxPages.
Used in
Section titled “Used in”- Run PdfNetStack on AWS Lambda
- Run PdfNetStack on Azure Functions
- Fonts and text in PDF with C# and .NET
- HTML to PDF in C# without a browser
- CSS support in PdfNetStack.Html
- Fonts, images and resources in HTML to PDF
- Page headers, footers and numbers in HTML
- Work with large PDF files in C#
- Create PDF/A files in C# and .NET
- Convert an HTML invoice to PDF in C#