Javascript jsPDF:addHTML方法不工作
我创建了html表,并尝试使用jsPDF生成pdf 我使用了以下代码:Javascript jsPDF:addHTML方法不工作,javascript,jspdf,Javascript,Jspdf,我创建了html表,并尝试使用jsPDF生成pdf 我使用了以下代码: var pdf = new jsPDF('p','pt','a4'); pdf.addHTML(document.getElementById('pdfTable'),function() { console.log('pdf generated'); }); pdf.save('pdfTable.pdf'); 我得到一个空白的pdf文件。我做错什么了吗 这里有一个例子 使用addHTML函数处理jsPDF。在ap
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.getElementById('pdfTable'),function() {
console.log('pdf generated');
});
pdf.save('pdfTable.pdf');
我得到一个空白的pdf文件。我做错什么了吗
这里有一个例子
使用addHTML函数处理jsPDF。在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>
我可以打印表格。。。使用chrome、safari或firefox看起来很棒,但是,如果我的表非常大,我只会看到第一页。为此:
更新:
我已经更新了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');
到
如果看到黑色背景,可以添加到sytle.css“background color:white;”中,而不是使用getElementById(),您应该使用querySelector() 我已经在你的电脑上测试过了 只需更新你的app.js
pdf.addHTML(document.getElementById('pdfTable'), function() {
pdf.save('pdfTable.pdf');
});
更新您的css文件:
#pdfTable{
background-color:#fff;
}
我很感激你的努力,但我需要使用addHtml方法,因为我将使用其他方法将不同的内容写入同一个pdf。因此,你可以通过连接HTML字符串生成纯HTML,最后使用一个fromhtml打印。我现在使用“addHtml”函数更新了答案@谢谢。我会检查并让您知道,注意,只有当这两个脚本都包含在这里显示的确切顺序中时,这才对我有效。如果您首先包含html2canvas,则它不起作用。原因是JSPDF覆盖了一些HTML2CANVAS方法,我认为这是坏实践。因此,只有在随后包含html2canvas以正确地重新定义其方法时,它才似乎起作用。这里也讨论了同样的问题:由于某种原因,
函数()中的任何内容都无法执行。正在渲染画布,但未调用pdf.save()
。
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.querySelector('#pdfTable'), function () {
console.log('pdf generated');
});
pdf.save('pdfTable.pdf');
pdf.addHTML(document.getElementById('pdfTable'), function() {
pdf.save('pdfTable.pdf');
});
#pdfTable{
background-color:#fff;
}