2014-09-30 12:22:38 -05:00
|
|
|
/* Copyright 2014 Mozilla Foundation
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-12-06 13:55:15 +01:00
|
|
|
if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
|
2021-01-22 17:38:26 +01:00
|
|
|
// eslint-disable-next-line no-alert
|
2019-12-25 20:03:46 +01:00
|
|
|
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
|
2014-09-30 12:22:38 -05:00
|
|
|
}
|
|
|
|
|
|
2015-11-06 07:39:13 -06:00
|
|
|
// The workerSrc property shall be specified.
|
2014-09-30 12:22:38 -05:00
|
|
|
//
|
2018-03-15 13:49:28 -07:00
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc =
|
2023-10-14 11:24:56 +02:00
|
|
|
"../../node_modules/pdfjs-dist/build/pdf.worker.mjs";
|
2014-09-30 12:22:38 -05:00
|
|
|
|
2014-09-30 15:42:28 -05:00
|
|
|
// Some PDFs need external cmaps.
|
2014-09-30 12:22:38 -05:00
|
|
|
//
|
2021-03-12 17:08:17 +01:00
|
|
|
const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
|
|
|
|
|
const CMAP_PACKED = true;
|
2014-09-30 12:22:38 -05:00
|
|
|
|
2021-03-12 17:08:17 +01:00
|
|
|
const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
2021-03-05 00:15:18 +01:00
|
|
|
// To test the AcroForm and/or scripting functionality, try e.g. this file:
|
2021-09-08 12:33:59 +02:00
|
|
|
// "../../test/pdfs/160F-2019.pdf"
|
|
|
|
|
|
|
|
|
|
const ENABLE_XFA = true;
|
|
|
|
|
const SEARCH_FOR = ""; // try "Mozilla";
|
2021-03-05 00:15:18 +01:00
|
|
|
|
2023-10-14 11:24:56 +02:00
|
|
|
const SANDBOX_BUNDLE_SRC = new URL(
|
|
|
|
|
"../../node_modules/pdfjs-dist/build/pdf.sandbox.mjs",
|
|
|
|
|
window.location
|
|
|
|
|
);
|
2021-03-05 00:15:18 +01:00
|
|
|
|
2026-04-23 20:57:11 +02:00
|
|
|
const WASM_URL = "../../node_modules/pdfjs-dist/build/wasm/";
|
|
|
|
|
|
2021-03-12 17:08:17 +01:00
|
|
|
const container = document.getElementById("viewerContainer");
|
2015-06-25 12:24:29 +02:00
|
|
|
|
2021-03-12 17:08:17 +01:00
|
|
|
const eventBus = new pdfjsViewer.EventBus();
|
2020-02-26 17:16:30 +01:00
|
|
|
|
2015-06-25 12:24:29 +02:00
|
|
|
// (Optionally) enable hyperlinks within PDF files.
|
2021-03-12 17:08:17 +01:00
|
|
|
const pdfLinkService = new pdfjsViewer.PDFLinkService({
|
2021-01-22 17:38:26 +01:00
|
|
|
eventBus,
|
2020-02-26 17:16:30 +01:00
|
|
|
});
|
2015-06-25 12:24:29 +02:00
|
|
|
|
2016-04-13 14:39:29 -05:00
|
|
|
// (Optionally) enable find controller.
|
2021-03-12 17:08:17 +01:00
|
|
|
const pdfFindController = new pdfjsViewer.PDFFindController({
|
2021-01-22 17:38:26 +01:00
|
|
|
eventBus,
|
2018-09-21 19:40:11 +02:00
|
|
|
linkService: pdfLinkService,
|
2016-04-13 14:39:29 -05:00
|
|
|
});
|
2018-09-22 19:44:13 +02:00
|
|
|
|
2021-03-05 00:15:18 +01:00
|
|
|
// (Optionally) enable scripting support.
|
2021-03-12 17:08:17 +01:00
|
|
|
const pdfScriptingManager = new pdfjsViewer.PDFScriptingManager({
|
2021-03-05 00:15:18 +01:00
|
|
|
eventBus,
|
|
|
|
|
sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
|
2026-04-23 20:57:11 +02:00
|
|
|
wasmUrl: WASM_URL,
|
2021-03-05 00:15:18 +01:00
|
|
|
});
|
|
|
|
|
|
2021-03-12 17:08:17 +01:00
|
|
|
const pdfViewer = new pdfjsViewer.PDFViewer({
|
2021-01-22 17:38:26 +01:00
|
|
|
container,
|
|
|
|
|
eventBus,
|
2018-09-22 19:44:13 +02:00
|
|
|
linkService: pdfLinkService,
|
|
|
|
|
findController: pdfFindController,
|
2021-03-05 00:15:18 +01:00
|
|
|
scriptingManager: pdfScriptingManager,
|
2018-09-22 19:44:13 +02:00
|
|
|
});
|
|
|
|
|
pdfLinkService.setViewer(pdfViewer);
|
2021-03-05 00:15:18 +01:00
|
|
|
pdfScriptingManager.setViewer(pdfViewer);
|
2016-04-13 14:39:29 -05:00
|
|
|
|
2020-04-14 12:28:14 +02:00
|
|
|
eventBus.on("pagesinit", function () {
|
2015-06-25 12:24:29 +02:00
|
|
|
// We can use pdfViewer now, e.g. let's change default scale.
|
2014-09-30 12:31:58 -05:00
|
|
|
pdfViewer.currentScaleValue = "page-width";
|
2016-04-13 14:39:29 -05:00
|
|
|
|
2019-12-25 20:03:46 +01:00
|
|
|
// We can try searching for things.
|
2016-04-13 14:39:29 -05:00
|
|
|
if (SEARCH_FOR) {
|
2022-03-02 11:21:47 +01:00
|
|
|
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
|
2016-04-13 14:39:29 -05:00
|
|
|
}
|
2014-09-30 12:22:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Loading document.
|
2021-03-12 17:08:17 +01:00
|
|
|
const loadingTask = pdfjsLib.getDocument({
|
2018-02-17 16:57:24 +01:00
|
|
|
url: DEFAULT_URL,
|
|
|
|
|
cMapUrl: CMAP_URL,
|
|
|
|
|
cMapPacked: CMAP_PACKED,
|
2021-09-08 12:33:59 +02:00
|
|
|
enableXfa: ENABLE_XFA,
|
2018-11-08 13:40:06 +01:00
|
|
|
});
|
2015-06-25 12:24:29 +02:00
|
|
|
|
2023-10-14 11:24:56 +02:00
|
|
|
const pdfDocument = await loadingTask.promise;
|
|
|
|
|
// Document loaded, specifying document for the viewer and
|
|
|
|
|
// the (optional) linkService.
|
|
|
|
|
pdfViewer.setDocument(pdfDocument);
|
|
|
|
|
|
|
|
|
|
pdfLinkService.setDocument(pdfDocument, null);
|