mozilla/pdf.js · error · Error

BinaryCMapReader.process: Invalid dataSize.

Error message

BinaryCMapReader.process: Invalid dataSize.

What it means

Thrown by BinaryCMapReader.process() (binary_cmap.js:175) when the low nibble of a record header byte yields dataSize such that dataSize + 1 exceeds MAX_NUM_SIZE (16). dataSize sizes the Uint8Array work buffers (start/end/char/charCode/tmp, all length 16), so a larger value would overflow them. The record is structurally invalid.

Source

Thrown at src/core/binary_cmap.js:175

    while ((b = stream.getByte()) >= 0) {
      const type = b >> 5;
      if (type === 7) {
        // metadata, e.g. comment or usecmap
        switch (b & 0x1f) {
          case 0:
            stream.readString(); // skipping comment
            break;
          case 1:
            useCMap = stream.readString();
            break;
        }
        continue;
      }
      const sequence = !!(b & 0x10);
      const dataSize = b & 15;

      if (dataSize + 1 > MAX_NUM_SIZE) {
        throw new Error("BinaryCMapReader.process: Invalid dataSize.");
      }

      const ucs2DataSize = 1;
      const subitemsCount = stream.readNumber();
      switch (type) {
        case 0: // codespacerange
          stream.readHex(start, dataSize);
          stream.readHexNumber(end, dataSize);
          addHex(end, start, dataSize);
          cMap.addCodespaceRange(
            dataSize + 1,
            hexToInt(start, dataSize),
            hexToInt(end, dataSize)
          );
          for (let i = 1; i < subitemsCount; i++) {
            incHex(end, dataSize);
            stream.readHexNumber(start, dataSize);
            addHex(start, end, dataSize);

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Replace the CMap resource at cMapUrl with a known-good copy from the matching PDF.js release.
  2. Confirm the served file is actually a binary packed CMap (starts with the expected bcmap header byte) and not a text CMap or error page.
  3. Re-derive/re-download the CMap set from the official source to eliminate bit-rot.
  4. Match the PDF.js build version to the CMap distribution version.
Defensive patterns

Strategy: validation

Validate before calling

// Guard the CMap source: ensure the URL resolves a genuine binary bcmap.
async function assertBcMapOk(url) {
  const res = await fetch(url);
  if (!res.ok) throw new Error(`CMap fetch failed: ${res.status}`);
  const buf = new Uint8Array(await res.arrayBuffer());
  if (buf.length < 2) throw new Error('bcmap too short / corrupt');
  return buf;
}
// Then rely on PDF.js by pointing cMapUrl at the validated directory.

Try / catch

try {
  await page.render({ canvasContext, viewport }).promise;
} catch (err) {
  if (/Invalid dataSize/i.test(err?.message)) {
    console.warn('Corrupt binary CMap; replace the cmaps directory', err);
  } else throw err;
}

Prevention

When it happens

Trigger: While iterating records, b & 15 (dataSize) produces a value >= 16. Occurs when a .bcmap file is corrupt, when a non-bcmap byte sequence is fed to BinaryCMapReader, or when the record stream is misaligned because an earlier record was mis-parsed (e.g., from a preceding EOF/short read).

Common situations: Corrupt or hand-modified .bcmap resource; wrong file served at the CMap URL (e.g., a text CMap or HTML body parsed as binary); version skew between the bcmap writer and reader; bit-rot/disk corruption of the cached CMap.

Related errors


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