mozilla/pdf.js · error · Error

Invalid PDF binary data: either TypedArray, string, or array

Error message

Invalid PDF binary data: either TypedArray, string, or array-like object is expected in the data property.

What it means

Thrown by getDataProp when the 'data' property of getDocument() is not a string, ArrayBuffer, TypedArray, or array-like object with a numeric length. PDF.js needs raw bytes to send to the worker; values that are none of the accepted shapes cannot be coerced and are rejected.

Source

Thrown at src/display/api_utils.js:85

    );
  }
  if (val instanceof Uint8Array && val.byteLength === val.buffer.byteLength) {
    // Use the data as-is when it's already a Uint8Array that completely
    // "utilizes" its underlying ArrayBuffer, to prevent any possible
    // issues when transferring it to the worker-thread.
    return val;
  }
  if (typeof val === "string") {
    return stringToBytes(val);
  }
  if (
    val instanceof ArrayBuffer ||
    ArrayBuffer.isView(val) ||
    (typeof val === "object" && !isNaN(val?.length))
  ) {
    return new Uint8Array(val);
  }
  throw new Error(
    "Invalid PDF binary data: either TypedArray, " +
      "string, or array-like object is expected in the data property."
  );
}

function getFactoryUrlProp(val) {
  if (typeof val !== "string") {
    return null;
  }
  if (val.endsWith("/")) {
    return val;
  }
  throw new Error(`Invalid factory url: "${val}" must include trailing slash.`);
}

const isRefProxy = v =>
  typeof v === "object" &&
  Number.isInteger(v?.num) &&

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Convert the source to a Uint8Array first: const data = new Uint8Array(await blob.arrayBuffer()).
  2. For fetch responses use response.bytes() or new Uint8Array(await response.arrayBuffer()).
  3. For raw bytes use a Uint8Array covering its full ArrayBuffer.

Example fix

// before
const res = await fetch(url);
getDocument({ data: await res.blob() });

// after
const res = await fetch(url);
getDocument({ data: new Uint8Array(await res.arrayBuffer()) });
Defensive patterns

Strategy: validation

Validate before calling

async function toBytes(src) {
  if (src instanceof Uint8Array) return src;
  if (src instanceof ArrayBuffer || ArrayBuffer.isView(src)) return new Uint8Array(src);
  if (typeof src === 'string') return new TextEncoder().encode(src);
  if (src && typeof src.arrayBuffer === 'function') return new Uint8Array(await src.arrayBuffer());
  throw new TypeError('Unsupported data type');
}
getDocument({ data: await toBytes(raw) });

Type guard

const isAcceptableData = (v): boolean =>
  v instanceof Uint8Array ||
  v instanceof ArrayBuffer ||
  ArrayBuffer.isView(v) ||
  typeof v === 'string' ||
  (!!v && typeof v.length === 'number');

Try / catch

null

Prevention

When it happens

Trigger: getDocument({ data: 42 }), getDocument({ data: true }), getDocument({ data: {} }) (plain object without .length), getDocument({ data: Promise }) or any non-array-like value.

Common situations: Passing a Blob or File directly (must be arrayBuffer()'d first); passing a JSON object; passing a ReadableStream; assuming fetch().then(r => r.body) can be used as data.

Related errors


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