jsPDF: addHTML method not working

这一生的挚爱 提交于 2019-12-07 18:43:43

问题


I created html table and tried to generate pdf using jsPDF.

I have used following code:

var pdf = new jsPDF('p','pt','a4');  
pdf.addHTML(document.getElementById('pdfTable'),function() {
  console.log('pdf generated');
});
pdf.save('pdfTable.pdf');

I am getting blank pdf file. Am I doing anything wrong?

Here is a plunker for this.

Working demo of jsPDF with addHTML function.


回答1:


Made working plunker for this: Update: I have updated the plunker and made it work with addHTML() function:

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

You can see plunker for more details.




回答2:


Change this in app.js:

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

for this one:

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



回答3:


I took out everything but these:

<script src="//mrrio.github.io/jsPDF/dist/jspdf.debug.js"></script>
<script src="//html2canvas.hertzen.com/build/html2canvas.js"></script>

then I used this:

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

I can print the table... looks great with chrome, safari or firefox, however, I only get the first page if my table is very large.




回答4:


In app.js change:

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

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

to

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

If you see a black background, you can add to sytle.css "background-color: white;"




回答5:


Instead of using the getElementById() you should use querySelector()

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

pdf.save('pdfTable.pdf');



回答6:


I have test this in your plunker.

Just update your app.js

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

update your css file:

#pdfTable{
  background-color:#fff;
}


来源:https://stackoverflow.com/questions/27047365/jspdf-addhtml-method-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!