---
title: "Compress HTML-to-PDF output in C#"
url: https://recursiveai.org/onerender/cookbook/compress-html-to-pdf-output/
description: "Set Compress on OneRender’s HTML-to-PDF output in C#. Compare the same text-heavy document with compression on and off without changing raster DPI."
version: "1.4.0"
---

# Compress HTML-to-PDF output in C#

**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/compress-html-to-pdf-output/compress-html-to-pdf-output.zip)

Set `Output.Compress = true` when rendering. It’s already on by default.

```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>Report</p>";
using var file = File.Create("report.pdf");
HtmlPdf.ToPdf(html, file, new()
{
    Output = new()
    {
        Compress = true, LeaveOpen = true
    }
});
Console.WriteLine($"{file.Length} bytes");
```

Writes `report.pdf` and prints:

```text
5344 bytes
```

Unzip [the example](https://recursiveai.org/onerender/cookbook/compress-html-to-pdf-output/compress-html-to-pdf-output.zip), then `dotnet run`.

## The full program

The download renders 60 copies of the same paragraph in each PDF. Only `Compress` changes between writes; raster DPI stays at 192.

```csharp
using OneRender.Pdf;

const string paragraph = "<p>Quarterly report: all accounts reconciled.</p>";
string html = "<h1>Quarterly report</h1>" +
    string.Concat(Enumerable.Repeat(paragraph, 60));

WritePdf("compressed.pdf", compress: true);
WritePdf("uncompressed.pdf", compress: false);

void WritePdf(string path, bool compress)
{
    using var file = File.Create(path);
    var result = HtmlPdf.ToPdf(html, file, new()
    {
        Output = new() { Compress = compress, RasterDpi = 192, LeaveOpen = true }
    });
    Console.WriteLine($"{path}: {result.PageCount} pages, {file.Length} bytes");
}
```

`dotnet run` prints:

```text
compressed.pdf: 3 pages, 14036 bytes
uncompressed.pdf: 3 pages, 45613 bytes
```

`LeaveOpen = true` keeps the stream open so the program can read its `Length`. Otherwise `HtmlPdf.ToPdf` closes it and reading `Length` throws `ObjectDisposedException`.

## Related recipes

[Convert HTML to PDF](https://recursiveai.org/onerender/cookbook/html-to-pdf/) · [Set raster DPI for HTML-to-PDF shadows](https://recursiveai.org/onerender/cookbook/set-raster-dpi-for-html-pdf-effects/)

---

## The short answer

Set `Output = new() { Compress = true }` in the options passed to `HtmlPdf.ToPdf`. That is already the default. To see its effect for your own template, render it twice and set `Compress = false` for the second render. Keep the HTML, fonts, page options, and raster DPI the same.

## How can I compress HTML to PDF?

Apply compression at output, not by stripping whitespace from the HTML. The `Compress` flag tells the PDF writer to use Flate compression on content and other streams. A page content stream contains the instructions that draw the page, including where to place text. [Flate](https://www.rfc-editor.org/rfc/rfc1951) encodes those bytes losslessly; it doesn’t turn the text into a picture or change where the line breaks fall.

The sample repeats text to make the effect easy to see: both files have three pages, and the file with stream compression enabled occupies fewer bytes. The difference depends on the input. Don’t use that ratio to estimate the size of a report with photos or graphics. Also, `Compress = false` isn’t a promise that every stream in the PDF will be uncompressed; it turns off the writer’s optional stream compression, not every use of a PDF filter.

## Does lowering DPI also compress the PDF?

It’s a separate control. `RasterDpi` sets how many pixels OneRender creates for effects it needs to draw as images, such as blurred shadows. Lowering it can reduce the amount of image data for those effects, but it also changes how much detail those rasterized areas contain. `Compress` changes how stream bytes are stored. It does not choose the pixel dimensions of a shadow or change the resolution of vector text.

Start with the stream setting left on. If your large template contains rasterized effects, vary `RasterDpi` in a separate experiment and examine those areas at the size readers will view or print them. The text-only comparison above keeps DPI fixed so the file size difference is attributable to stream compression.
