ZingPDF logo

Guide

How to convert HTML to PDF in C#

Add ZingPDF.FromHTML, pass either an HTML string or a URL to Converter.ToPdfAsync(...), then copy the returned stream to the destination you control.

The package renders with browser automation, so deployment planning includes the Chromium runtime and fonts required by the HTML.

Install the HTML package

dotnet add package ZingPDF.FromHTML

Convert an HTML string

Use the string overload when your application has already rendered the markup.

using ZingPDF.FromHTML;

await using var pdfStream = await Converter.ToPdfAsync("""



  

Invoice INV-1001

Rendered through Chromium.

"""); await using var output = File.Create("invoice.pdf"); await pdfStream.CopyToAsync(output);

Convert a URL

Use the URL overload when the HTML is served by an application route, static site, or report endpoint.

using ZingPDF.FromHTML;

await using var pdfStream = await Converter.ToPdfAsync(
    new Uri("https://example.com/report"),
    new NavigationOptions
    {
        TimeoutExpiration = 60_000,
        WaitUntilFlags = WaitUntil.Networkidle0
    });

await using var output = File.Create("report.pdf");
await pdfStream.CopyToAsync(output);

Navigation options

NavigationOptions controls URL navigation. The default timeout is 30 seconds. The wait flags map to browser navigation states such as Load, DOMContentLoaded, Networkidle0, and Networkidle2.

var options = NavigationOptions.WaitUntil(WaitUntil.DOMContentLoaded);
options.TimeoutExpiration = 15_000;

Deployment notes

  • The HTML package is separate from the core ZingPDF package because browser rendering has additional runtime requirements.
  • The string overload sets the supplied HTML as page content. The URL overload navigates to the address and waits for browser font readiness before returning the PDF stream.
  • The returned stream is positioned for copying and belongs to the caller.
  • Use ZingPDF.Templates.LiquidHtml for HTML rendered from Liquid variables, loops, and conditionals before conversion.

Next step

For model-driven HTML, see How to create a PDF from a Liquid HTML template in C#.