mozilla/pdf.js · error · FormatError

Unknown encoding format: ${format} in CFF

Error message

Unknown encoding format: ${format} in CFF

What it means

Thrown by CFFParser.parseEncoding when the encoding table's low 7 bits of the format byte are not 0 or 1 (the only defined encoding sub-formats). The default branch fires, indicating a corrupt or non-standard encoding table in the CFF font.

Source

Thrown at src/core/cff_parser.js:1044

          for (i = 1; i <= glyphsCount; i++) {
            encoding[bytes[pos++]] = i;
          }
          break;

        case 1:
          const rangesCount = bytes[pos++];
          let gid = 1;
          for (i = 0; i < rangesCount; i++) {
            const start = bytes[pos++];
            const left = bytes[pos++];
            for (let j = start; j <= start + left; j++) {
              encoding[j] = gid++;
            }
          }
          break;

        default:
          throw new FormatError(`Unknown encoding format: ${format} in CFF`);
      }
      const dataEnd = pos;
      if (format & 0x80) {
        // hasSupplement
        // The font sanitizer does not support CFF encoding with a
        // supplement, since the encoding is not really used to map
        // between gid to glyph, let's overwrite what is declared in
        // the top dictionary to let the sanitizer think the font use
        // StandardEncoding, that's a lie but that's ok.
        bytes[dataStart] &= 0x7f;
        readSupplement();
      }
      raw = bytes.subarray(dataStart, dataEnd);
    }
    format &= 0x7f;
    return new CFFEncoding(predefined, format, encoding, raw);
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Accept pdf.js's automatic fallback to a system font.
  2. Re-embed/repair the font with fontTools or Ghostscript.
  3. Validate CFF encoding bytes during your PDF generation step.

Example fix

// pdf.js substitutes a fallback font; caller handles via render promise.
try {
  await page.render({ canvasContext, viewport }).promise;
} catch (e) {
  console.warn('Render failed due to unknown CFF encoding format:', e);
}
Defensive patterns

Strategy: fallback

Validate before calling

null

Type guard

null

Try / catch

try {
  await page.render({ canvasContext, viewport }).promise;
} catch (e) {
  // Corrupt CFF encoding; pdf.js falls back to a system font.
  console.warn('Unknown CFF encoding format:', e);
}

Prevention

When it happens

Trigger: Parsing a CFF font whose encoding table format byte (after `format & 0x7f`) is neither 0 nor 1. Reached in the non-predefined path (`pos` not 0 or 1).

Common situations: Corrupt encoding tables in embedded CFF fonts; damaged font streams; non-standard encoding formats emitted by faulty font tools; CIDFont streams parsed through the legacy encoding path.

Related errors


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