Skip to main content

Function: loadGoogleFont()

function loadGoogleFont(family, options?): Promise<Uint8Array<ArrayBufferLike>>;

Downloads a Google Font and returns its raw TrueType/OpenType bytes, ready to pass to PdfDocument.embedTrueTypeFont or as a face in PdfDocument.registerFontFamily.

The Google Fonts CSS API serves woff2 (Brotli-compressed) to clients that advertise woff2 support — such as browsers — and an embeddable .ttf/.otf to other clients, such as Node. Both work: when a woff2 URL is served, the @barrierbreak/a11ydocs-pdf/woff2 decoder is loaded on demand and the file is converted to TTF bytes before returning.

Parameters​

ParameterTypeDescription
familystringGoogle Font family name, e.g. "Inter" or "Roboto Slab".
optionsLoadGoogleFontOptionsWeight, style, subsetting, and network options.

Returns​

Promise<Uint8Array<ArrayBufferLike>>

The font file bytes.

Example​

const bytes = await loadGoogleFont("Inter", { weight: 700 });
const inter = doc.embedTrueTypeFont(bytes, { family: "Inter" });
doc.addPage().text("Hello", { font: inter, fontSize: 24 });