mozilla/pdf.js · error · Error
Encoding required.
Error message
Encoding required.
What it means
Thrown by CMapFactory.create when the encoding argument is neither a Name nor a BaseStream - the only two supported input shapes. It means the caller passed undefined, a raw string, a number, or a malformed dictionary entry where a CMap/encoding was expected.
Source
Thrown at src/core/cmap.js:720
if (encoding.isAsync) {
const bytes = await encoding.asyncGetBytes();
if (bytes) {
encoding = new Stream(bytes, 0, bytes.length, encoding.dict);
}
}
const parsedCMap = await parseCMap(
/* cMap = */ new CMap(),
/* lexer = */ new Lexer(encoding),
fetchBuiltInCMap,
useCMap
);
if (parsedCMap.isIdentityCMap) {
return createBuiltInCMap(parsedCMap.name, fetchBuiltInCMap);
}
return parsedCMap;
}
throw new Error("Encoding required.");
}
}
export { CMap, CMapFactory, IdentityCMap };
View on GitHub (pinned to 5903d58d58)
Solutions
- Repair the PDF so the font dictionary has a valid /Encoding (Name or stream).
- If calling the API directly, pass a Name (e.g. Name.get('Identity-H')) or a BaseStream wrapping CMap bytes.
Example fix
// before
CMapFactory.create({ encoding: 'Identity-H' }); // raw string
// after
import { Name } from './primitives.js';
CMapFactory.create({ encoding: Name.get('Identity-H') }); Defensive patterns
Strategy: validation
Validate before calling
import { Name } from './primitives.js';
import { BaseStream } from './base_stream.js';
function validateEncoding(encoding) {
if (!(encoding instanceof Name) && !(encoding instanceof BaseStream)) {
throw new TypeError('encoding must be a Name or BaseStream');
}
} Type guard
function isCMapEncoding(encoding) {
return encoding instanceof Name || encoding instanceof BaseStream;
} Prevention
- When calling CMapFactory.create, pass a Name (Name.get('Identity-H')) or a BaseStream, never a raw string.
- Repair PDFs whose /Encoding or /ToUnicode entries resolve to unexpected types.
- If you wrap pdf.js in your own API, type-check encoding before forwarding.
- Add unit tests covering both the Name and BaseStream shapes.
When it happens
Trigger: Calling CMapFactory.create({ encoding: undefined }) or { encoding: 'Identity-H' } (raw string instead of Name), or a PDF whose font /Encoding or ToUnicode reference resolves to something other than a Name or stream.
Common situations: Corrupt PDF with a missing or wrong-typed /Encoding entry; programmatic misuse of the internal CMapFactory API by a downstream library.
Related errors
- mapCidRange - ignoring data above MAX_MAP_RANGE.
- mapBfRange - ignoring data above MAX_MAP_RANGE.
- mapBfRangeToArray - ignoring data above MAX_MAP_RANGE.
- Malformed CMap: expected string.
- Malformed CMap: expected int.
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/34c1ba260b860f82.
Report an issue: GitHub.