Recursive AI

HTML to PDF: set paper size and margins in C#

OnePdf.Html is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

Set the paper size and margins in C#. These settings override the size or margins in the HTML’s CSS @page rule. Scripts in the page don’t run.

Terminal
dotnet add package RecursiveAI.OnePdf.Html.Render

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf.Html.Render;
 
var options = new HtmlToPdfOptions
{
    PageSize = HtmlPageSize.Letter,
    Margins = HtmlMargins.FromInches(1),
};
var pdf = HtmlToPdf.ConvertFile(
    "menu.html", "menu-letter.pdf", options);
Console.WriteLine(
    $"menu-letter.pdf: {pdf.PageCount} page(s)");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

The program writes menu-letter.pdf and prints:

Text
menu-letter.pdf: 1 page(s)

Use the menu.html in the download, or save this file beside Program.cs before running the snippet:

HTML
<!doctype html>
<html lang="en">
<title>Lunch menu</title>
<style>
  @page { size: A5; margin: 10mm; }
  body { margin: 0; }
</style>
<h1>Lunch menu</h1>
<p>Tomato soup and rye bread.</p>

An unreadable HTML file throws HtmlToPdfException with Failure set to Input.

Variations

A4 landscape with different side margins. Replace everything after the using line with:

C#
var options = new HtmlToPdfOptions
{
    PageSize = HtmlPageSize.A4,
    Orientation = HtmlPageOrientation.Landscape,
    Margins = HtmlMargins.FromMillimeters(
        top: 15, right: 20, bottom: 15, left: 20),
};
HtmlToPdf.ConvertFile("menu.html", "wide.pdf", options);

This writes wide.pdf on A4 landscape, with 15 mm at the top and bottom and 20 mm on the left and right.

The short answer

Set PageSize and Margins on HtmlToPdfOptions and pass the options to HtmlToPdf.ConvertFile. The converter uses those settings to lay out the HTML for the new sheet. It isn’t resizing an existing PDF. In the example, the file asks for A5 and a 10 mm margin, but the result is an 8.5 × 11 inch sheet with one inch of space on each side before the content area starts.

Can CSS @page set the PDF size and margins instead?

Yes. Put the size and margins in the document’s @page rule when the template itself should decide the print layout. The sample’s @page rule applies as written if you omit the C# options. With neither an option nor a setting in the HTML, the defaults are A4, portrait and half-inch margins.

If you set only PageSize in C#, the HTML can still supply the margin. If you set only Margins, the HTML can still supply the paper size. The converter appends a print rule for the options you set, so you can share one HTML template across jobs and decide which properties to override when you create each PDF. See the page options.

How do I make an HTML-to-PDF page landscape?

Use Orientation = HtmlPageOrientation.Landscape with the paper size you want. It makes the longer edge the width. For a wide table, that gives the layout more room across the page before it wraps. It does not turn the rendered text sideways. Choose a predefined paper size such as Letter or A4, or use HtmlPageSize.FromInches(width, height) or FromMillimeters(width, height) for a sheet that doesn’t match a preset.

How do I adjust the whitespace around the converted page?

Change Margins for the inset from the paper edges. FromInches(1) sets all four edges. To specify four values, give them in top, right, bottom, left order, as in the landscape example. Use FromInches or FromMillimeters for measurements from a print specification: the bare HtmlMargins constructor and HtmlMargins.All take CSS pixels, not inches or PDF points.

A page margin and the HTML body’s margin are separate. The sample sets body { margin: 0; } so the body doesn’t add its own inset. If text still starts further in than you expected, check the body and its inner elements for extra padding or margins before changing the page setting.

All onepdf-html recipes