Recursive AI

Embed a CSS variable font instance in a PDF 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.

An instance is a fixed weight selected from a variable font. OneRender embeds each selected weight as a static TrueType font in the PDF.

Terminal
dotnet add package RecursiveAI.OneRender.Pdf
dotnet add package RecursiveAI.OneRender.Image

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

The Image package is only for the PNG preview in the full program; the snippet needs only the Pdf package. Copy sample.woff2 from the zip next to your project file, or use a TrueType variable WOFF2 renamed to sample.woff2 and adjust the CSS weight range to match it.

C#
using OneRender.Pdf;
 
const string html = """
    <style>
      @font-face { font-family: Sample;
        src: url(sample.woff2);
        font-weight: 400 900 }
      p { font: 28px Sample }
    </style>
    <p style="font-weight:400">Monthly report
    <p style="font-weight:900">Monthly report
    """;
var font = File.ReadAllBytes("sample.woff2");
// A label for local bytes, not a request.
const string origin = "https://example.invalid/";
byte[]? Load(string url) =>
    url == origin + "sample.woff2"
        ? font : null;
using var pdf = File.Create("weights.pdf");
var result = HtmlPdf.ToPdf(html, pdf,
    new HtmlPdfOptions
    { BaseUrl = origin, Resources = Load });
Console.WriteLine(
    $"weights.pdf: {result.PageCount} page(s)");

Writes weights.pdf. The zip includes Schibsted Grotesk and its SIL Open Font License.

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.

Text
weights.pdf: 1 page(s)

The full program

C#
using OneRender.Pdf;
using OneRender.Image;
 
const string html = """
    <style>
      @font-face { font-family: Sample;
        src: url(sample.woff2); font-weight: 400 900 }
      body { margin: 20px }
      p { font: 28px Sample; margin: 0 0 8px }
    </style>
    <p style="font-weight: 400">Monthly report</p>
    <p style="font-weight: 900">Monthly report</p>
    """;
var font = File.ReadAllBytes("sample.woff2");
// A label for local bytes, not a request.
const string baseUrl = "https://example.invalid/";
byte[]? Load(string url) =>
    url == baseUrl + "sample.woff2" ? font : null;
using var pdf = File.Create("weights.pdf");
var result = HtmlPdf.ToPdf(html, pdf,
    new HtmlPdfOptions { BaseUrl = baseUrl, Resources = Load });
Console.WriteLine($"weights.pdf: {result.PageCount} page(s)");
foreach (var diagnostic in result.Diagnostics)
    Console.WriteLine(diagnostic.Message);
var preview = HtmlRender.ToImage(html, new HtmlRenderOptions
{
    BaseUrl = baseUrl, Resources = Load, Width = 280, Height = 120
});
File.WriteAllBytes("weights.png", preview.ToPng());
Console.WriteLine("Wrote weights.png");
Text
weights.pdf: 1 page(s)
Wrote weights.png

Open weights.pdf and weights.png: both show “Monthly report” in regular and heavy weights. The PDF text remains selectable.

Monthly report at weight 400 and at weight 900, rendered from the same font file.

If your font falls back or doesn’t embed, read result.Diagnostics; CFF2 variable outlines aren’t embeddable in this version.

Variations

Set the weight axis directly. Add this rule to the stylesheet to render the second paragraph at axis value 650 even though its font-weight is 900:

CSS
p:last-of-type { font-variation-settings: "wght" 650 }

The short answer

Declare the variable font with an @font-face weight range and supply its bytes in HtmlPdfOptions.Resources. Use font-weight on the text to pick an instance, meaning the outlines and widths for one point in the font’s range. HtmlPdf.ToPdf embeds the selected TrueType instances; the reader doesn’t need the original WOFF2 file.

How do I use a CSS variable font with a range of weights?

There are two font-weight declarations with different jobs. The one inside @font-face advertises what the face can serve. The one on each paragraph requests a weight. Without a range on the face, OneRender treats the face as one weight (400 if omitted) and can synthesize bold instead of choosing a heavier instance. Match the range to the actual file; for this Schibsted Grotesk sample, that’s 400 to 900. OneRender clamps the request to the declared range and then to the font’s weight axis.

Use the high-level CSS property for ordinary weight selection. When you need a particular axis value, font-variation-settings runs last and wins for that axis. An unknown axis tag is ignored. Setting font-optical-sizing matters only if the file has an optical-size axis; it won’t have an effect on this weight-only example. The fonts and text documentation lists the order for weight, width, slant, optical size and explicit overrides.

Can I embed a variable font in a PDF without rasterizing the text?

Yes, for variable faces with embeddable TrueType outlines. OneRender writes a separate static TrueType font program for each used instance, so the PDF stores the glyphs and advances for weight 400 and weight 900, not a font the viewer has to vary. The PNG preview uses the same HTML and font resolver, but it’s just a separate image of the weights. It isn’t inserted into the PDF in place of the text.

If the output still looks like a fallback font, check which URL Resources receives. It’s absolute after resolution against BaseUrl; the example’s URL is a label for a local byte array, not a network request. It’s also possible to load a font for rendering that can’t be embedded in the PDF: the pinned font documentation identifies CFF2 variable instances as one such case.

All onerender recipes