singlepageviewer.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.PDFSinglePageViewer) {
  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 pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
  51. container,
  52. eventBus,
  53. linkService: pdfLinkService,
  54. findController: pdfFindController,
  55. scriptingManager: pdfScriptingManager,
  56. });
  57. pdfLinkService.setViewer(pdfSinglePageViewer);
  58. pdfScriptingManager.setViewer(pdfSinglePageViewer);
  59. eventBus.on("pagesinit", function () {
  60. // We can use pdfSinglePageViewer now, e.g. let's change default scale.
  61. pdfSinglePageViewer.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. loadingTask.promise.then(function (pdfDocument) {
  75. // Document loaded, specifying document for the viewer and
  76. // the (optional) linkService.
  77. pdfSinglePageViewer.setDocument(pdfDocument);
  78. pdfLinkService.setDocument(pdfDocument, null);
  79. });