Recursive AI

HTML to PDF CSS support in C#

See how OneRender applies print styles, page size, margins, and page breaks. Use three PDF outputs to tell missing CSS from a layout limit.

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI, licensed MIT or Apache-2.0. NuGet · GitHub

Install: dotnet add package RecursiveAI.OneRender.Pdf --version 1.4.0

This page covers print rules for page size, margins, and page breaks. See OneRender’s layout reference for flexbox, grid, and floats, and its font reference for web fonts.

OneRender uses print media: @media print controls what appears, @page sets paper size and margins, and break-before: page can start a block on a new page.

In a .NET 10 console project, replace Program.cs and run dotnet run.

Program.cs.NET 10
using System.Text;
using OneRender.Pdf;
 
const string css = """
    @page { size: 5in 7in; margin: 0.5in; }
    body { margin: 0; }
    @media print { .screen-only { display: none; } }
    .appendix { break-before: page; }
    """;
const string body = """
    <p>Invoice</p>
    <p class="screen-only">Preview controls</p>
    <p class="appendix">Payment terms</p>
    """;
const string link = """<link rel="stylesheet" href="print.css">""";
 
Render("inline", $"<style>{css}</style>");
Render("missing", link);
Render("supplied", link, url =>
    url == "https://example.invalid/print.css" ? Encoding.UTF8.GetBytes(css) : null);
 
void Render(string name, string head, Func<string, byte[]?>? resources = null)
{
    var html = $"<!doctype html><html lang='en'><head>{head}</head><body>{body}</body></html>";
    using var file = File.Create($"{name}.pdf");
    var result = HtmlPdf.ToPdf(html, file, new HtmlPdfOptions
    {
        BaseUrl = "https://example.invalid/invoice.html",
        Resources = resources
    });
    Console.WriteLine($"{name}: {result.PageCount} page(s)");
}
Console output
inline: 2 page(s)
missing: 1 page(s)
supplied: 2 page(s)
PDF and paper sizeText by page
inline.pdf
360 × 504 pt
1: Invoice; 2: Payment terms
missing.pdf
595.276 × 841.89 pt (A4)
1: Invoice, Preview controls, Payment terms
supplied.pdf
360 × 504 pt
1: Invoice; 2: Payment terms

Those sizes and page contents were checked with pdfinfo and pdftotext. The inline PDF’s text starts 36 pt from the left edge, consistent with the half-inch page margin and zero body margin in the test.

Which CSS rules control the printed pages?

  • Print styles: HtmlPdf.ToPdf lays out for print. Ordinary rules can apply, as can rules inside @media print. Rules limited to @media screen don’t apply to the PDF.
  • Paper size and margins: Put these in @page. HtmlPdfOptions.Page gives defaults where CSS does not set them. A body margin adds space inside the layout; it doesn’t set the paper margin.
  • Page breaks: Use break-before: page on the block that should start a page, or break-after: page on the preceding block. Use break-inside: avoid to ask the renderer to keep a block together, not to make oversized content fit. The legacy page-break-* properties have modern equivalents; for example, page-break-before: always maps to break-before: page.

OneRender can break between block siblings, between lines, and between table rows. It can repeat thead and tfoot, but it won’t split a row’s cells across pages. A row taller than a page overflows even when the print stylesheet loaded correctly. Split that content into smaller rows rather than trying another break declaration inside the cell. The PDF output reference also covers named pages, page-margin boxes, and page counters.

Missing stylesheet or unsupported CSS?

  1. Inline the exact rules first. Reproduce the issue in a small HTML document with a <style> block. If the inline version works but the linked version doesn’t, the property is supported in that test. Check the linked sheet’s media condition and whether the host returned the same bytes.
  2. Check the address the host sees. BaseUrl resolves the relative print.css to an absolute address. It does not fetch the file. In the sample the host returns bytes for that one address; without them, the PDF falls back to A4 and loses the break and print-only hiding together. See how to supply HTML render resources for the host setup.
  3. If inline CSS still fails, isolate one declaration. Check that the selector matches and the rule wins the cascade in print. Then check the feature in the pinned renderer’s docs and test the rule on a simple block. A break between two paragraphs and a break inside a tall table row are different layout cases. Inspect the render diagnostics for reported approximations, but don’t treat a quiet render as proof that every rule took effect.

When choosing a C# converter, test the paper size and the actual page boundaries on one of your templates. A screen preview doesn’t tell you whether a table heading will repeat or a particular section will start on a new sheet. The OneRender getting-started guide covers installing and running the renderer.