Skip to content

Commit ab1297f

Browse files
Merge pull request #15299 from Snuffleupagus/getDocument-ArrayBuffer
Add *official* support for passing `ArrayBuffer`-data to `getDocument` (issue 15269)
2 parents e1e97c6 + dd95e4f commit ab1297f

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/display/api.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
117117
* } TypedArray
118118
*/
119119

120+
/**
121+
* @typedef { TypedArray | ArrayBuffer | Array<number> | string } BinaryData
122+
*/
123+
120124
/**
121125
* @typedef {Object} RefProxy
122126
* @property {number} num
@@ -127,10 +131,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
127131
* Document initialization / loading parameters object.
128132
*
129133
* @typedef {Object} DocumentInitParameters
130-
* @property {string|URL} [url] - The URL of the PDF.
131-
* @property {TypedArray|Array<number>|string} [data] - Binary PDF data. Use
132-
* typed arrays (Uint8Array) to improve the memory usage. If PDF data is
133-
* BASE64-encoded, use `atob()` to convert it to a binary string first.
134+
* @property {string | URL} [url] - The URL of the PDF.
135+
* @property {BinaryData} [data] - Binary PDF data.
136+
* Use typed arrays (Uint8Array) to improve the memory usage. If PDF data is
137+
* BASE64-encoded, use `atob()` to convert it to a binary string first.
134138
* @property {Object} [httpHeaders] - Basic authentication headers.
135139
* @property {boolean} [withCredentials] - Indicates whether or not
136140
* cross-site Access-Control requests should be made using credentials such
@@ -217,14 +221,20 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
217221
* (see `web/debugger.js`). The default value is `false`.
218222
*/
219223

224+
/**
225+
* @typedef { string | URL | TypedArray | ArrayBuffer |
226+
* PDFDataRangeTransport | DocumentInitParameters
227+
* } GetDocumentParameters
228+
*/
229+
220230
/**
221231
* This is the main entry point for loading a PDF and interacting with it.
222232
*
223233
* NOTE: If a URL is used to fetch the PDF data a standard Fetch API call (or
224234
* XHR as fallback) is used, which means it must follow same origin rules,
225235
* e.g. no cross-domain requests without CORS.
226236
*
227-
* @param {string|URL|TypedArray|PDFDataRangeTransport|DocumentInitParameters}
237+
* @param {GetDocumentParameters}
228238
* src - Can be a URL where a PDF file is located, a typed array (Uint8Array)
229239
* already populated with data, or a parameter object.
230240
* @returns {PDFDocumentLoadingTask}
@@ -243,7 +253,7 @@ function getDocument(src) {
243253
if (typeof src !== "object") {
244254
throw new Error(
245255
"Invalid parameter in getDocument, " +
246-
"need either string, URL, Uint8Array, or parameter object."
256+
"need either string, URL, TypedArray, or parameter object."
247257
);
248258
}
249259
if (!src.url && !src.data && !src.range) {
@@ -308,7 +318,7 @@ function getDocument(src) {
308318
params[key] = new Uint8Array(value);
309319
} else {
310320
throw new Error(
311-
"Invalid PDF binary data: either typed array, " +
321+
"Invalid PDF binary data: either TypedArray, " +
312322
"string, or array-like object is expected in the data property."
313323
);
314324
}

test/unit/api_spec.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ describe("api", function () {
170170
expect(true).toEqual(true);
171171
});
172172

173-
it("creates pdf doc from typed array", async function () {
173+
it("creates pdf doc from TypedArray", async function () {
174174
const typedArrayPdf = await DefaultFileReaderFactory.fetch({
175175
path: TEST_PDFS_PATH + basicApiFileName,
176176
});
177177

178178
// Sanity check to make sure that we fetched the entire PDF file.
179+
expect(typedArrayPdf instanceof Uint8Array).toEqual(true);
179180
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
180181

181182
const loadingTask = getDocument(typedArrayPdf);
@@ -196,6 +197,33 @@ describe("api", function () {
196197
await loadingTask.destroy();
197198
});
198199

200+
it("creates pdf doc from ArrayBuffer", async function () {
201+
const { buffer: arrayBufferPdf } = await DefaultFileReaderFactory.fetch({
202+
path: TEST_PDFS_PATH + basicApiFileName,
203+
});
204+
205+
// Sanity check to make sure that we fetched the entire PDF file.
206+
expect(arrayBufferPdf instanceof ArrayBuffer).toEqual(true);
207+
expect(arrayBufferPdf.byteLength).toEqual(basicApiFileLength);
208+
209+
const loadingTask = getDocument(arrayBufferPdf);
210+
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
211+
212+
const progressReportedCapability = createPromiseCapability();
213+
loadingTask.onProgress = function (data) {
214+
progressReportedCapability.resolve(data);
215+
};
216+
217+
const data = await Promise.all([
218+
loadingTask.promise,
219+
progressReportedCapability.promise,
220+
]);
221+
expect(data[0] instanceof PDFDocumentProxy).toEqual(true);
222+
expect(data[1].loaded / data[1].total).toEqual(1);
223+
224+
await loadingTask.destroy();
225+
});
226+
199227
it("creates pdf doc from invalid PDF file", async function () {
200228
// A severely corrupt PDF file (even Adobe Reader fails to open it).
201229
const loadingTask = getDocument(buildGetDocumentParams("bug1020226.pdf"));
@@ -441,7 +469,7 @@ describe("api", function () {
441469
}
442470
);
443471

444-
it("creates pdf doc from empty typed array", async function () {
472+
it("creates pdf doc from empty TypedArray", async function () {
445473
const loadingTask = getDocument(new Uint8Array(0));
446474
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
447475

0 commit comments

Comments
 (0)