diff --git a/src/display/api.js b/src/display/api.js index 15b605c84..6549d85ab 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -111,7 +111,7 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { * Document initialization / loading parameters object. * * @typedef {Object} DocumentInitParameters - * @property {string} [url] - The URL of the PDF. + * @property {string|URL} [url] - The URL of the PDF. * @property {TypedArray|Array|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. @@ -192,16 +192,16 @@ 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|TypedArray|DocumentInitParameters|PDFDataRangeTransport} src - - * Can be a URL to where a PDF file is located, a typed array (Uint8Array) - * already populated with data or parameter object. + * @param {string|URL|TypedArray|PDFDataRangeTransport|DocumentInitParameters} + * 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} */ function getDocument(src) { const task = new PDFDocumentLoadingTask(); let source; - if (typeof src === "string") { + if (typeof src === "string" || src instanceof URL) { source = { url: src }; } else if (isArrayBuffer(src)) { source = { data: src }; @@ -211,7 +211,7 @@ function getDocument(src) { if (typeof src !== "object") { throw new Error( "Invalid parameter in getDocument, " + - "need either Uint8Array, string or a parameter object" + "need either string, URL, Uint8Array, or parameter object." ); } if (!src.url && !src.data && !src.range) { @@ -231,11 +231,21 @@ function getDocument(src) { switch (key) { case "url": if (typeof window !== "undefined") { - // The full path is required in the 'url' field. - params[key] = new URL(value, window.location).href; + try { + // The full path is required in the 'url' field. + params[key] = new URL(value, window.location).href; + continue; + } catch (ex) { + warn(`Cannot create valid URL: "${ex}".`); + } + } else if (typeof value === "string" || value instanceof URL) { + params[key] = value.toString(); // Support Node.js environments. continue; } - break; + throw new Error( + "Invalid PDF url data: " + + "either string or URL-object is expected in the url property." + ); case "range": rangeTransport = value; continue; diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 791abdad6..22ea78f13 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -73,6 +73,36 @@ describe("api", function () { } describe("getDocument", function () { + it("creates pdf doc from URL-string", async function () { + const urlStr = TEST_PDFS_PATH + basicApiFileName; + const loadingTask = getDocument(urlStr); + const pdfDocument = await loadingTask.promise; + + expect(typeof urlStr).toEqual("string"); + expect(pdfDocument instanceof PDFDocumentProxy).toEqual(true); + expect(pdfDocument.numPages).toEqual(3); + + await loadingTask.destroy(); + }); + + it("creates pdf doc from URL-object", async function () { + if (isNodeJS) { + pending("window.location is not supported in Node.js."); + } + const urlObj = new URL( + TEST_PDFS_PATH + basicApiFileName, + window.location + ); + const loadingTask = getDocument(urlObj); + const pdfDocument = await loadingTask.promise; + + expect(urlObj instanceof URL).toEqual(true); + expect(pdfDocument instanceof PDFDocumentProxy).toEqual(true); + expect(pdfDocument.numPages).toEqual(3); + + await loadingTask.destroy(); + }); + it("creates pdf doc from URL", function (done) { const loadingTask = getDocument(basicApiGetDocumentParams);