{"record":{"id":"34c1ba260b860f82","repo":"mozilla/pdf.js","slug":"encoding-required","errorCode":null,"errorMessage":"Encoding required.","messagePattern":"Encoding required\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/cmap.js","lineNumber":720,"sourceCode":"      if (encoding.isAsync) {\n        const bytes = await encoding.asyncGetBytes();\n        if (bytes) {\n          encoding = new Stream(bytes, 0, bytes.length, encoding.dict);\n        }\n      }\n      const parsedCMap = await parseCMap(\n        /* cMap = */ new CMap(),\n        /* lexer = */ new Lexer(encoding),\n        fetchBuiltInCMap,\n        useCMap\n      );\n\n      if (parsedCMap.isIdentityCMap) {\n        return createBuiltInCMap(parsedCMap.name, fetchBuiltInCMap);\n      }\n      return parsedCMap;\n    }\n    throw new Error(\"Encoding required.\");\n  }\n}\n\nexport { CMap, CMapFactory, IdentityCMap };\n","sourceCodeStart":702,"sourceCodeEnd":725,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/cmap.js#L702-L725","documentation":"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.","triggerScenarios":"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.","commonSituations":"Corrupt PDF with a missing or wrong-typed /Encoding entry; programmatic misuse of the internal CMapFactory API by a downstream library.","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."],"exampleFix":"// before\nCMapFactory.create({ encoding: 'Identity-H' }); // raw string\n\n// after\nimport { Name } from './primitives.js';\nCMapFactory.create({ encoding: Name.get('Identity-H') });","handlingStrategy":"validation","validationCode":"import { Name } from './primitives.js';\nimport { BaseStream } from './base_stream.js';\n\nfunction validateEncoding(encoding) {\n  if (!(encoding instanceof Name) && !(encoding instanceof BaseStream)) {\n    throw new TypeError('encoding must be a Name or BaseStream');\n  }\n}","typeGuard":"function isCMapEncoding(encoding) {\n  return encoding instanceof Name || encoding instanceof BaseStream;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["cmap","font","api-misuse","pdf","parsing"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}