Soenneker.Pdfs.Html 11.0.118

Prefix Reserved
dotnet add package Soenneker.Pdfs.Html --version 11.0.118
                    
NuGet\Install-Package Soenneker.Pdfs.Html -Version 11.0.118
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Pdfs.Html" Version="11.0.118" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Pdfs.Html" Version="11.0.118" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Pdfs.Html" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Pdfs.Html --version 11.0.118
                    
#r "nuget: Soenneker.Pdfs.Html, 11.0.118"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Pdfs.Html@11.0.118
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Pdfs.Html&version=11.0.118
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Pdfs.Html&version=11.0.118
                    
Install as a Cake Tool

NuGet Build Downloads

Soenneker logo Soenneker.Pdfs.Html

Generate PDFs from HTML with Chromium and Playwright.

The utility keeps one Chromium process alive, gives every render an isolated browser context, and limits concurrency so a burst of PDFs does not overwhelm the host. Chromium is installed automatically on first use.

Installation

dotnet add package Soenneker.Pdfs.Html

Quick start

Register the utility once as a singleton:

using Soenneker.Pdfs.Html.Registrars;

builder.Services.AddHtmlPdfUtilAsSingleton();

Inject IHtmlPdfUtil and generate a file:

using Soenneker.Pdfs.Html.Abstract;

public sealed class InvoiceService
{
    private readonly IHtmlPdfUtil _htmlPdfUtil;

    public InvoiceService(IHtmlPdfUtil htmlPdfUtil)
    {
        _htmlPdfUtil = htmlPdfUtil;
    }

    public ValueTask CreatePdf(string html, string outputPath, CancellationToken cancellationToken = default)
    {
        return _htmlPdfUtil.GenerateToFile(html, outputPath, cancellationToken: cancellationToken);
    }
}

GenerateToFile writes to a temporary sibling file and atomically replaces the destination only after generation succeeds. An existing PDF is preserved if rendering or writing fails.

Choose an output

Return a readable stream:

await using Stream pdf = await htmlPdfUtil.Generate(html, cancellationToken: cancellationToken);

Write to an existing stream:

await htmlPdfUtil.GenerateToStream(html, response.Body, cancellationToken: cancellationToken);

Write or replace a file atomically:

await htmlPdfUtil.GenerateToFile(html, "invoice.pdf", cancellationToken: cancellationToken);

Dispose returned streams promptly; they use pooled memory.

Configure the service

The default limit is four active renders. Additional calls wait asynchronously for a slot. Tune this based on available CPU and memory rather than request volume alone.

builder.Services.AddHtmlPdfUtilAsSingleton(options =>
{
    options.MaxConcurrency = 8;
    options.RenderTimeout = TimeSpan.FromSeconds(45);
    options.LaunchOptions.Headless = true;
});

RenderTimeout starts once the shared browser is ready, so the initial Chromium installation is not counted against it. If Chromium exits unexpectedly, the next request starts a replacement browser.

Configure a document

Use HtmlPdfOptions for settings that vary per PDF:

using Microsoft.Playwright;
using Soenneker.Pdfs.Html.Options;

var options = new HtmlPdfOptions
{
    ContextOptions = new BrowserNewContextOptions
    {
        Locale = "en-US",
        ViewportSize = new ViewportSize
        {
            Width = 1280,
            Height = 720
        }
    },
    ContentOptions = new PageSetContentOptions
    {
        WaitUntil = WaitUntilState.NetworkIdle
    },
    PdfOptions = new PagePdfOptions
    {
        Format = "A4",
        PrintBackground = true,
        Margin = new Margin
        {
            Top = "16mm",
            Right = "12mm",
            Bottom = "16mm",
            Left = "12mm"
        }
    }
};

await htmlPdfUtil.GenerateToFile(html, "invoice.pdf", options, cancellationToken);

The utility waits for document.fonts.ready by default. Set WaitForFonts to false when the document does not use web fonts or manages readiness itself.

HTML and CSS tips

Chromium renders PDFs using print CSS. A small print stylesheet usually makes the largest difference:

<style>
  @page {
    size: A4;
    margin: 16mm 12mm;
  }

  body {
    font-family: Arial, sans-serif;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .page-break {
    break-before: page;
  }
</style>

Keep these behaviors in mind:

  • Enable PrintBackground when backgrounds and colors matter.
  • Use absolute URLs, data: URLs, or a <base href="..."> element for relative assets. HTML is loaded with SetContentAsync, so it has no application URL by default.
  • Embed critical fonts and images when output must be deterministic or the renderer may not have network access.
  • Use Playwright's header and footer template properties in PagePdfOptions for page numbers and repeated content.

Rendering untrusted HTML

JavaScript and external HTTP/HTTPS requests are allowed by default. Disable both when the HTML is not trusted:

var safeOptions = new HtmlPdfOptions
{
    BlockNetworkRequests = true,
    ContextOptions = new BrowserNewContextOptions
    {
        JavaScriptEnabled = false
    }
};

await htmlPdfUtil.GenerateToFile(untrustedHtml, "document.pdf", safeOptions, cancellationToken);

Network blocking prevents remote images, stylesheets, scripts, and fonts from loading. Embedded content and data: resources remain available. Font readiness waiting is skipped automatically when JavaScript is disabled.

Deployment notes

  • The first render is slower because it verifies or installs Chromium. If cold-start latency matters, generate a small warm-up document when the application starts.
  • The process needs write access to the Playwright browser directory used by Soenneker.Playwrights.Installation.
  • Linux hosts may require Chromium system dependencies. The installation utility installs dependencies by default; ensure the runtime user has the necessary permissions or include them in the container image.
  • Some containers running as root require Chromium's --no-sandbox argument. Use it only when the environment requires it, because disabling the browser sandbox reduces isolation.
builder.Services.AddHtmlPdfUtilAsSingleton(options =>
{
    options.LaunchOptions.Args = ["--no-sandbox"];
});
Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.118 119 11/11/2026
11.0.117 70 11/11/2026
11.0.115 65 11/11/2026
11.0.114 72 11/11/2026
11.0.112 78 11/11/2026
11.0.111 64 11/11/2026
11.0.110 88 11/11/2026
11.0.109 77 11/11/2026
11.0.108 217 11/3/2026
11.0.107 104 11/3/2026
11.0.106 89 11/3/2026
11.0.105 92 11/3/2026
11.0.103 94 11/3/2026
11.0.102 95 11/3/2026
11.0.101 92 11/2/2026
11.0.100 115 10/29/2026
11.0.99 120 10/27/2026
11.0.98 99 10/27/2026
11.0.97 103 10/26/2026
11.0.96 98 10/26/2026
Loading failed

Updated Soenneker.Playwrights.Installation to 11.0.255