Skip to content

Commit ba56732

Browse files
Merge pull request #13079 from Snuffleupagus/issue-13075
Ensure that `getDocument` handles Node.js `Buffer`s more gracefully (issue 13075)
2 parents 6ae8125 + 50681d7 commit ba56732

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/display/api.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,21 @@ function getDocument(src) {
243243
} else if (key === "worker") {
244244
worker = source[key];
245245
continue;
246-
} else if (key === "data" && !(source[key] instanceof Uint8Array)) {
246+
} else if (key === "data") {
247247
// Converting string or array-like data to Uint8Array.
248248
const pdfBytes = source[key];
249-
if (typeof pdfBytes === "string") {
249+
if (
250+
typeof PDFJSDev !== "undefined" &&
251+
PDFJSDev.test("GENERIC") &&
252+
isNodeJS &&
253+
typeof Buffer !== "undefined" && // eslint-disable-line no-undef
254+
pdfBytes instanceof Buffer // eslint-disable-line no-undef
255+
) {
256+
params[key] = new Uint8Array(pdfBytes);
257+
} else if (pdfBytes instanceof Uint8Array) {
258+
// Use the data as-is when it's already a Uint8Array.
259+
params[key] = pdfBytes;
260+
} else if (typeof pdfBytes === "string") {
250261
params[key] = stringToBytes(pdfBytes);
251262
} else if (
252263
typeof pdfBytes === "object" &&
@@ -259,8 +270,7 @@ function getDocument(src) {
259270
} else {
260271
throw new Error(
261272
"Invalid PDF binary data: either typed array, " +
262-
"string or array-like object is expected in the " +
263-
"data property."
273+
"string, or array-like object is expected in the data property."
264274
);
265275
}
266276
continue;

0 commit comments

Comments
 (0)