mozilla/pdf.js · error · FormatError

Invalid codespace range.

Error message

Invalid codespace range.

What it means

Thrown by parseCodespaceRanges when its token loop exits without finding endcodespacerange - the codespace declaration is incomplete or its low/high tokens are not hex strings (the loop breaks on a non-string token). The codespace is the foundational part of a CMap, so an incomplete one is unrecoverable.

Source

Thrown at src/core/cmap.js:573

    let obj = lexer.getObj();
    if (obj === EOF) {
      break;
    }
    if (isCmd(obj, "endcodespacerange")) {
      return;
    }
    if (typeof obj !== "string") {
      break;
    }
    const low = strToInt(obj);
    obj = lexer.getObj();
    if (typeof obj !== "string") {
      break;
    }
    const high = strToInt(obj);
    cMap.addCodespaceRange(obj.length, low, high);
  }
  throw new FormatError("Invalid codespace range.");
}

function parseWMode(cMap, lexer) {
  const obj = lexer.getObj();
  if (Number.isInteger(obj)) {
    cMap.vertical = !!obj;
  }
}

function parseCMapName(cMap, lexer) {
  const obj = lexer.getObj();
  if (obj instanceof Name) {
    cMap.name = obj.name;
  }
}

async function parseCMap(cMap, lexer, fetchBuiltInCMap, useCMap) {
  let previous, embeddedUseCMap;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair or regenerate the PDF.
  2. Update pdf.js.
  3. Catch FormatError and degrade gracefully for that font.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await loadingTask.promise;
} catch (e) {
  if (e instanceof FormatError && /Invalid codespace range/.test(e.message)) {
    console.warn('Truncated codespace in CMap, font mapping will degrade', e);
  } else throw e;
}

Prevention

When it happens

Trigger: Parsing a CMap whose begincodespacerange block is truncated, whose low/high tokens are not strings, or which hits EOF before endcodespacerange.

Common situations: Corrupt or truncated CMap; the wrong stream referenced as a CMap; a CMap produced by a buggy writer that omitted the codespace terminator.

Related errors


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