From 2598b0dcdd80a2a0eb204cd8f4da107304073453 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 8 Mar 2026 14:06:03 +0100 Subject: [PATCH] Start using `Blob.prototype.bytes()` in the code-base Note that this isn't motivated by the miniscule reduction in code-size, but rather by wanting to unblock using this newer feature; see https://developer.mozilla.org/en-US/docs/Web/API/Blob/bytes --- src/core/annotation.js | 6 +++--- src/shared/util.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index d97c01a96..29273fc01 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -5105,9 +5105,9 @@ class StampAnnotation extends MarkupAnnotation { ctx.drawImage(bitmap, 0, 0); } - const jpegBufferPromise = canvas + const jpegBytesPromise = canvas .convertToBlob({ type: "image/jpeg", quality: 1 }) - .then(blob => blob.arrayBuffer()); + .then(blob => blob.bytes()); const xobjectName = Name.get("XObject"); const imageName = Name.get("Image"); @@ -5144,7 +5144,7 @@ class StampAnnotation extends MarkupAnnotation { smaskStream = new Stream(alphaBuffer, 0, 0, smask); } - const imageStream = new Stream(await jpegBufferPromise, 0, 0, image); + const imageStream = new Stream(await jpegBytesPromise, 0, 0, image); return { imageStream, diff --git a/src/shared/util.js b/src/shared/util.js index ace389d50..92bc091d0 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -1259,6 +1259,17 @@ if ( }; } +// See https://developer.mozilla.org/en-US/docs/Web/API/Blob/bytes#browser_compatibility +if ( + typeof PDFJSDev !== "undefined" && + !PDFJSDev.test("SKIP_BABEL") && + typeof Blob.prototype.bytes !== "function" +) { + Blob.prototype.bytes = async function () { + return new Uint8Array(await this.arrayBuffer()); + }; +} + // See https://developer.mozilla.org/en-US/docs/Web/API/Response/bytes#browser_compatibility if ( typeof PDFJSDev !== "undefined" &&