-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (56 loc) · 1.54 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const url = 'ur-url or ur-file.pdf';
let pdfDoc = null,
pageNum = 1,
pageIsRendering = false,
pageNumIsPending = null,
scale = 1,
canvas = document.getElementById('pdf-render'),
ctx = canvas.getContext('2d');
pdfjsLib.getDocument(url).promise.then((pdfDoc_) => {
pdfDoc = pdfDoc_;
document.getElementById('page-count').textContent = pdfDoc.numPages;
renderPage(pageNum);
});
function renderPage(num){
pageIsRendering = true;
pdfDoc.getPage(num).then((page) => {
const viewport = page.getViewport({scale});
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: ctx,
viewport
};
const renderTask = page.render(renderContext);
renderTask.promise.then(() => {
pageIsRendering = false;
if(pageNumIsPending !== null){
renderPage(pageNumIsPending);
pageNumIsPending = null;
}
});
document.getElementById('page-num').textContent = num;
});
}
function queueRenderPage(num){
if (pageIsRendering){
pageNumIsPending = num;
}
else{
renderPage(num);
}
}
document.getElementById('p-page').addEventListener('click',() => {
if (pageNum <= 1){
return;
}
pageNum--;
queueRenderPage(pageNum);
});
document.getElementById('n-page').addEventListener('click',() =>{
if (pageNum>= pdfDoc.numPages){
return;
}
pageNum++;
queueRenderPage(pageNum);
});