123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <html>
- <head>
- <title>pdf.js展示1,上一页,下一页</title>
- </head>
- <h1>PDF.js Previous/Next example</h1>
- <div>
- <button id="prev">Previous</button>
- <button id="next">Next</button>
-
- <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
- </div>
- <canvas id="the-canvas"></canvas>
- <script src="./pdf.js"></script>
- <script src="./pdf.worker.js"></script>
- <script>
- var url = 'http://new-media-ca.oss-cn-beijing.aliyuncs.com/upload/ca_signs/2020-09-04/kx-5f52369304d06.pdf';
- var pdfDoc = null,
- pageNum = 1,
- pageRendering = false,
- pageNumPending = null,
- scale = 0.8,
- canvas = document.getElementById('the-canvas'),
- ctx = canvas.getContext('2d');
- /**
- * Get page info from document, resize canvas accordingly, and render page.
- * @param num Page number.
- */
- function renderPage(num) {
- pageRendering = true;
- // Using promise to fetch the page
- pdfDoc.getPage(num).then(function(page) {
- var viewport = page.getViewport(scale);
- canvas.height = viewport.height;
- canvas.width = viewport.width;
- // Render PDF page into canvas context
- var renderContext = {
- canvasContext: ctx,
- viewport: viewport
- };
- var renderTask = page.render(renderContext);
- // Wait for rendering to finish
- renderTask.promise.then(function() {
- pageRendering = false;
- if (pageNumPending !== null) {
- // New page rendering is pending
- renderPage(pageNumPending);
- pageNumPending = null;
- }
- });
- });
- // Update page counters
- document.getElementById('page_num').textContent = num;
- }
- /**
- * If another page rendering in progress, waits until the rendering is
- * finised. Otherwise, executes rendering immediately.
- */
- function queueRenderPage(num) {
- if (pageRendering) {
- pageNumPending = num;
- } else {
- renderPage(num);
- }
- }
- /**
- * Displays previous page.
- */
- function onPrevPage() {
- if (pageNum <= 1) {
- return;
- }
- pageNum--;
- queueRenderPage(pageNum);
- }
- document.getElementById('prev').addEventListener('click', onPrevPage);
- /**
- * Displays next page.
- */
- function onNextPage() {
- if (pageNum >= pdfDoc.numPages) {
- return;
- }
- pageNum++;
- queueRenderPage(pageNum);
- }
- document.getElementById('next').addEventListener('click', onNextPage);
- /**
- * Asynchronously downloads PDF.
- */
- const CMAP_URL = 'https://unpkg.com/pdfjs-dist@2.0.489/cmaps/';
- pdfjsLib.GlobalWorkerOptions.workerSrc ='./pdf.worker.js';//自己的路径
- var loadingTask = pdfjsLib.getDocument({
- data: url,
- cMapUrl: CMAP_URL,
- cMapPacked: true,
- });
- loadingTask.promise.then(function(pdfDoc_) {
- pdfDoc = pdfDoc_;
- document.getElementById('page_count').textContent = pdfDoc.numPages;
- // Initial/first page rendering
- renderPage(pageNum);
- });
- // const loadingTask = pdfjsLib.getDocument(/* data here */);
- // loadingTask.promise.then(pdfDocument => {
- // // The document is loaded here...
- // });
- </script>
- </html>
|