Skip to content

Commit 3031fca

Browse files
committed
[api-minor] Move the viewer scripting initialization/handling into a new PDFScriptingManager class
The *main* purpose of this patch is to allow scripting to be used together with the viewer components, note the updated "simpleviewer"/"singlepageviewer" examples, in addition to the full default viewer. Given how the scripting functionality is currently implemented in the default viewer, trying to re-use this with the standalone viewer components would be *very* hard and ideally you'd want it to work out-of-the-box. For an initial implementation, in the default viewer, of the scripting functionality it probably made sense to simply dump all of the code in the `app.js` file, however that cannot be used with the viewer components. To address this, the functionality is moved into a new `PDFScriptingManager` class which can thus be handled in the same way as all other viewer components (and e.g. be passed to the `BaseViewer`-implementations). Obviously the scripting functionality needs quite a lot of data, during its initialization, and for the default viewer we want to maintain the current way of doing the lookups since that helps avoid a number of redundant API-calls. To that end, the `PDFScriptingManager` implementation accepts (optional) factories/functions such that we can maintain the current behaviour for the default viewer. For the viewer components specifically, fallback code-paths are provided to ensure that scripting will "just work"[1]. Besides moving the viewer handling of the scripting code to its own file/class, this patch also takes the opportunity to re-factor the functionality into a number of helper methods to improve overall readability[2]. Note that it's definitely possible that the `PDFScriptingManager` class could be improved even further (e.g. for general re-use), since it's still heavily tailored to the default viewer use-case, however I believe that this patch is still a good step forward overall. --- [1] Obviously *all* the relevant document properties might not be available in the viewer components use-case (e.g. the various URLs), but most things should work just fine. [2] The old `PDFViewerApplication._initializeJavaScript` method, where everything was simply inlined, have over time (in my opinion) become quite large and somewhat difficult to *easily* reason about.
1 parent 80017a9 commit 3031fca

8 files changed

+509
-247
lines changed

examples/components/simpleviewer.js

+15
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ var CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
3131
var CMAP_PACKED = true;
3232

3333
var DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
34+
// To test the AcroForm and/or scripting functionality, try e.g. this file:
35+
// var DEFAULT_URL = "../../test/pdfs/160F-2019.pdf";
36+
3437
var SEARCH_FOR = ""; // try 'Mozilla';
3538

39+
// For scripting support, note also `enableScripting` below.
40+
var SANDBOX_BUNDLE_SRC = "../../node_modules/pdfjs-dist/build/pdf.sandbox.js";
41+
3642
var container = document.getElementById("viewerContainer");
3743

3844
var eventBus = new pdfjsViewer.EventBus();
@@ -48,13 +54,22 @@ var pdfFindController = new pdfjsViewer.PDFFindController({
4854
linkService: pdfLinkService,
4955
});
5056

57+
// (Optionally) enable scripting support.
58+
var pdfScriptingManager = new pdfjsViewer.PDFScriptingManager({
59+
eventBus,
60+
sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
61+
});
62+
5163
var pdfViewer = new pdfjsViewer.PDFViewer({
5264
container,
5365
eventBus,
5466
linkService: pdfLinkService,
5567
findController: pdfFindController,
68+
scriptingManager: pdfScriptingManager,
69+
enableScripting: true,
5670
});
5771
pdfLinkService.setViewer(pdfViewer);
72+
pdfScriptingManager.setViewer(pdfViewer);
5873

5974
eventBus.on("pagesinit", function () {
6075
// We can use pdfViewer now, e.g. let's change default scale.

examples/components/singlepageviewer.js

+15
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ var CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
3131
var CMAP_PACKED = true;
3232

3333
var DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
34+
// To test the AcroForm and/or scripting functionality, try e.g. this file:
35+
// var DEFAULT_URL = "../../test/pdfs/160F-2019.pdf";
36+
3437
var SEARCH_FOR = ""; // try 'Mozilla';
3538

39+
// For scripting support, note also `enableScripting` below.
40+
var SANDBOX_BUNDLE_SRC = "../../node_modules/pdfjs-dist/build/pdf.sandbox.js";
41+
3642
var container = document.getElementById("viewerContainer");
3743

3844
var eventBus = new pdfjsViewer.EventBus();
@@ -48,13 +54,22 @@ var pdfFindController = new pdfjsViewer.PDFFindController({
4854
linkService: pdfLinkService,
4955
});
5056

57+
// (Optionally) enable scripting support.
58+
var pdfScriptingManager = new pdfjsViewer.PDFScriptingManager({
59+
eventBus,
60+
sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
61+
});
62+
5163
var pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
5264
container,
5365
eventBus,
5466
linkService: pdfLinkService,
5567
findController: pdfFindController,
68+
scriptingManager: pdfScriptingManager,
69+
enableScripting: true,
5670
});
5771
pdfLinkService.setViewer(pdfSinglePageViewer);
72+
pdfScriptingManager.setViewer(pdfSinglePageViewer);
5873

5974
eventBus.on("pagesinit", function () {
6075
// We can use pdfSinglePageViewer now, e.g. let's change default scale.

0 commit comments

Comments
 (0)