{"record":{"id":"eec27fe5d68a55c8","repo":"mozilla/pdf.js","slug":"please-provide-binary-data-as-uint8array-rather","errorCode":null,"errorMessage":"Please provide binary data as `Uint8Array`, rather than `Buffer`.","messagePattern":"Please provide binary data as `Uint8Array`, rather than `Buffer`\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/display/api_utils.js","lineNumber":65,"sourceCode":"      return url;\n    }\n  }\n  throw new Error(\n    \"Invalid PDF url data: \" +\n      \"either string or URL-object is expected in the url property.\"\n  );\n}\n\nfunction getDataProp(val) {\n  // Converting string or array-like data to Uint8Array.\n  if (\n    typeof PDFJSDev !== \"undefined\" &&\n    PDFJSDev.test(\"GENERIC\") &&\n    isNodeJS &&\n    typeof Buffer !== \"undefined\" && // eslint-disable-line no-undef\n    val instanceof Buffer // eslint-disable-line no-undef\n  ) {\n    throw new Error(\n      \"Please provide binary data as `Uint8Array`, rather than `Buffer`.\"\n    );\n  }\n  if (val instanceof Uint8Array && val.byteLength === val.buffer.byteLength) {\n    // Use the data as-is when it's already a Uint8Array that completely\n    // \"utilizes\" its underlying ArrayBuffer, to prevent any possible\n    // issues when transferring it to the worker-thread.\n    return val;\n  }\n  if (typeof val === \"string\") {\n    return stringToBytes(val);\n  }\n  if (\n    val instanceof ArrayBuffer ||\n    ArrayBuffer.isView(val) ||\n    (typeof val === \"object\" && !isNaN(val?.length))\n  ) {\n    return new Uint8Array(val);","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/display/api_utils.js#L47-L83","documentation":"Thrown only in the GENERIC build running under Node.js, when the 'data' property passed to getDocument() is a Node Buffer. Buffers are Uint8Array subclasses but their allocation semantics can break transfer to the worker thread, so PDF.js explicitly rejects them and asks for a plain Uint8Array.","triggerScenarios":"In Node: getDocument({ data: fs.readFileSync(path) }) where readFileSync returns a Buffer; receiving a Buffer from an HTTP response and passing it directly.","commonSituations":"Server-side rendering, PDF processing scripts, Electron main process using Node fs; migrating from an older pdfjs version that silently accepted Buffers.","solutions":["Convert the Buffer to a Uint8Array that owns its buffer: new Uint8Array(buffer).","Read with an explicit Uint8Array target, e.g. fs.promises.readFile(path) then new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) only when byteOffset/length align — simplest is new Uint8Array(buf).","Stream the file into a pre-allocated Uint8Array if memory is a concern."],"exampleFix":"// before\nconst data = fs.readFileSync('file.pdf');\ngetDocument({ data });\n\n// after\nconst data = new Uint8Array(fs.readFileSync('file.pdf'));\ngetDocument({ data });","handlingStrategy":"validation","validationCode":"function toUint8Array(data) {\n  if (typeof Buffer !== 'undefined' && data instanceof Buffer) {\n    return new Uint8Array(data);\n  }\n  return data;\n}\ngetDocument({ data: toUint8Array(raw) });","typeGuard":"const isPossiblyBuffer = (v): v is Uint8Array | ArrayBufferLike =>\n  v instanceof Uint8Array || v instanceof ArrayBuffer || ArrayBuffer.isView(v);","tryCatchPattern":"null","preventionTips":["In Node, wrap fs.readFileSync with new Uint8Array(...) at the call site.","Never let Buffer reach pdfjs; convert at the boundary.","Add a unit test that asserts Uint8Array ownership."],"tags":["nodejs","getdocument","binary-data"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}