mozilla/pdf.js · error · FormatError

Invalid bf range.

Error message

Invalid bf range.

What it means

Thrown at the end of parseBfChar when its token loop exits without encountering endbfrange - that is, the bfrange block is truncated or its tokens do not match any of the expected forms (hex string, Name, or the [ ... ] array form), causing the loop's else break. Indicates an incomplete or malformed bfrange section.

Source

Thrown at src/core/cmap.js:511

    expectString(obj);
    const high = strToInt(obj);
    obj = lexer.getObj();
    if (Number.isInteger(obj) || typeof obj === "string") {
      const dstLow = Number.isInteger(obj) ? String.fromCharCode(obj) : obj;
      cMap.mapBfRange(low, high, dstLow);
    } else if (isCmd(obj, "[")) {
      obj = lexer.getObj();
      const array = [];
      while (!isCmd(obj, "]") && obj !== EOF) {
        array.push(obj);
        obj = lexer.getObj();
      }
      cMap.mapBfRangeToArray(low, high, array);
    } else {
      break;
    }
  }
  throw new FormatError("Invalid bf range.");
}

function parseCidChar(cMap, lexer) {
  while (true) {
    let obj = lexer.getObj();
    if (obj === EOF) {
      break;
    }
    if (isCmd(obj, "endcidchar")) {
      return;
    }
    expectString(obj);
    const src = strToInt(obj);
    obj = lexer.getObj();
    expectInt(obj);
    const dst = obj;
    cMap.mapOne(src, dst);
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair or regenerate the PDF.
  2. Update pdf.js; later versions are more tolerant of some malformed shapes.
  3. Catch FormatError during load and fall back to rendering without ToUnicode.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await loadingTask.promise;
} catch (e) {
  if (e instanceof FormatError && /Invalid bf range/.test(e.message)) {
    console.warn('Truncated bfrange in CMap, ToUnicode may be incomplete', e);
  } else throw e;
}

Prevention

When it happens

Trigger: A beginbfrange ... endbfrange block that is truncated, contains an unexpected token type that triggers the break, or hits EOF before the endbfrange marker.

Common situations: Truncated ToUnicode stream embedded in the PDF; CMap stream cut off by a broken indirect-object resolution; a hand-edited CMap missing its terminator.

Related errors


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