为此我制作了一个在 plunker 上工作的示例:
更新:
我已经更新了 plunker 并使用 addHTML()
函数使其可用:
var pdf = new jsPDF('p','pt','a4');
//var source = document.getElementById('table-container').innerHTML;
console.log(document.getElementById('table-container'));
var margins = {
top: 25,
bottom: 60,
left: 20,
width: 522
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.text(20, 20, 'Hello world.');
pdf.addHTML(document.body, margins.top, margins.left, {}, function() {
pdf.save('test.pdf');
});
在 app.js 中更改此内容:
pdf.addHTML(document.getElementById('pdfTable'),function() {
});
pdf.save('pdfTable.pdf');
pdf.addHTML(document.getElementById('pdfTable'),function() {
pdf.save('pdfTable.pdf');
});
<script src="//mrrio.github.io/jsPDF/dist/jspdf.debug.js"></script>
<script src="//html2canvas.hertzen.com/build/html2canvas.js"></script>
$(document).ready(function() {
$("#pdfDiv").click(function() {
var pdf = new jsPDF('p','pt','letter');
var specialElementHandlers = {
'#rentalListCan': function (element, renderer) {
return true;
}
};
pdf.addHTML($('#rentalListCan').first(), function() {
pdf.save("rentals.pdf");
});
});
});
我已经在你的plunker中测试过了。
只需更新你的app.js文件即可。
pdf.addHTML(document.getElementById('pdfTable'), function() {
pdf.save('pdfTable.pdf');
});
更新你的 CSS 文件:
#pdfTable{
background-color:#fff;
}
在 app.js 中更改:
pdf.addHTML(document.getElementById('pdfTable'),function() {
});
pdf.save('pdfTable.pdf');
到
pdf.addHTML(document.getElementById('pdfTable'),function() {
pdf.save('pdfTable.pdf');
});
function()
内的任何内容都没有被执行。画布正在被渲染,但是pdf.save()
没有被调用。 - DevEng你应该使用querySelector()而不是getElementById()
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.querySelector('#pdfTable'), function () {
console.log('pdf generated');
});
pdf.save('pdfTable.pdf');