simpleviewer.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright 2014 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. "use strict";
  16. if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
  17. // eslint-disable-next-line no-alert
  18. alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
  19. }
  20. // The workerSrc property shall be specified.
  21. //
  22. pdfjsLib.GlobalWorkerOptions.workerSrc =
  23. "../../node_modules/pdfjs-dist/build/pdf.worker.js";
  24. // Some PDFs need external cmaps.
  25. //
  26. const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
  27. const CMAP_PACKED = true;
  28. const DEFAULT_URL = "../../web/compressed.tracemonkey-pldi-09.pdf";
  29. // To test the AcroForm and/or scripting functionality, try e.g. this file:
  30. // "../../test/pdfs/160F-2019.pdf"
  31. const ENABLE_XFA = true;
  32. const SEARCH_FOR = ""; // try "Mozilla";
  33. const SANDBOX_BUNDLE_SRC = "../../node_modules/pdfjs-dist/build/pdf.sandbox.js";
  34. const container = document.getElementById("viewerContainer");
  35. const eventBus = new pdfjsViewer.EventBus();
  36. // (Optionally) enable hyperlinks within PDF files.
  37. const pdfLinkService = new pdfjsViewer.PDFLinkService({
  38. eventBus,
  39. });
  40. // (Optionally) enable find controller.
  41. const pdfFindController = new pdfjsViewer.PDFFindController({
  42. eventBus,
  43. linkService: pdfLinkService,
  44. });
  45. // (Optionally) enable scripting support.
  46. const pdfScriptingManager = new pdfjsViewer.PDFScriptingManager({
  47. eventBus,
  48. sandboxBundleSrc: SANDBOX_BUNDLE_SRC,
  49. });
  50. const pdfViewer = new pdfjsViewer.PDFViewer({
  51. container,
  52. eventBus,
  53. linkService: pdfLinkService,
  54. findController: pdfFindController,
  55. scriptingManager: pdfScriptingManager,
  56. });
  57. pdfLinkService.setViewer(pdfViewer);
  58. pdfScriptingManager.setViewer(pdfViewer);
  59. eventBus.on("pagesinit", function () {
  60. // We can use pdfViewer now, e.g. let's change default scale.
  61. pdfViewer.currentScaleValue = "page-width";
  62. // We can try searching for things.
  63. if (SEARCH_FOR) {
  64. eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
  65. }
  66. });
  67. // Loading document.
  68. const loadingTask = pdfjsLib.getDocument({
  69. url: DEFAULT_URL,
  70. cMapUrl: CMAP_URL,
  71. cMapPacked: CMAP_PACKED,
  72. enableXfa: ENABLE_XFA,
  73. });
  74. (async function () {
  75. const pdfDocument = await loadingTask.promise;
  76. // Document loaded, specifying document for the viewer and
  77. // the (optional) linkService.
  78. pdfViewer.setDocument(pdfDocument);
  79. pdfLinkService.setDocument(pdfDocument, null);
  80. })();