先看效果如下,表格数据动态从后台返回:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple example</title>
</head>
<body>
<button onclick="generate()">导出 pdf</button>
<!-- pdf展示 -->
<div style="height: 600px;">
<iframe id="output" src="" frameborder="0" width="100%" height="90%"></iframe>
</div>
<script src="libs/jspdf.min.js"></script>
<script src="../dist/jspdf.plugin.autotable.src.js"></script>
<script>
var columns = ["ID", "Name", "Age", "City"]; // 表头
var data = [ // tableDate
[1, "Jonatan", 25, "Gothenburg"],
[2, "Simon", 23, "Gothenburg"],
[3, "Hanna", 21, "Stockholm"]
];
var doc = new jsPDF('p', 'pt'); // 获取实例
doc.autoTable(columns, data); // 渲染table
document.getElementById("output").src = doc.output('datauristring'); // 渲染pdf
function generate() { // 导出pdf
doc.save("table.pdf");
}
</script>
</body>
</html>