-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdf_service.js
141 lines (137 loc) · 4.07 KB
/
pdf_service.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function PDF_JS(initobj){
var initobj=initobj||{}
var obj={
containerId:'the-canvas'
}
for(var key in initobj){
obj[key]=initobj[key]
}
this.pdfDoc=null; //下载到的整体后的pdf
this.pageNum=1; //当前页码
this.scale=1;//缩放比率
this.totalPage=null;
this.pageNumPending=null;
this.canvas = document.getElementById(obj.containerId);
this.ctx = this.canvas.getContext('2d');
this.downloading=true; //PDF下载中标识
this.pageRendering=false; //单页pdf解析标识
this.pdfName=null;//当前PDF名称
this.cache={};
this.init();
}
PDF_JS.prototype={
init:function(){
var that=this
console.log('引入PDF_JS成功')
},
render:function(url,callback){
var that=this
that.rendercallback=callback
that.downloading=true
that.pdfName=that.getPdfName(url)
if(that.rendercallback&&that.rendercallback.cache){
that.cache=that.rendercallback.cache()
//that.cache.PDF=that.localGet()
}
that.rendercallback&&that.rendercallback.downloading&&that.rendercallback.downloading({
downloading:that.downloading
})
function pdfDoc(pdfDoc_){
that.pdfDoc = pdfDoc_;
that.downloading=false;
that.rendercallback&&that.rendercallback.downloading&&that.rendercallback.downloadingSuccess({
downloading:that.downloading
})
console.log('当前文档',that.pdfDoc)
that.totalPage=that.pdfDoc.numPages
that.renderPage(that.pageNum);
}
if(that.cache.PDF&&that.cache.PDF[that.pdfName]){
console.log('缓存中加载')
return pdfDoc(that.cache.PDF[that.pdfName])
}else{
PDFJS.getDocument(url).then(function(pdfDoc_) {
console.log('首次加载')
that.cache.PDF=that.cache.PDF||{}
that.cache.PDF[that.pdfName]=pdfDoc_//CircularJSON.parse(CircularJSON.stringify(pdfDoc_))
//that.localSet(that.cache.PDF)
pdfDoc(that.cache.PDF[that.pdfName])
});
}
},
renderPage:function(num){ //解析并渲染单页
var that=this
that.pageRendering = true; //单页渲染中
that.rendercallback&&that.rendercallback.pageRendering&&that.rendercallback.pageRendering({
pageNum:num,
pageRendering:that.pageRendering
})
function getPage(page){
var viewport = page.getViewport(that.scale);
that.canvas.height = viewport.height;
that.canvas.width = viewport.width;
var renderContext = {
canvasContext: that.ctx,
viewport: viewport
};
var renderTask = page.render(renderContext);
renderTask.promise.then(function() {
that.pageRendering = false;
that.rendercallback&&that.rendercallback.pageRenderingSuccess&&that.rendercallback.pageRenderingSuccess({
pageNum:num,
totalPage:that.totalPage,
pageRendering:that.pageRendering
})
if (that.pageNumPending !== null) {
that.renderPage(that.pageNumPending);
that.pageNumPending = null;
}
});
}
setTimeout(function(){ //如果不用这个,rendercallback.loadding无法及时执行
that.pdfDoc.getPage(num).then(function(page) {
getPage(page)
});
},100)
},
queueRenderPage:function(num){
var that=this;
if (that.pageRendering) {
that.pageNumPending = num;
} else {
that.renderPage(num);
}
},
onNextPage:function(){
var that=this;
if (that.pageNum >= that.pdfDoc.numPages) {
return;
}
that.pageNum++;
that.queueRenderPage(that.pageNum);
},
onPrevPage:function () {
var that=this
if (that.pageNum <= 1) {
return;
}
that.pageNum--;
that.queueRenderPage(that.pageNum);
},
getPdfName:function(url){
var p=/([a-zA-Z0-9]*)\.(pdf)/gi
var m=p.exec(url)
return m[2]+m[1]
},
localSet:function(obj,name){
var name=name||'PDF'
localStorage.setItem(name,JSON.stringify(obj))
},
localGet:function(name){
var name=name||'PDF'
return localStorage[name]&&(JSON.parse(localStorage.getItem(name)))
},
time:function(){
console.log('当前秒钟',(new Date()).getSeconds())
}
}