---
title: "Convert TTF or OTF to WOFF2 or WOFF in C#"
url: https://recursiveai.org/onefont/cookbook/convert-ttf-otf-to-woff2/
description: "Pack a single TTF or OTF file as WOFF2 or WOFF with onefont. Run the C# example with the included TrueType font or pass your own OpenType file."
version: "1.6.0"
---

# Convert TTF or OTF to WOFF2 or WOFF in C#

**onefont** is a free, open-source font and text-shaping library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneFont) · [GitHub](https://github.com/recursive-ai-oss/onefont-csharp) · [Docs](https://recursiveai.org/onefont/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/cookbook/convert-ttf-otf-to-woff2/convert-ttf-otf-to-woff2.zip)

Pack one TTF or OTF for the web. `NotoSans-Regular.ttf` is included in the example ZIP. onefont writes WOFF and WOFF2 but doesn’t read them; start with the original TTF or OTF.

```shell
dotnet add package RecursiveAI.OneFont
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneFont;

const string name = "NotoSans-Regular";
byte[] font = File.ReadAllBytes(name + ".ttf");
File.WriteAllBytes(name + ".woff2",
    WebFont.ToWoff2(font));
File.WriteAllBytes(name + ".woff",
    WebFont.ToWoff(font));
Console.WriteLine(
    $"Wrote {name}.woff2 and {name}.woff");
```

[The example](https://recursiveai.org/onefont/cookbook/convert-ttf-otf-to-woff2/convert-ttf-otf-to-woff2.zip) 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`.

If you paste the snippet into a new project, copy a `.ttf` next to `Program.cs` as `NotoSans-Regular.ttf` or change `name` to your font’s filename without `.ttf`.

```text
Wrote NotoSans-Regular.woff2 and NotoSans-Regular.woff
```

## The full program

```csharp
using OneFont;

string input = args.Length == 0 ? "NotoSans-Regular.ttf" : args[0];
string name = Path.GetFileNameWithoutExtension(input);
byte[] sfnt = File.ReadAllBytes(input);
byte[] woff2 = WebFont.ToWoff2(sfnt);
byte[] woff = WebFont.ToWoff(sfnt);
File.WriteAllBytes(name + ".woff2", woff2);
File.WriteAllBytes(name + ".woff", woff);
Console.WriteLine($"Wrote {name}.woff2 and {name}.woff");
```

```text
Wrote NotoSans-Regular.woff2 and NotoSans-Regular.woff
```

The program takes an optional path and writes both files to the current directory. Both methods throw `FontFormatException` for a collection (`.ttc` or `.otc`) or an invalid table directory. Pass one standalone font, not an existing web font.

## Variations

**Convert an OTF file.** Pass the path to any standalone `.otf` to the full program:

```shell
dotnet run -- MyFont.otf
```

```text
Wrote MyFont.woff2 and MyFont.woff
```

If you don’t have an OTF handy, build `Triangle.otf` with the [glyph outline example](https://recursiveai.org/onefont/cookbook/build-opentype-font-from-glyph-outlines/) and pass that file instead.

## Related recipes

- [Subset a font to the characters on a web page](https://recursiveai.org/onefont/cookbook/subset-font-to-characters-used-on-a-web-page/)
- [Read a font file](https://recursiveai.org/onefont/cookbook/read-font-file/)

---

## The short answer

Read a standalone TTF or OTF into a byte array and pass it to `WebFont.ToWoff2` or `WebFont.ToWoff`. Save the returned bytes with the matching `.woff2` or `.woff` extension. Both methods pack the font as-is rather than selecting characters, drawing outlines, or changing a CFF outline into a TrueType outline.

## What is the difference between WOFF and WOFF2 font formats?

Both are web containers for OpenType font data. The [onefont docs](https://recursiveai.org/onefont/docs/building/) explain how these two methods pack the source: `ToWoff2` puts the tables in one Brotli stream and uses the null transform for the TrueType `glyf` and `loca` tables; `ToWoff` compresses each table separately with zlib. WOFF2 omits the `DSIG` table and sets the required flag in `head`. The outline format inside the container is still the one you started with.

Use WOFF2 if that’s what your supported browsers load. If you also need to support a browser that understands WOFF but not WOFF2, generate both from the original TTF or OTF. Don’t pass the `.woff2` to `ToWoff` to generate the fallback. These are encoders, not readers for either web format.

## Why use WOFF2 over TTF?

WOFF2 wraps the font for delivery with compression specified by [WOFF File Format 2.0](https://www.w3.org/TR/WOFF2/). You can keep the uncompressed original for future changes and publish the packed copy for the browser. Packing and subsetting solve different problems: packing compresses the stored font data, while subsetting reduces the character mappings and glyphs before you pack. If the font is meant to cover text you don’t know in advance, keep its coverage and pack it without subsetting.

## How do you use a WOFF2 font in CSS?

Use [`@font-face`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face). Put the example’s output files beside the stylesheet. List WOFF2 first and WOFF second so the browser can pick a source it supports. The URLs are relative to the stylesheet.

```css
@font-face {
  font-family: "Site Sans";
  src: url("NotoSans-Regular.woff2") format("woff2"),
       url("NotoSans-Regular.woff") format("woff");
}
body { font-family: "Site Sans", sans-serif; }
```

The name `Site Sans` is the name your stylesheet uses, not a request to rename the font on disk. If you only publish the WOFF2 output, remove the WOFF entry. Before putting either file on a public server, check the source font’s license for web distribution. The ZIP’s Noto Sans input is distributed with its OFL license.
