jspdf的简单使用

jspdf 简单使用

jspdf 是一款功能强大插件,作用是利用javascript来生成pdf文档,目前遗憾的是不支持utf-8编码导致中文会乱码。

使用addHTML()方法可以解决这一问题,不过官方马上将弃用次方法 代码如下:

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
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>HTML Test File</title>
</head>
<body>
<div style='position: absolute; left: 0; top: 0; bottom: 0; overflow: auto; width: 400px; background: #fff'>
<h1>Tdlkjg拉克丝的结果分厘卡撒酒疯了</h1>
<h1>Headings</h1>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>
<hr>
</div>
<script src='../js/jquery-2.1.4.min.js'></script>
<script src='../js/html2canvas.js'></script>
<script src='../js/jspdf.debug.js'></script>
<script>
var pdf = new jsPDF('p', 'pt', 'a4');
pdf.canvas.height = 72 * 11;
pdf.canvas.width = 72 * 8.5;
pdf.addHTML($('body'), function () {
var iframe = document.createElement('iframe');
iframe.setAttribute('style', 'position:absolute;right:0; top:0; bottom:0; height:100%; width:500px');
document.body.appendChild(iframe);
iframe.src = pdf.output('datauristring');
// // pdf.output('save','test.pdf');
// pdf.save();
});
</script>
</body>
</html>

doc文档截图
新方法 不过要加载==html2pdf.js==,而且并不支持中文。