|
| 1 | +/* Copyright 2026 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { getDocument, GlobalWorkerOptions } from "pdfjs-lib"; |
| 17 | +import { EventBus } from "../../web/event_utils.js"; |
| 18 | +import { GenericL10n } from "../../web/genericl10n.js"; |
| 19 | +import { PDFFindController } from "../../web/pdf_find_controller.js"; |
| 20 | +import { PDFLinkService } from "../../web/pdf_link_service.js"; |
| 21 | +import { PDFScriptingManager } from "../../web/pdf_scripting_manager.js"; |
| 22 | +import { PDFViewer } from "../../web/pdf_viewer.js"; |
| 23 | + |
| 24 | +// The workerSrc property shall be specified. |
| 25 | +// |
| 26 | +GlobalWorkerOptions.workerSrc = |
| 27 | + typeof PDFJSDev === "undefined" |
| 28 | + ? "../../src/pdf.worker.js" |
| 29 | + : "../../build/pdf.worker.mjs"; |
| 30 | + |
| 31 | +// Some PDFs need external cmaps. |
| 32 | +// |
| 33 | +const CMAP_URL = |
| 34 | + typeof PDFJSDev === "undefined" |
| 35 | + ? "../../external/bcmaps/" |
| 36 | + : "../../web/cmaps/"; |
| 37 | + |
| 38 | +const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf"; |
| 39 | + |
| 40 | +const ENABLE_XFA = true; |
| 41 | +const SEARCH_FOR = ""; // try "Mozilla"; |
| 42 | + |
| 43 | +const SANDBOX_BUNDLE_SRC = new URL( |
| 44 | + typeof PDFJSDev === "undefined" |
| 45 | + ? "../../src/pdf.sandbox.js" |
| 46 | + : "../../build/pdf.sandbox.mjs", |
| 47 | + window.location |
| 48 | +); |
| 49 | + |
| 50 | +const fileUrl = new URLSearchParams(location.search).get("file") ?? DEFAULT_URL; |
| 51 | + |
| 52 | +const container = document.getElementById("viewerContainer"); |
| 53 | + |
| 54 | +const eventBus = new EventBus(); |
| 55 | + |
| 56 | +// (Optionally) enable hyperlinks within PDF files. |
| 57 | +const pdfLinkService = new PDFLinkService({ |
| 58 | + eventBus, |
| 59 | +}); |
| 60 | + |
| 61 | +// (Optionally) enable find controller. |
| 62 | +const pdfFindController = new PDFFindController({ |
| 63 | + eventBus, |
| 64 | + linkService: pdfLinkService, |
| 65 | +}); |
| 66 | + |
| 67 | +// (Optionally) enable scripting support. |
| 68 | +const pdfScriptingManager = new PDFScriptingManager({ |
| 69 | + eventBus, |
| 70 | + sandboxBundleSrc: SANDBOX_BUNDLE_SRC, |
| 71 | +}); |
| 72 | + |
| 73 | +const pdfViewer = new PDFViewer({ |
| 74 | + container, |
| 75 | + eventBus, |
| 76 | + l10n: new GenericL10n(navigator.language), |
| 77 | + linkService: pdfLinkService, |
| 78 | + findController: pdfFindController, |
| 79 | + scriptingManager: pdfScriptingManager, |
| 80 | +}); |
| 81 | +pdfLinkService.setViewer(pdfViewer); |
| 82 | +pdfScriptingManager.setViewer(pdfViewer); |
| 83 | + |
| 84 | +eventBus.on("pagesinit", function () { |
| 85 | + // We can use pdfViewer now, e.g. let's change default scale. |
| 86 | + pdfViewer.currentScaleValue = "page-width"; |
| 87 | + |
| 88 | + // We can try searching for things. |
| 89 | + if (SEARCH_FOR) { |
| 90 | + eventBus.dispatch("find", { type: "", query: SEARCH_FOR }); |
| 91 | + } |
| 92 | +}); |
| 93 | + |
| 94 | +// Loading document. |
| 95 | +const loadingTask = getDocument({ |
| 96 | + url: fileUrl, |
| 97 | + cMapUrl: CMAP_URL, |
| 98 | + enableXfa: ENABLE_XFA, |
| 99 | +}); |
| 100 | + |
| 101 | +const pdfDocument = await loadingTask.promise; |
| 102 | +// Document loaded, specifying document for the viewer and |
| 103 | +// the (optional) linkService. |
| 104 | +pdfViewer.setDocument(pdfDocument); |
| 105 | + |
| 106 | +pdfLinkService.setDocument(pdfDocument, null); |
0 commit comments