mozilla/pdf.js · error · FormatError

invalid font name

Error message

invalid font name

What it means

Thrown by translateFont() after all font-name resolution attempts (descriptor /FontName, BaseFont-derived name, known-font-name heuristics) fail to produce a Name object for the font. Every code path that assigns fontName has run and none yielded a valid Name, so the font cannot be loaded because glyph metrics and file lookup depend on a name. This is the final, unrecoverable guard before font-file fetching.

Source

Thrown at src/core/evaluator.js:4605

      );
      // - Workaround for cases where e.g. fontNameStr = 'Arial' and
      //   baseFontStr = 'Arial,Bold' (needed when no font file is embedded).
      //
      // - Workaround for cases where e.g. fontNameStr = 'wg09np' and
      //   baseFontStr = 'Wingdings-Regular' (fixes issue7454.pdf).
      if (
        fontNameStr &&
        baseFontStr &&
        (baseFontStr.startsWith(fontNameStr) ||
          (!isKnownFontName(fontNameStr) && isKnownFontName(baseFontStr)))
      ) {
        fontName = null;
      }
      fontName ||= baseFont;
    }

    if (!(fontName instanceof Name)) {
      throw new FormatError("invalid font name");
    }

    let fontFile, fontFileN, subtype, length1, length2, length3;
    try {
      for (const n of ["FontFile", "FontFile2", "FontFile3"]) {
        fontFile = descriptor.get(n);
        if (fontFile) {
          fontFileN = n;
          break;
        }
      }

      if (fontFile) {
        if (!(fontFile instanceof BaseStream)) {
          throw new FormatError("FontFile should be a stream");
        } else {
          if (fontFile.isAsync) {
            const bytes = await fontFile.asyncGetBytes();

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair the PDF with Ghostscript (gs -sDEVICE=pdfwrite -dPDFSETTINGS=/default) to rebuild font metadata.
  2. If authoring, ensure at least one of /FontDescriptor >> /FontName or /BaseFont is a valid Name.
  3. Isolate the page with stopAtErrors: true; the rest of the document will render with fallback fonts.
  4. If you control the consumer app, surface a user-visible warning that a font could not be identified and text may be substituted.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await page.render({ canvasContext, viewport });
} catch (e) {
  if (e.name === 'FormatError' && /invalid font name/.test(e.message)) {
    // no resolvable font name — PDF.js cannot load this font
  } else throw e;
}

Prevention

When it happens

Trigger: A font dictionary whose /FontDescriptor lacks /FontName, whose /BaseFont is not a Name, and where none of the heuristics (startsWith, isKnownFontName) recovered a usable name. Fires at the end of the name-resolution block in translateFont().

Common situations: Severely malformed font dictionaries, often from corrupt or hand-built PDFs. Also occurs when a font subsetting tool stripped identifying names but left the structure otherwise intact. Rarely encountered with mainstream PDF producers.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/5de619a7dfd47337. Report an issue: GitHub.