(functionbook() { // var url = '你要展示的pdf文件路径'; //這邊是要秀的pdf var pdfDoc = null, pageNum = 1, pageRendering = false, pageNumPending = null, canvas = document.getElementById('the-book'), // 抓html ID ctx = canvas.getContext('2d');
/** * Get page info from document, resize canvas accordingly, and render page. * @param num Page number. */ functionrenderPage(num) { pageRendering = true; // Using promise to fetch the page pdfDoc.getPage(num).then(function (page) { // 直接按照783来配scale var viewport = page.getViewport({ scale: 2 });
// 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; } }); }); }
/** * If another page rendering in progress, waits until the rendering is * finised. Otherwise, executes rendering immediately. */ functionqueueRenderPage(num) { if (pageRendering) { pageNumPending = num; } else { renderPage(num); } }