1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Add *official* support for passing ArrayBuffer-data to getDocument (issue 15269)

While this has always worked, as a consequence of the implementation, it's never been officially supported.
In addition to adding basic unit-tests, this patch also introduces a couple of new JSDoc `@typedef`s in the API to avoid overly long lines.
This commit is contained in:
Jonas Jenwald 2022-08-10 14:13:01 +02:00
parent e1e97c6edd
commit dd95e4f851
2 changed files with 47 additions and 9 deletions

View file

@ -170,12 +170,13 @@ describe("api", function () {
expect(true).toEqual(true);
});
it("creates pdf doc from typed array", async function () {
it("creates pdf doc from TypedArray", async function () {
const typedArrayPdf = await DefaultFileReaderFactory.fetch({
path: TEST_PDFS_PATH + basicApiFileName,
});
// Sanity check to make sure that we fetched the entire PDF file.
expect(typedArrayPdf instanceof Uint8Array).toEqual(true);
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
const loadingTask = getDocument(typedArrayPdf);
@ -196,6 +197,33 @@ describe("api", function () {
await loadingTask.destroy();
});
it("creates pdf doc from ArrayBuffer", async function () {
const { buffer: arrayBufferPdf } = await DefaultFileReaderFactory.fetch({
path: TEST_PDFS_PATH + basicApiFileName,
});
// Sanity check to make sure that we fetched the entire PDF file.
expect(arrayBufferPdf instanceof ArrayBuffer).toEqual(true);
expect(arrayBufferPdf.byteLength).toEqual(basicApiFileLength);
const loadingTask = getDocument(arrayBufferPdf);
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
const progressReportedCapability = createPromiseCapability();
loadingTask.onProgress = function (data) {
progressReportedCapability.resolve(data);
};
const data = await Promise.all([
loadingTask.promise,
progressReportedCapability.promise,
]);
expect(data[0] instanceof PDFDocumentProxy).toEqual(true);
expect(data[1].loaded / data[1].total).toEqual(1);
await loadingTask.destroy();
});
it("creates pdf doc from invalid PDF file", async function () {
// A severely corrupt PDF file (even Adobe Reader fails to open it).
const loadingTask = getDocument(buildGetDocumentParams("bug1020226.pdf"));
@ -441,7 +469,7 @@ describe("api", function () {
}
);
it("creates pdf doc from empty typed array", async function () {
it("creates pdf doc from empty TypedArray", async function () {
const loadingTask = getDocument(new Uint8Array(0));
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);