Recursive AI

Set raster DPI for HTML-to-PDF shadows 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 RasterDpi for the shadow image; the default is 192 DPI. The heading stays selectable PDF text.

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 = "<h1 style='text-shadow:"
    + "2px 2px 2px #888'>Invoice</h1>";
using var file = File.Create("shadow.pdf");
var result = HtmlPdf.ToPdf(html, file, new()
{
    Output = new() { RasterDpi = 300 }
});
Console.WriteLine($"Pages: {result.PageCount}");

Writes shadow.pdf from the inline HTML and prints:

Text
Pages: 1

RasterDpi must be between 24 and 600; a value outside that range throws ArgumentOutOfRangeException.

Unzip the example, then dotnet run.

Variations

To request 96 DPI instead, replace the render call with this version. It writes the same one-page layout with fewer pixels in the embedded shadow image.

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

The program still prints Pages: 1.

Inspect HTML-to-PDF diagnostics · Render CSS box shadows to an image


The short answer

Set PdfOutputOptions.RasterDpi inside HtmlPdfOptions.Output before calling HtmlPdf.ToPdf. The example requests 300 dots per inch for the CSS text shadow behind “Invoice.” The heading itself remains PDF text.

How do I set 300 DPI when converting HTML to PDF in C#?

Set RasterDpi = 300 to make OneRender sample effects that need rasterization at that resolution. The engine calls these pieces raster islands: it draws the effect into an image and places the result back in its original position on the page. It doesn’t convert the page to a 300 DPI image or change the page’s physical dimensions. If you need a 300 DPI image of the whole page, that’s a separate rendering step.

For the example, the pixels belong to the shadow, not the text. Changing the DPI doesn’t change the font size or the layout, and you can still select or search for “Invoice.” PDF-native content such as text, paths and supported gradients doesn’t get resampled. The same option can affect other rasterized effects, such as inline SVG or filters other than opacity.

Why doesn’t a blurred box shadow use the requested DPI?

A shadow without blur can be drawn as a PDF shape. For a blurred box shadow, OneRender can sample below the requested DPI because a wide blur has little fine detail. The box edge is still clipped by a vector shape. So increasing RasterDpi doesn’t necessarily add pixels to a wide, soft box shadow.

There’s also a pixel budget: at most 16 million pixels per island and 400 million per document. A large island may be drawn at reduced resolution, and an effect that exceeds the document budget can be skipped. Check HtmlPdfResult.Diagnostics when an effect looks different or is missing. The example’s text shadow is rasterized without a diagnostic on a successful write. See the PDF output docs for which effects are rasterized and how the writer handles them.

All onerender recipes