fork from https://github.com/mozilla/pdf.js.git
This commit is contained in:
66
test/components/simple-viewer.html
Normal file
66
test/components/simple-viewer.html
Normal file
@@ -0,0 +1,66 @@
|
||||
<!doctype html>
|
||||
<!--
|
||||
Copyright 2026 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.
|
||||
-->
|
||||
|
||||
<html dir="ltr" mozdisallowselectionprint lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
|
||||
<title>PDF.js — Simple viewer</title>
|
||||
|
||||
<style>
|
||||
body {
|
||||
background-color: #808080;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
#viewerContainer {
|
||||
overflow: auto;
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
<link rel="resource" type="application/l10n" href="../../web/locale/locale.json" />
|
||||
<link rel="stylesheet" href="../../web/pdf_viewer.css" />
|
||||
<script type="importmap">
|
||||
{
|
||||
"imports": {
|
||||
"pdfjs/": "../../src/",
|
||||
"pdfjs-lib": "../../src/pdf.js",
|
||||
|
||||
"display-binary_data_factory": "../../src/display/binary_data_factory.js",
|
||||
"display-network_stream": "../../src/display/network_stream.js",
|
||||
"display-node_utils": "../../src/display/stubs.js",
|
||||
|
||||
"fluent-bundle": "../../node_modules/@fluent/bundle/esm/index.js",
|
||||
"fluent-dom": "../../node_modules/@fluent/dom/esm/index.js",
|
||||
"cached-iterable": "../../node_modules/cached-iterable/src/index.mjs",
|
||||
|
||||
"web-null_l10n": "../../web/genericl10n.js"
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<script src="simple-viewer.js" type="module"></script>
|
||||
</head>
|
||||
|
||||
<body tabindex="1">
|
||||
<div id="viewerContainer">
|
||||
<div id="viewer" class="pdfViewer"></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
113
test/components/simple-viewer.js
Normal file
113
test/components/simple-viewer.js
Normal file
@@ -0,0 +1,113 @@
|
||||
/* Copyright 2026 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.
|
||||
*/
|
||||
|
||||
import { getDocument, GlobalWorkerOptions } from "pdfjs-lib";
|
||||
import { EventBus } from "../../web/event_utils.js";
|
||||
import { GenericL10n } from "../../web/genericl10n.js";
|
||||
import { PDFFindController } from "../../web/pdf_find_controller.js";
|
||||
import { PDFLinkService } from "../../web/pdf_link_service.js";
|
||||
import { PDFScriptingManager } from "../../web/pdf_scripting_manager.component.js";
|
||||
import { PDFViewer } from "../../web/pdf_viewer.js";
|
||||
|
||||
// The workerSrc property shall be specified.
|
||||
//
|
||||
GlobalWorkerOptions.workerSrc =
|
||||
typeof PDFJSDev === "undefined"
|
||||
? "../../src/pdf.worker.js"
|
||||
: "../../build/pdf.worker.mjs";
|
||||
|
||||
// Some PDFs need external cmaps.
|
||||
//
|
||||
const CMAP_URL =
|
||||
typeof PDFJSDev === "undefined"
|
||||
? "../../external/bcmaps/"
|
||||
: "../../web/cmaps/";
|
||||
|
||||
const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||
|
||||
const ENABLE_XFA = true;
|
||||
const SEARCH_FOR = ""; // try "Mozilla";
|
||||
|
||||
const SANDBOX_BUNDLE_SRC = new URL(
|
||||
typeof PDFJSDev === "undefined"
|
||||
? "../../build/generic/build/pdf.sandbox.mjs"
|
||||
: "../../build/pdf.sandbox.mjs",
|
||||
window.location
|
||||
);
|
||||
|
||||
const WASM_URL =
|
||||
typeof PDFJSDev === "undefined"
|
||||
? "../../build/generic/web/wasm/"
|
||||
: "../../build/wasm/";
|
||||
|
||||
const fileUrl = new URLSearchParams(location.search).get("file") ?? DEFAULT_URL;
|
||||
|
||||
const container = document.getElementById("viewerContainer");
|
||||
|
||||
const eventBus = new EventBus();
|
||||
|
||||
// (Optionally) enable hyperlinks within PDF files.
|
||||
const pdfLinkService = new PDFLinkService({
|
||||
eventBus,
|
||||
});
|
||||
|
||||
// (Optionally) enable find controller.
|
||||
const pdfFindController = new PDFFindController({
|
||||
eventBus,
|
||||
linkService: pdfLinkService,
|
||||
});
|
||||
|
||||
// (Optionally) enable scripting support.
|
||||
const pdfScriptingManager = new PDFScriptingManager({
|
||||
eventBus,
|
||||
sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
|
||||
wasmUrl: WASM_URL,
|
||||
});
|
||||
|
||||
const pdfViewer = new PDFViewer({
|
||||
container,
|
||||
eventBus,
|
||||
l10n: new GenericL10n(navigator.language),
|
||||
linkService: pdfLinkService,
|
||||
findController: pdfFindController,
|
||||
scriptingManager: pdfScriptingManager,
|
||||
});
|
||||
pdfLinkService.setViewer(pdfViewer);
|
||||
pdfScriptingManager.setViewer(pdfViewer);
|
||||
window.pdfScriptingManager = pdfScriptingManager;
|
||||
|
||||
eventBus.on("pagesinit", function () {
|
||||
// We can use pdfViewer now, e.g. let's change default scale.
|
||||
pdfViewer.currentScaleValue = "page-width";
|
||||
|
||||
// We can try searching for things.
|
||||
if (SEARCH_FOR) {
|
||||
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
|
||||
}
|
||||
});
|
||||
|
||||
// Loading document.
|
||||
const loadingTask = getDocument({
|
||||
url: fileUrl,
|
||||
cMapUrl: CMAP_URL,
|
||||
enableXfa: ENABLE_XFA,
|
||||
});
|
||||
|
||||
const pdfDocument = await loadingTask.promise;
|
||||
// Document loaded, specifying document for the viewer and
|
||||
// the (optional) linkService.
|
||||
pdfViewer.setDocument(pdfDocument);
|
||||
|
||||
pdfLinkService.setDocument(pdfDocument, null);
|
||||
Reference in New Issue
Block a user