jsPDF:addHTML方法不起作用

时间:2014-11-20 18:55:44

标签: 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文件。我做错了吗?

这是plunker

使用addHTML函数运行jsPDF的demo

5 个答案:

答案 0 :(得分:2)

为此工作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');
});
  

您可以查看plunker了解详情。

答案 1 :(得分:1)

在app.js中更改此内容:

  pdf.addHTML(document.getElementById('pdfTable'),function() {
      });
  pdf.save('pdfTable.pdf');

这个:

pdf.addHTML(document.getElementById('pdfTable'),function() {
    pdf.save('pdfTable.pdf');
});

答案 2 :(得分:1)

除了这些之外,我拿出了所有东西:

<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");
    });
    });
});

我可以打印表格......使用chrome,safari或firefox看起来很棒,但是,如果我的桌子非常大,我只能获得第一页。

答案 3 :(得分:0)

在app.js中更改:

  pdf.addHTML(document.getElementById('pdfTable'),function() {

  });
  pdf.save('pdfTable.pdf');

  pdf.addHTML(document.getElementById('pdfTable'),function() {
      pdf.save('pdfTable.pdf');
  });

如果您看到黑色背景,可以添加到sytle.css&#34;背景颜色:白色;&#34;

答案 4 :(得分:0)

您应该使用querySelector()

,而不是使用getElementById()
var pdf = new jsPDF('p','pt','a4');  
pdf.addHTML(document.querySelector('#pdfTable'), function () {
  console.log('pdf generated');
});

pdf.save('pdfTable.pdf');
相关问题