Recursive AI

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.

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

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

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

Convert HTML to PDF · Set raster DPI for HTML-to-PDF shadows


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

All onerender recipes