js實現導出PDF
<button onclick="aaa()">導出PDF</button>
<div class="right-aside">
<table border="" cellspacing="" cellpadding="" id="ta">
<h3>111</h3>
<thead>
<tr>
<td>IU號碼</td>
<td>車牌號碼</td>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.js"></script>
<script src="https://cdn.bootcss.com/jspdf/1.3.4/jspdf.debug.js"></script>
<script>
function aaa() {
var target = document.getElementsByClassName("right-aside")[0];
target.style.background = "#FFFFFF";
html2canvas(target, {
onrendered: function (canvas) {
var contentWidth = canvas.width;
var contentHeight = canvas.height;
var pageHeight = contentWidth / 592.28 * 841.89;
var leftHeight = contentHeight;
var position = 0;
var imgWidth = 595.28;
var imgHeight = 592.28 / contentWidth * contentHeight;
var pageData = canvas.toDataURL('image/jpeg', 1.0);
var pdf = new jsPDF('', 'pt', 'a4');
if (leftHeight < pageHeight) {
pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);
} else {
while (leftHeight > 0) {
pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
leftHeight -= pageHeight;
position -= 841.89;
if (leftHeight > 0) {
pdf.addPage();
}
}
}
pdf.save("content.pdf");
}
})
}
</script>