-
Notifications
You must be signed in to change notification settings - Fork 66
/
index.html
230 lines (224 loc) · 7.62 KB
/
index.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<!DOCTYPE html>
<html>
<script>
var _hmt = _hmt || [];
(function () {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?e495c059c321f94f833a194c3dc8c87c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-118964508-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-118964508-1');
</script>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="description" content="PDF Viewer" />
<title>Convert PDF To Image</title>
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/mian.css" />
</head>
<body>
<div class="frame">
<div class="center">
<div class="profile">
<a href="https://github.com/xxlllq/pdf2img" target="_blank">
<div class="image">
<div class="circle-1"></div>
<div class="circle-2"></div>
<img src="img/James Kobe.png" width="70" height="70" alt="">
</div>
</a>
<div class="name">PDF转图片</div>
<div class="job">
文档大小
< 10Mb </div> <div class="actions">
<a href="javascript:;" class="btn file">
Choose Pdf
<input id='pdf' type='file' accept="application/pdf">
</a>
<button class="btn" id="export">Export Image</button>
<a style="display:block;margin-left:40px;"><iframe src="https://ghbtns.com/github-btn.html?user=xxlllq&repo=pdf2img&type=star&count=true&size=large"
frameborder="0" scrolling="0" width="170px" height="30px"></iframe></a>
</div>
</div>
<div class="stats">
<div class="box">
<span class="value">名称</span>
<span class="parameter" id="pdfName"></span>
</div>
<div class="box">
<span class="value">大小</span>
<span class="parameter" id="sizeText"></span>
</div>
<div class="box">
<span class="value">页数</span>
<span class="parameter" id="pagesText"></span>
</div>
<div class="box allPage">
<span class="value all">导出整图</span>
<input type="checkbox" id="allInOnPicture" />
<span class="value all small">少于20张效果较好</span>
</div>
<div class="box pageChoose">
<span class="value all">页码选择</span>
<input type="number" id="pageChooseStart" placeholder="开始页码"/>
<input type="number" id="pageChooseEnd" placeholder="结束页码"/>
</div>
</div>
</div>
</div>
<div id="imgDiv">
</div>
<script type="text/javascript" src="js/jszip.min.js"></script>
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="js/FileSaver.js"></script>
<script type="text/javascript" src="js/pdf.js"></script>
<script type="text/javascript" src="js/pdf.worker.js"></script>
<script>
// PDFJS.disableStream = true;
$("#export").attr("disabled", "disabled"); //禁用导出图片按钮
var pdfFile;
$('#pdf').change(function() {
var pdfFileURL = $('#pdf').val();
if (pdfFileURL) {
$("#imgDiv").empty(); //清空上一PDF文件展示图
var files = $('#pdf').prop('files'); //获取到文件
var fileSize = files[0].size;
var mb;
if (fileSize) {
mb = fileSize / 1048576;
if (mb > 10) {
alert("文件大小不能>10M");
return;
}
}
$("#export").removeAttr("disabled", "disabled");
$("#pdfName").text(files[0].name).attr("title", files[0].name);
$("#sizeText").text(mb.toFixed(2) + "Mb");
/*pdf.js无法直接打开本地文件,所以利用FileReader转换*/
var reader = new FileReader();
reader.readAsArrayBuffer(files[0]);
reader.onload = function(e) {
var myData = new Uint8Array(e.target.result)
var docInitParams = {
data: myData
};
var typedarray = new Uint8Array(this.result);
PDFJS.cMapUrl = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.288/cmaps/';
PDFJS.cMapPacked = true;
PDFJS.getDocument(typedarray).then(function(pdf) { //PDF转换为canvas
$("#imgDiv").css("border", "0"); //清除文本、边框
if (pdf) {
window.pageNum = pdf.numPages;
$("#pagesText").text(pageNum);
for (var i = 1; i <= pageNum; i++) {
var canvas = document.createElement('canvas');
canvas.id = "pageNum" + i;
$("#imgDiv").append(canvas);
var context = canvas.getContext('2d');
openPage(pdf, i, context);
}
}
});
};
}
});
function openPage(pdfFile, pageNumber, context) {
var scale = 2;
pdfFile.getPage(pageNumber).then(function(page) {
viewport = page.getViewport(scale); // reference canvas via context
var canvas = context.canvas;
canvas.width = viewport.width;
canvas.height = viewport.height;
// 记录当前canvas的width,height
window.wi = canvas.width;
window.he = canvas.height;
canvas.style.width = "100%";
canvas.style.height = "100%";
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
return;
};
//dataURL转成Blob
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {
type: mime
});
}
//导出图片
$("#export").click(function() {
var zip = new JSZip();
if ($("#allInOnPicture").prop("checked")) { //导出为一张整图
var height = 0,
context, mainCanvas, b = 2; //b:整图缩放倍数
var count = $("canvas").length;
$("canvas").each(function(index, ele) {
var canvas = document.getElementById("pageNum" + (index+1));
if (canvas) {
if (index == 0) {
mainCanvas = document.createElement('canvas');
context = mainCanvas.getContext('2d');
mainCanvas.width = canvas.width / b;
mainCanvas.height = canvas.height / b * count;
context.drawImage(canvas, 0, 0, canvas.width / b, canvas.height / b);
} else {
context.drawImage(canvas, 0, height, canvas.width / b, canvas.height / b);
}
height += canvas.height / b;
}
})
if (mainCanvas) {
zip.file("image.png", dataURLtoBlob(mainCanvas.toDataURL("image/png", 1.0)), {
base64: true
});
// 或者自动下载为png
// var oA = document.createElement("a");
// oA.download = '';
// oA.href = mainCanvas.toDataURL();
// document.body.appendChild(oA);
// oA.click();
// oA.remove();
}
} else {
var images = zip.folder("images");
$("canvas").each(function(index, ele) {
var idx = index + 1;
var canvas = document.getElementById("pageNum" + idx);
var startIdx = $("#pageChooseStart").val(),endIdx=$("#pageChooseEnd").val();
if((startIdx && parseInt(startIdx) >idx) ||( endIdx && parseInt(endIdx)<idx)) {
return true;
}
images.file("image-" + idx + ".png", dataURLtoBlob(canvas.toDataURL("image/png", 1.0)), {
base64: true
});
})
}
zip.generateAsync({
type: "blob"
}).then(function(content) {
saveAs(content, "pdftoimages.zip");
});
});
</script>
</body>
</html>