pdf.js 入門使用指南2-顯示篇
阿新 • • 發佈:2018-11-25
一:如何控制pdf縮放
var viewport = page.getViewport(scale);
引數:scale,0到1之間的小數,比如0.8,比如100%顯示,填寫1,參考如下
var viewport = page.getViewport(1);
部分程式碼參考:
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 1,//縮放比例
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);
2:顯示效果
ps:很顯然,即便是100%顯示,仍然比較小,模糊
3:dpi 的原因
如上所描述,pdf顯示小,不夠清晰。有一個重要的原因是預設dpi為96,然後網頁一般使用dpi為72。
具體dpi ,請搜尋百度,這裡不再解釋。
程式碼調整:
var CSS_UNITS = 96.0/ 72.0;
var renderContext = {
transform: [CSS_UNITS, 0, 0, CSS_UNITS, 0, 0],
canvasContext: ctx,
viewport: viewport
};
transform:就是和縮放相關的引數
網頁完成程式碼如下:
<html>
<head>
<title>pdf.js展示1,上一頁,下一頁</title>
<meta charset="UTF-8">
</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="js/pdf.js"></script>
<script src="js/pdf.worker.js"></script>
<script>
var url = '/pdf/doc/demo1.pdf';
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 1,//縮放比例
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);
var CSS_UNITS = 96.0 / 72.0;
canvas.height = Math.floor(viewport.height * CSS_UNITS);
canvas.width = Math.floor(viewport.width * CSS_UNITS) ;
// Render PDF page into canvas context
var renderContext = {
transform: [CSS_UNITS, 0, 0, CSS_UNITS, 0, 0],
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.
*/
PDFJS.getDocument(url).then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
</script>
</html>
4:顯示效果如下:
轉換後,如上圖
轉換前,如上圖,作為對比