Custom Font List
PDF text and the editor font menu draw on two related but distinct sources.
| Source | What it controls |
|---|---|
document.assets.fonts and a text run’s fontId | Imported PDF font metadata and the resources used to reconstruct native content. |
fontFamily on an editable text run | The CSS font family used to lay out and render editable text. |
| Univer’s shared font list | The options shown in the PDF inspector’s font dropdown. |
Adding an option to the shared list does not add an asset to document.assets.fonts, and it does not download or embed a font file.
Configure the editor font list
When editor.enabled is true, the PDF inspector uses the shared FontFamilyDropdown. Configure that list on UniverUIPlugin when initializing the browser:
UniverUIPlugin
containerstring | HTMLElementElement to mount into, or its DOM ID.customFontFamilyIFontConfig[] | objectAdds these fonts to the dropdown.listIFontConfig[]Adds these fonts to the dropdown.valuestringFont family name used in CSS.labelstringDisplay name or translation key.categoryFontCategoryFont group. Usesans-serif,serif,monospace,display, orhandwriting.
overridebooleanDefault:falseReplaces the built-in choices with the supplied list.
import { FontCategory, UniverUIPlugin } from '@univerjs/ui'univer.registerPlugin(UniverUIPlugin, { container: 'app', customFontFamily: { list: [ { value: 'Inter', label: 'Inter', category: FontCategory.SANS_SERIF, }, { value: 'Noto Serif', label: 'Noto Serif', category: FontCategory.SERIF, }, ], override: false, },})Load each browser font through @font-face or a stylesheet. Otherwise Canvas uses the browser’s fallback font.
Add choices at runtime
After the UI Facade has loaded, append choices to the same shared list with addFonts:
import { FontCategory } from '@univerjs/ui'univerAPI.addFonts([ { value: 'Inter', label: 'Inter', category: FontCategory.SANS_SERIF, },])This only updates the application font list. It does not load font bytes or rewrite existing PDF runs.
Apply a font to editable PDF text
Pass fontFamily when inserting text, or update an editable text box through the PDF Facade:
const page = univerAPI.getActivePdf()?.getPageByIndex(0)const textBox = page?.insertTextBox({ text: 'Hello PDF', fontFamily: 'Inter', fontSize: 14, left: 36, top: 36,})textBox?.setTextStyle({ fontFamily: 'Noto Serif' })Native imported text returned by page.getTextSpans() is a snapshot, so its style cannot be changed in place. Call replaceText(...) first to convert it into an editable text box, then call setTextStyle.
How is this guide?