prevnext.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Previous/Next example</title>
  6. </head>
  7. <body>
  8. <h1>'Previous/Next' example</h1>
  9. <div>
  10. <button id="prev">Previous</button>
  11. <button id="next">Next</button>
  12. &nbsp; &nbsp;
  13. <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
  14. </div>
  15. <div>
  16. <canvas id="the-canvas" style="border: 1px solid black; direction: ltr;"></canvas>
  17. </div>
  18. <script src="../../node_modules/pdfjs-dist/build/pdf.js"></script>
  19. <script id="script">
  20. //
  21. // If absolute URL from the remote server is provided, configure the CORS
  22. // header on that server.
  23. //
  24. var url = '../../web/compressed.tracemonkey-pldi-09.pdf';
  25. //
  26. // In cases when the pdf.worker.js is located at the different folder than the
  27. // PDF.js's one, or the PDF.js is executed via eval(), the workerSrc property
  28. // shall be specified.
  29. //
  30. pdfjsLib.GlobalWorkerOptions.workerSrc =
  31. '../../node_modules/pdfjs-dist/build/pdf.worker.js';
  32. var pdfDoc = null,
  33. pageNum = 1,
  34. pageRendering = false,
  35. pageNumPending = null,
  36. scale = 0.8,
  37. canvas = document.getElementById('the-canvas'),
  38. ctx = canvas.getContext('2d');
  39. /**
  40. * Get page info from document, resize canvas accordingly, and render page.
  41. * @param num Page number.
  42. */
  43. function renderPage(num) {
  44. pageRendering = true;
  45. // Using promise to fetch the page
  46. pdfDoc.getPage(num).then(function(page) {
  47. var viewport = page.getViewport({ scale: scale, });
  48. // Support HiDPI-screens.
  49. var outputScale = window.devicePixelRatio || 1;
  50. canvas.width = Math.floor(viewport.width * outputScale);
  51. canvas.height = Math.floor(viewport.height * outputScale);
  52. canvas.style.width = Math.floor(viewport.width) + "px";
  53. canvas.style.height = Math.floor(viewport.height) + "px";
  54. var transform = outputScale !== 1
  55. ? [outputScale, 0, 0, outputScale, 0, 0]
  56. : null;
  57. // Render PDF page into canvas context
  58. var renderContext = {
  59. canvasContext: ctx,
  60. transform: transform,
  61. viewport: viewport,
  62. };
  63. var renderTask = page.render(renderContext);
  64. // Wait for rendering to finish
  65. renderTask.promise.then(function () {
  66. pageRendering = false;
  67. if (pageNumPending !== null) {
  68. // New page rendering is pending
  69. renderPage(pageNumPending);
  70. pageNumPending = null;
  71. }
  72. });
  73. });
  74. // Update page counters
  75. document.getElementById('page_num').textContent = num;
  76. }
  77. /**
  78. * If another page rendering in progress, waits until the rendering is
  79. * finished. Otherwise, executes rendering immediately.
  80. */
  81. function queueRenderPage(num) {
  82. if (pageRendering) {
  83. pageNumPending = num;
  84. } else {
  85. renderPage(num);
  86. }
  87. }
  88. /**
  89. * Displays previous page.
  90. */
  91. function onPrevPage() {
  92. if (pageNum <= 1) {
  93. return;
  94. }
  95. pageNum--;
  96. queueRenderPage(pageNum);
  97. }
  98. document.getElementById('prev').addEventListener('click', onPrevPage);
  99. /**
  100. * Displays next page.
  101. */
  102. function onNextPage() {
  103. if (pageNum >= pdfDoc.numPages) {
  104. return;
  105. }
  106. pageNum++;
  107. queueRenderPage(pageNum);
  108. }
  109. document.getElementById('next').addEventListener('click', onNextPage);
  110. /**
  111. * Asynchronously downloads PDF.
  112. */
  113. var loadingTask = pdfjsLib.getDocument(url);
  114. loadingTask.promise.then(function(pdfDoc_) {
  115. pdfDoc = pdfDoc_;
  116. document.getElementById('page_count').textContent = pdfDoc.numPages;
  117. // Initial/first page rendering
  118. renderPage(pageNum);
  119. });
  120. </script>
  121. </body>
  122. </html>