mozilla/pdf.js · error · FormatError

Unknown charset format

Error message

Unknown charset format

What it means

Thrown by CFFParser.parseCharset when the charset table's format byte is not one of the three spec-defined values (0, 1, or 2). The default branch of the format switch fires, indicating a corrupt or non-standard charset table.

Source

Thrown at src/core/cff_parser.js:988

        while (charset.length <= length) {
          id = (bytes[pos++] << 8) | bytes[pos++];
          count = bytes[pos++];
          for (i = 0; i <= count; i++) {
            charset.push(cid ? id++ : strings.get(id++));
          }
        }
        break;
      case 2:
        while (charset.length <= length) {
          id = (bytes[pos++] << 8) | bytes[pos++];
          count = (bytes[pos++] << 8) | bytes[pos++];
          for (i = 0; i <= count; i++) {
            charset.push(cid ? id++ : strings.get(id++));
          }
        }
        break;
      default:
        throw new FormatError("Unknown charset format");
    }

    return new CFFCharset(false, format, charset);
  }

  parseEncoding(pos, properties, strings, charset) {
    const encoding = Object.create(null);
    const bytes = this.bytes;
    let predefined = false;
    let format, i, ii;
    let raw = null;

    function readSupplement() {
      const supplementsCount = bytes[pos++];
      for (i = 0; i < supplementsCount; i++) {
        const code = bytes[pos++];
        const sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
        encoding[code] = charset.indexOf(strings.get(sid));

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Rely on pdf.js's font substitution path, which catches the FormatError and falls back.
  2. Repair or re-embed the font using fontTools or Ghostscript.
  3. Re-validate embedded fonts in your generation pipeline before writing the PDF.

Example fix

// Caller observes via render promise; pdf.js substitutes a fallback.
try {
  await page.render({ canvasContext, viewport }).promise;
} catch (e) {
  console.warn('Render failed due to corrupt CFF charset:', 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 charset; pdf.js falls back to a system font.
  console.warn('Unknown CFF charset format:', e);
}

Prevention

When it happens

Trigger: Parsing a CFF font whose charset table begins with a format byte outside {0,1,2}. Reached after predefined-charset shortcuts (ISOAdobe/Expert/ExpertSubset) are skipped and `format = bytes[pos++]` is read.

Common situations: Corrupt CFF charset tables in embedded fonts; damaged font streams; fonts produced by tools that write invalid charset format identifiers; CIDFont streams with mangled charset data.

Related errors


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