---
title: "Decompress WOFF2 to a font file in Go"
url: https://recursiveai.org/onefont/go/cookbook/decompress-woff2-to-sfnt/
description: "Unpack WOFF2 in Go and save the result as TTF, OTF, or TTC by reading its font signature instead of guessing from the web font’s name."
version: "1.1.1"
---

# Decompress WOFF2 to a font file in Go

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

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onefont) · [GitHub](https://github.com/recursive-ai-oss/onefont-go) · [Docs](https://recursiveai.org/onefont/go/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/go/cookbook/decompress-woff2-to-sfnt/decompress-woff2-to-sfnt.zip)

Use `UnwrapWebFont` to recover the font inside a WOFF2 file; the first four bytes of the unpacked font tell you whether it’s TTF, OTF, or TTC. The zip includes a Noto Sans web font.

```shell
go get recursiveai.org/go/onefont
```

```go
package main

import (
	"fmt"
	"log"
	"os"

	"recursiveai.org/go/onefont"
)

func main() {
	input := "NotoSans-Subset-Regular.woff2"
	data, err := os.ReadFile(input)
	if err != nil {
		log.Fatal(err)
	}
	font, err := onefont.UnwrapWebFont(data,
		onefont.WithMaxFontSize(8<<20))
	if err != nil {
		log.Fatal(err)
	}

	var ext string
	switch string(font[:4]) {
	case "\x00\x01\x00\x00", "true":
		ext = ".ttf"
	case "OTTO":
		ext = ".otf"
	case "ttcf":
		ext = ".ttc"
	default:
		log.Fatalf("unknown SFNT flavor: %x", font[:4])
	}
	output := "unpacked" + ext
	err = os.WriteFile(output, font, 0644)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Wrote", output)
}
```

```text
Wrote unpacked.ttf
```

`UnwrapWebFont` returns an error for damaged input or a font that exceeds the unpacked-size limit. The program writes no file in either case.

Paste the code above into main.go, run `go mod init example` and `go get recursiveai.org/go/onefont`, then `go run .`; or unzip [the example](https://recursiveai.org/onefont/go/cookbook/decompress-woff2-to-sfnt/decompress-woff2-to-sfnt.zip) and `go run .`.

Requires Go 1.24 or later.

## Variations

**Unpack a CFF web font.** Use an input that contains CFF outlines, such as [the pinned `cff.woff2` example](https://raw.githubusercontent.com/recursive-ai-oss/onefont-go/2a9a35cbfcd2e359c0178abdf2e0beaef4a7213f/testdata/webfont/cff.woff2). Save the raw file beside `main.go` and change the input:

```go
input := "cff.woff2"
```

```text
Wrote unpacked.otf
```

**Unpack a collection.** Download [the pinned `collection.woff2` example](https://raw.githubusercontent.com/recursive-ai-oss/onefont-go/2a9a35cbfcd2e359c0178abdf2e0beaef4a7213f/testdata/webfont/collection.woff2) the same way:

```go
input := "collection.woff2"
```

```text
Wrote unpacked.ttc
```

## Related recipes

- [Inspect an unpacked font in Go](https://recursiveai.org/onefont/go/docs/getting-started/#load-a-font)

## The short answer

Call `onefont.UnwrapWebFont` on the file’s bytes, then write the returned slice as a font file. Choose `.ttf`, `.otf`, or `.ttc` from the signature of the *unpacked* font. The `.woff2` suffix only tells you about the wrapper.

## Can you convert WOFF2 to TTF?

Yes, if the web font wraps a font with TrueType outlines. The [OpenType file specification](https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory) gives `00 01 00 00` as the sfnt version for those outlines. The sample has that signature, so the program writes `unpacked.ttf`. An sfnt is a font container built from named tables; the program checks its first four bytes rather than looking for a family name or a filename extension.

Unwrapping is not an outline conversion. If the source is a web subset with only the characters needed by a site, the output is still that subset. A `.ttf` extension will not restore missing characters, weights, or tables. And the output need not be byte-for-byte identical to the font before compression: onefont-go reverses WOFF2 table transforms and recomputes the table checksums. It skips the wrapper’s optional metadata and private data.

## How can I convert a WOFF2 file to OTF?

Use the same call. An unpacked `OTTO` signature means the OpenType font contains CFF or CFF2 outlines; give it the `.otf` extension. If you get `ttcf` instead, keep the result as a `.ttc` collection. It can hold multiple faces, not just one. If a downstream tool accepts only TrueType outlines, changing the extension of an `OTTO` result will not make it compatible.

## How can I convert a WOFF2 file without using a web service?

`UnwrapWebFont` runs locally. It also accepts WOFF 1.0. Use `IsWebFont` if one code path needs to handle both already-unpacked and wrapped fonts: it checks the signature only, not whether the remaining data is valid. When you only want to inspect or shape a face, pass the unpacked bytes to `Parse` without writing a copy first.

A compressed file can be small even when the unpacked tables are large. The example sets an 8 MiB output limit with `WithMaxFontSize`; omitting it uses the library’s 64 MiB default. If a known font needs a higher limit than your application chose, raise it for that input rather than inferring an output limit from the file size.
