RebodocsDocs
Start3
What is Rebodocs
Getting started
Demos to run
Put it in your app8
React component
Headless: your own UI
A complete interface
Without a framework
Only to read: the viewer
Styling and Tailwind
Phones and touch
Accessibility
Documents8
Templates and variables
On a server: DOCX and PDF
Documents in an S3 bucket
Letterheads
Versions
Tracked changes
Fonts
Languages
Together2
Real-time collaboration
AI assistants (MCP)
Reference5
Packages
Components and hooks
Editor API
How it works
Status and roadmap
↑↓ to move↵ to openEsc to close26 pages

Documents Fonts

Fonts

The editor measures and paints with the browser’s fonts, so any font the machine has already works. For one it does not have, your brand’s typeface or a letterhead’s, give the editor the file.

<DocsEditor
  docx={file}
  fonts={[
    { family: 'Raleway', src: '/fonts/Raleway-Regular.ttf' },
    { family: 'Raleway', src: '/fonts/Raleway-Bold.ttf', bold: true },
    { family: 'Raleway', src: '/fonts/Raleway-Italic.ttf', italic: true },
  ]}
/>
tip: give the page below a font, then make a PDF
Live

Give it a font

Courier Prime is on almost no machine. Give it to the page, put the document in it, and make the files: the PDF and the DOCX say what they carry.

Opening the editor…

Make the PDF before and after step 1: before, it names a standard font; after, it carries Courier Prime. The DOCX carries it only while its text is in it: whole, cut down with 'subset', or not at all.

Once given

What a font is used for

Offered

First in the toolbar’s font box, also in a bar made before it came. Headless, in state.fonts.

Measured and painted

Text in that family is set with it. Every open document is laid out again when it loads, since the width of its lines changes: a page may break elsewhere.

In the PDF

When the file is TrueType or OpenType, the PDF carries it, and its text can be selected and searched.

In the DOCX

When it is TrueType and its licence lets it, whole or cut down, for whoever opens the file in Word without the font installed.

Fonts belong to the page, not to one editor: every editor on it uses them, and they stay once given. Giving the same face of the same family again with the same source does nothing, so a component that renders again does not load its fonts again; with another source, the new file takes its place.

Giving one

The source

The fonts prop, also changed later: a font added to it is loaded then. Anywhere else, and at any time, there is addFonts.

import { addFonts } from '@rebodocs/react';   // or '@rebodocs/editor'

const read = await addFonts([{ family: 'Raleway', src: '/fonts/Raleway-Regular.ttf' }]);
// [true]: false when the file could not be read
srcWhen
An addressThe file is fetched.
Bytes (ArrayBuffer, Uint8Array)You already have it: from your API, a file input, a database.
Left outThe page loads the font itself, with an @font-face rule. The editor only lists it, and waits for it before measuring.

A font the page loads itself

@font-face {
  font-family: 'Raleway';   /* the name the documents give it */
  src: url('/fonts/raleway.woff2') format('woff2');
}
addFonts([{ family: 'Raleway' }]);   // listed, and waited for before the text is measured

The family must be the name the documents give the font. next/font gives its fonts names of its own (__Raleway_a1b2c3), which no document has: for those, give the file with src. A font left out goes into neither the PDF nor the DOCX: there is no file to put in.

Faces

A family is given one face at a time: regular, bold, italic, each its own file. A face not given is imitated from the regular one, on screen by the browser, as on any web page, and in a PDF the same way: the letters drawn thicker, or slanted. An imitation is not the face its designer drew: give the four (bold italic is bold: true, italic: true) for text in all of them.

PDF and DOCX

Into the files

What a font goes into depends on the kind of file it is.

FileOn screenIn the PDFIn the DOCX
.ttfYesCut down to the characters usedIf the licence lets it: whole, or cut down
.otf, TrueType outlinesYesCut down to the characters usedIf the licence lets it: whole, or cut down
.otf, CFF outlinesYesWholeNo
.woff, .woff2YesNo: Helvetica, Times or CourierNo
Left out (CSS)YesNo: Helvetica, Times or CourierNo

A PDF without the file names the standard font nearest the name: Courier for a monospaced one, Times for a serif, Helvetica for the rest. Words stay where the editor measured them, so lines keep their length, though the letters are not the font’s. Printing from the browser uses the font as shown, whatever its kind.

Chinese and Japanese characters the font has not are drawn in the font for them the document carries, given as the font of its Chinese text; or else in the one every PDF reader has: STSong-Light for Simplified Chinese, MSung-Light for Taiwan and Hong Kong, KozMinPro-Regular for Japanese.

The DOCX

The faces go in when some text of the document is in their family, its tables, headers, footers and styles included, as Word embeds fonts: someone who opens the file without the font installed sees it all the same. A font whose licence says it may not be embedded, or only as pictures, stays out.

embedFontsThe font goes
true (default)Whole, as Word embeds it. Whoever opens the file can type anything in it.
'subset'Cut down to the characters of the document and of the keyboard, as Word’s “Embed only the characters used”: a much smaller file. A character typed later that it does not keep shows in another font for whoever lacks the font. Whole when the licence says it may not be cut down.
falseNot at all.

On DocsEditor and DocsProvider it is a prop, followed when it changes; on the editor, editor.embedFonts.

PDF needs .ttf or .otf. A .woff2 shows and measures as well, but the PDF draws its text in a standard font. Serve a .ttf for the editor even when the page uses .woff2.

In the file

Fonts a DOCX carries

A DOCX saved by Word with its fonts embedded brings them along. The editor uses the ones this machine lacks, laying the document out again once they load, and draws its PDFs with them before any font given. Saving keeps them in the file. They are of that document: they are not listed in the toolbar, and they go when another document is opened.

No browser

On a server

With no browser, addFont does nothing and says false. The width of text is read from font files given to renderPdf.

import { importDocx, readFont, renderPdf } from '@rebodocs/core';

const fonts = ['Raleway-Regular.ttf', 'Raleway-Bold.ttf'].flatMap((file) => readFont(readFileSync(`fonts/${file}`)) ?? []);
const { pdf, missingFonts } = renderPdf(importDocx(bytes), fonts);

See On a server: missingFonts, what stands in, and the fonts of the system.

Text is kerned as Word kerns it: the pairs of letters the font file lists are set closer, a space and the letter after it too, in a run as big as its w:kern or bigger. The PDF moves the letters of each pair together, from the file. In the editor the browser kerns them, from another table of the font, and leaves out the pairs with a space of Times New Roman, so a line of it can be a fraction of a point apart from Word's on screen, not in the PDF.

Reference

From code

From @rebodocs/react or @rebodocs/editor; the last two, from @rebodocs/editor only.

addFont(font: FontSource) → Promise<boolean>
Gives the page a face of a family. Says whether it could be read: false for an address that does not answer with a font, a file the browser cannot read, or no browser at all.
addFonts(fonts: FontSource[]) → Promise<boolean[]>
Several at once, read side by side; the documents are laid out again once, when all are in.
fontFamilies() → string[]
The families given, in the order they came, each once.
onFontsChange(listener: () => void) → () => void
Calls listener when a font is given. Returns how to stop.
fontFile(spec: { family: string; bold: boolean; italic: boolean }) → Uint8Array | undefinededitor
The file of that face, or of the regular one of its family: what a PDF embeds.
fontFaces() → { family, bold, italic, bytes }[]editor
Every face given with its file: what a DOCX can carry.

FontSource

family: string
The name the document gives the font. Compared without minding capitals.
src?: string | ArrayBuffer | Uint8Array
An address to fetch, the bytes, or nothing for a font the page loads itself.
bold?: boolean
The file is the bold face.
italic?: boolean
The file is the italic face. Neither: the regular one.

On DocsEditor and DocsProvider, fonts?: FontSource[] and embedFonts?: boolean | 'subset', both followed when they change. A list of the same fonts in a new array is not read again.

Limits

What to know

  • There is no taking a font away once given; a page reload starts without it.
  • A font that loads after the document is open lays every page out again: lines and page breaks can move under the person’s eyes. Give fonts before opening, or wait for addFonts.
  • The file is fetched by the browser: an address on another domain needs CORS.
  • A DOCX carries each face whole unless embedFonts is 'subset': a font of 300 KB makes the file 300 KB larger.