main.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Any copyright is dedicated to the Public Domain.
  2. // http://creativecommons.org/licenses/publicdomain/
  3. // Hello world example for webpack.
  4. const pdfjsLib = require("pdfjs-dist");
  5. const pdfPath = "../learning/helloworld.pdf";
  6. // Setting worker path to worker bundle.
  7. pdfjsLib.GlobalWorkerOptions.workerSrc =
  8. "../../build/webpack/pdf.worker.bundle.js";
  9. // Loading a document.
  10. const loadingTask = pdfjsLib.getDocument(pdfPath);
  11. loadingTask.promise
  12. .then(function (pdfDocument) {
  13. // Request a first page
  14. return pdfDocument.getPage(1).then(function (pdfPage) {
  15. // Display page on the existing canvas with 100% scale.
  16. const viewport = pdfPage.getViewport({ scale: 1.0 });
  17. const canvas = document.getElementById("theCanvas");
  18. canvas.width = viewport.width;
  19. canvas.height = viewport.height;
  20. const ctx = canvas.getContext("2d");
  21. const renderTask = pdfPage.render({
  22. canvasContext: ctx,
  23. viewport,
  24. });
  25. return renderTask.promise;
  26. });
  27. })
  28. .catch(function (reason) {
  29. console.error("Error: " + reason);
  30. });