Recursive AI

Stream HTML to PDF in C# without buffering pages

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Pass a stream and leave PDF/A and PDF/UA profiles off (PdfProfile.None): those profiles buffer the whole PDF. HTML layout still stays in memory.

Terminal
dotnet add package RecursiveAI.OneRender.Pdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneRender.Pdf;
 
const string html = "<p>First page</p>"
    + "<p style='break-before:page'>"
    + "Second page</p>";
using var file = File.Create("report.pdf");
var result = HtmlPdf.ToPdf(html, file, new()
{
    Output = new() { Profile = PdfProfile.None }
});
Console.WriteLine($"Pages: {result.PageCount}");

Writes report.pdf from the inline HTML and prints:

Text
Pages: 2

The example sets Profile = PdfProfile.None explicitly for clarity; it’s already the default.

If the document exceeds MaxPages (100,000 by default), HtmlPdf.ToPdf throws PdfRenderException.

Unzip the example, then dotnet run.

Variations

Set a page limit if you need to reject unexpectedly long output. Replace the call in the example with:

C#
var result = HtmlPdf.ToPdf(html, file, new()
{
    Output = new() { MaxPages = 20 }
});

Profile defaults to None, so writing still streams. The sample stays under the limit and prints Pages: 2.

Convert HTML to PDF · Page headers, footers and page numbers


The short answer

Pass a writable Stream as the second argument to HtmlPdf.ToPdf and keep Output.Profile at PdfProfile.None. The call writes report.pdf directly to a FileStream and returns the page count and diagnostics, not a byte array. It doesn’t accumulate finished PDF pages in memory.

How do I convert HTML to a PDF stream in C#?

Use the overload that takes a stream, not HtmlPdf.ToPdf(html, options), which returns the entire PDF as byte[]. The destination matters too: a MemoryStream keeps those same output bytes in memory. Use a file stream if you want the PDF to land on disk; open or share the file only after the call has finished and the file stream has been disposed.

The second paragraph in the example has break-before:page, so it starts on its own page. The same call applies print CSS, including @page settings and repeated table headers. You don’t have to split the HTML into one string per PDF page.

Does streaming HTML to PDF keep memory use flat?

It’s the PDF writing that streams. The HTML layout and the display list (the drawing instructions for the whole document) still live in memory. A long table can therefore take substantial memory even if no byte[] ever holds the complete PDF. MaxPages controls how many pages the renderer accepts; it isn’t a memory cap. The API also takes a complete HTML string, not a stream of HTML chunks.

PDF/UA-1 and PDF/A-2a are different: when you request either profile or both together, OneRender first builds the PDF in memory, then re-saves it with the profile identification. Passing a stream doesn’t remove that step. Tagged output alone doesn’t require a profile: tags are enabled by default even when Profile is None.

See PDF output for the stream overload, pagination rules and profile behavior.

All onerender recipes