Axonpack
@axonpdf/react

Fonts

Built-in fonts, registering your own, font weights, hyphenation and emoji.

Built-in fonts

Courier, Helvetica and Times-Roman are built in, each with bold, italic and bold italic faces. Helvetica is the default. Built-in fonts are never embedded in the file, which keeps it small but rules them out for PDF/A.

Registering a font

import { Font } from '@axonpdf/react';

Font.register({ family: 'Lato', src: 'https://example.com/fonts/Lato-Regular.ttf' });

Font.register({
  family: 'Lato',
  fonts: [
    { src: 'fonts/Lato-Regular.ttf' },
    { src: 'fonts/Lato-Bold.ttf', fontWeight: 'bold' },
    { src: 'fonts/Lato-Italic.ttf', fontStyle: 'italic' },
  ],
});

Register fonts before you render. src is a URL, a data: URL, a file path or file: URL in Node, or the name of a built-in face. For a URL, method, headers and body set the request.

TrueType, OpenType, WOFF and WOFF2 files work, and only the characters the document uses are embedded. A font collection (TTC) throws Font collection is not supported.

The store is shared by the whole app, even when it bundles two copies of the package. In a Web Worker the worker has its own, so register fonts in the worker file.

Weights and fallback

fontWeight is a number or a name: thin and hairline are 100, ultralight and extralight 200, light 300, normal 400, medium 500, semibold and demibold 600, bold 700, ultrabold and extrabold 800, heavy and black 900. When text asks for a weight that isn't registered, the nearest face is picked the way browsers do it.

fontFamily can be a list. Each character uses the first family that has it, then Helvetica, so a second family can cover symbols or another script:

<Text style={{ fontFamily: ['Lato', 'NotoSansArabic'] }}>...</Text>

Every family in the list must be registered.

Hyphenation

Words are hyphenated with English patterns by default, and soft hyphens (­) in a word replace the patterns for that word. To use your own rules:

Font.registerHyphenationCallback((word, builtin) => (word.length < 8 ? [word] : builtin(word)));

The callback returns the parts of each word. builtin(word) is the default split, and (word) => [word] turns hyphenation off. A Text can also take its own hyphenationCallback.

Emoji

Font.registerEmojiSource({ format: 'png', url: 'https://cdn.example.com/emoji/72x72/' });

Emoji are drawn as images from that source, or from { builder(code) }, a function that returns each image's URL. An emoji whose image fails to load stays text, with a warning.

The rest of the store

Font.getFont(descriptor), Font.load(descriptor), Font.reset() (forget loaded bytes, keep the registrations), Font.clear() (remove everything), Font.getRegisteredFonts(), Font.getRegisteredFontFamilies(), Font.getEmojiSource() and Font.getHyphenationCallback(). See the Reference.

On this page