mozilla/pdf.js · error · FormatError
Malformed CMap: expected string.
Error message
Malformed CMap: expected string.
What it means
Thrown by the expectString helper while parsing a CMap when the lexer yields an object that is not a JS string at a position where a hex/string token is mandatory (for example the source code points inside a beginbfchar block). It signals a structurally malformed CMap stream rather than a recoverable typo.
Source
Thrown at src/core/cmap.js:452
}
// eslint-disable-next-line getter-return
get isIdentityCMap() {
unreachable("should not access .isIdentityCMap");
}
}
function strToInt(str) {
let a = 0;
for (let i = 0; i < str.length; i++) {
a = (a << 8) | str.charCodeAt(i);
}
return a >>> 0;
}
function expectString(obj) {
if (typeof obj !== "string") {
throw new FormatError("Malformed CMap: expected string.");
}
}
function expectInt(obj) {
if (!Number.isInteger(obj)) {
throw new FormatError("Malformed CMap: expected int.");
}
}
function parseBfChar(cMap, lexer) {
while (true) {
let obj = lexer.getObj();
if (obj === EOF) {
break;
}
if (isCmd(obj, "endbfchar")) {
return;
}View on GitHub (pinned to 5903d58d58)
Solutions
- Validate the PDF in an authoritative viewer and regenerate it if it fails.
- Update pdf.js.
- Catch FormatError around document load and skip ToUnicode for the offending font.
Defensive patterns
Strategy: try-catch
Try / catch
try {
await loadingTask.promise;
} catch (e) {
if (e instanceof FormatError && /expected string/.test(e.message)) {
console.warn('Malformed CMap (expected string), text selection may degrade', e);
} else throw e;
} Prevention
- Validate PDFs from untrusted sources before rendering.
- Keep pdf.js updated for more tolerant CMap parsing.
- If you generate PDFs, validate the emitted ToUnicode streams parse cleanly.
- Treat ToUnicode failures as non-fatal: rendering can continue without them.
When it happens
Trigger: Parsing a CMap stream where a token expected to be a hex string such as <0041> is instead a number, a Name, a Cmd, or EOF. Reached via parseBfChar / parseCidChar / similar parse* helpers that call expectString.
Common situations: Truncated ToUnicode stream; a PDF whose /ToUnicode entry points at a non-CMap stream; a broken CMap writer that emitted numbers where strings were required.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- mapBfRange - ignoring data above MAX_MAP_RANGE.
- mapBfRangeToArray - ignoring data above MAX_MAP_RANGE.
- Invalid bf range.
- mapCidRange - ignoring data above MAX_MAP_RANGE.
- Malformed CMap: expected int.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/db46023298cd0146.
Report an issue: GitHub.