Set the resolution of fallback pictures in swatches.pdf from the example zip, not the page size.
dotnet add package RecursiveAI.OnePdf.HtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf.Html; PdfHtml.Convert("swatches.pdf", "swatches.html", new() { Mode = PdfHtmlMode.FixedLayout, RasterDpi = 288 });
Writes swatches.html with the fallback rendered at 288 DPI.
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
RasterDpi defaults to 144 DPI and accepts 36–600. The converter uses fallback pictures for
features the browser can’t express, such as certain shadings and transparency groups.
SVG graphics are unaffected.
The full program
swatches.pdf has one 240 × 200 point page titled Overlapping swatches. Its red and blue rectangles overlap in a transparency group where the later shape replaces earlier paint. That group triggers the picture fallback. The program exports the PDF at two resolutions and prints the CSS page size and converter diagnostic.
using OnePdf.Html; using System.Text.RegularExpressions; Export(144, "swatches-144.html"); Export(288, "swatches-288.html"); static void Export(int dpi, string output) { var options = new PdfHtmlOptions { Mode = PdfHtmlMode.FixedLayout, RasterDpi = dpi, }; var result = PdfHtml.Convert("swatches.pdf", output, options); string html = File.ReadAllText(output); var page = Regex.Match(html, @"width:([\d.]+px);height:([\d.]+px)"); Console.WriteLine($"{output}: page {page.Groups[1]} × {page.Groups[2]}"); foreach (var diagnostic in result.Diagnostics) if (diagnostic.Code.Length != 0) Console.WriteLine(diagnostic.Code); }
swatches-144.html: page 320px × 266.667px raster-fallback swatches-288.html: page 320px × 266.667px raster-fallback
The embedded picture is 480 × 400 pixels at 144 DPI and 960 × 800 at 288 DPI; the HTML page box stays the same size. Values outside 36–600 throw ArgumentOutOfRangeException when conversion starts.
Variations
To enlarge the displayed page without increasing the fallback DPI, add Zoom to the options initializer while keeping RasterDpi = 288:
Zoom = 1.5,The same PDF now has a 480 × 400 CSS-pixel page box.
Related recipes
The short answer
Set PdfHtmlOptions.RasterDpi before calling PdfHtml.Convert in fixed-layout mode. The default is 144 DPI (dots per inch). Changing it changes the resolution of fallback pictures, not the size of the page box on screen. Use Zoom when you want to change the page box and everything inside it.
How do I set the DPI for PDF graphics exported as HTML?
First distinguish pictures produced during conversion from graphics the browser can already draw. In fixed layout, ordinary paths and many other marks are emitted as SVG. Raising RasterDpi doesn’t redraw SVG at a different resolution or increase the detail available in a source photograph. It controls pictures made for features the browser can’t express in the output, including certain PDF shadings and some transparency groups. The raster-fallback diagnostic tells you when the converter took that path. In semantic mode, the option also sets the resolution for vector figures that become pictures.
Which DPI should I use for the fallback pictures?
Start at the default of 144. Consider raising it if a fallback picture looks too coarse at the size your readers use, or lowering it if the pictures make the HTML too large. Twice the DPI produces twice the width and height in pixels before any cap, so it takes four times as many pixels to represent the same region. The encoded file won’t necessarily grow by the same factor.
A large page can run into MaxRasterPixels even when the requested DPI is valid. The converter then draws the page picture at a lower resolution and reports raster-reduced. If the picture looks smaller or softer than you expected, look for that diagnostic before raising the DPI again.