mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16: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:
parent
e1e97c6edd
commit
dd95e4f851
2 changed files with 47 additions and 9 deletions
|
@ -117,6 +117,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||
* } TypedArray
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef { TypedArray | ArrayBuffer | Array<number> | string } BinaryData
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} RefProxy
|
||||
* @property {number} num
|
||||
|
@ -127,10 +131,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||
* Document initialization / loading parameters object.
|
||||
*
|
||||
* @typedef {Object} DocumentInitParameters
|
||||
* @property {string|URL} [url] - The URL of the PDF.
|
||||
* @property {TypedArray|Array<number>|string} [data] - Binary PDF data. Use
|
||||
* typed arrays (Uint8Array) to improve the memory usage. If PDF data is
|
||||
* BASE64-encoded, use `atob()` to convert it to a binary string first.
|
||||
* @property {string | URL} [url] - The URL of the PDF.
|
||||
* @property {BinaryData} [data] - Binary PDF data.
|
||||
* Use typed arrays (Uint8Array) to improve the memory usage. If PDF data is
|
||||
* BASE64-encoded, use `atob()` to convert it to a binary string first.
|
||||
* @property {Object} [httpHeaders] - Basic authentication headers.
|
||||
* @property {boolean} [withCredentials] - Indicates whether or not
|
||||
* cross-site Access-Control requests should be made using credentials such
|
||||
|
@ -217,6 +221,12 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||
* (see `web/debugger.js`). The default value is `false`.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef { string | URL | TypedArray | ArrayBuffer |
|
||||
* PDFDataRangeTransport | DocumentInitParameters
|
||||
* } GetDocumentParameters
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is the main entry point for loading a PDF and interacting with it.
|
||||
*
|
||||
|
@ -224,7 +234,7 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||
* XHR as fallback) is used, which means it must follow same origin rules,
|
||||
* e.g. no cross-domain requests without CORS.
|
||||
*
|
||||
* @param {string|URL|TypedArray|PDFDataRangeTransport|DocumentInitParameters}
|
||||
* @param {GetDocumentParameters}
|
||||
* src - Can be a URL where a PDF file is located, a typed array (Uint8Array)
|
||||
* already populated with data, or a parameter object.
|
||||
* @returns {PDFDocumentLoadingTask}
|
||||
|
@ -243,7 +253,7 @@ function getDocument(src) {
|
|||
if (typeof src !== "object") {
|
||||
throw new Error(
|
||||
"Invalid parameter in getDocument, " +
|
||||
"need either string, URL, Uint8Array, or parameter object."
|
||||
"need either string, URL, TypedArray, or parameter object."
|
||||
);
|
||||
}
|
||||
if (!src.url && !src.data && !src.range) {
|
||||
|
@ -308,7 +318,7 @@ function getDocument(src) {
|
|||
params[key] = new Uint8Array(value);
|
||||
} else {
|
||||
throw new Error(
|
||||
"Invalid PDF binary data: either typed array, " +
|
||||
"Invalid PDF binary data: either TypedArray, " +
|
||||
"string, or array-like object is expected in the data property."
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue