pageviewer.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.PDFPageView) {
  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. const PAGE_TO_VIEW = 1;
  30. const SCALE = 1.0;
  31. const ENABLE_XFA = true;
  32. const container = document.getElementById("pageContainer");
  33. const eventBus = new pdfjsViewer.EventBus();
  34. // Loading document.
  35. const loadingTask = pdfjsLib.getDocument({
  36. url: DEFAULT_URL,
  37. cMapUrl: CMAP_URL,
  38. cMapPacked: CMAP_PACKED,
  39. enableXfa: ENABLE_XFA,
  40. });
  41. (async function () {
  42. const pdfDocument = await loadingTask.promise;
  43. // Document loaded, retrieving the page.
  44. const pdfPage = await pdfDocument.getPage(PAGE_TO_VIEW);
  45. const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(pdfjsLib.version);
  46. if (match && (match[1] | 0) >= 3 && (match[2] | 0) >= 2) {
  47. // Creating the page view with default parameters.
  48. const pdfPageView = new pdfjsViewer.PDFPageView({
  49. container,
  50. id: PAGE_TO_VIEW,
  51. scale: SCALE,
  52. defaultViewport: pdfPage.getViewport({ scale: SCALE }),
  53. eventBus,
  54. });
  55. // Associate the actual page with the view, and draw it.
  56. pdfPageView.setPdfPage(pdfPage);
  57. return pdfPageView.draw();
  58. }
  59. // Creating the page view with default parameters.
  60. const pdfPageView = new pdfjsViewer.PDFPageView({
  61. container,
  62. id: PAGE_TO_VIEW,
  63. scale: SCALE,
  64. defaultViewport: pdfPage.getViewport({ scale: SCALE }),
  65. eventBus,
  66. // We can enable text/annotation/xfa/struct-layers, as needed.
  67. textLayerFactory: !pdfDocument.isPureXfa
  68. ? new pdfjsViewer.DefaultTextLayerFactory()
  69. : null,
  70. annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),
  71. xfaLayerFactory: pdfDocument.isPureXfa
  72. ? new pdfjsViewer.DefaultXfaLayerFactory()
  73. : null,
  74. structTreeLayerFactory: new pdfjsViewer.DefaultStructTreeLayerFactory(),
  75. });
  76. // Associate the actual page with the view, and draw it.
  77. pdfPageView.setPdfPage(pdfPage);
  78. return pdfPageView.draw();
  79. })();