---
title: "Stream HTML to PDF in C# without buffering pages"
url: https://recursiveai.org/onerender/cookbook/stream-html-to-pdf-with-bounded-memory/
description: "Write HTML to a PDF stream in C# without buffering the finished PDF. See how OneRender streams page output and what it still needs in memory."
version: "1.4.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Pdf) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/stream-html-to-pdf-with-bounded-memory/stream-html-to-pdf-with-bounded-memory.zip)

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.

```shell
dotnet add package RecursiveAI.OneRender.Pdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onerender/cookbook/stream-html-to-pdf-with-bounded-memory/stream-html-to-pdf-with-bounded-memory.zip), then `dotnet run`.

## Variations

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

```csharp
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`.

## Related recipes

[Convert HTML to PDF](https://recursiveai.org/onerender/cookbook/html-to-pdf/) · [Page headers, footers and page numbers](https://recursiveai.org/onerender/cookbook/paged-media/)

---

## 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](https://recursiveai.org/onerender/docs/pdf/) for the stream overload, pagination rules and profile behavior.
