3

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
  • Solved it for you see my answer.. Commented Jun 14, 2015 at 7:03

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I appreciate your efforts but I need to use addHtml method only because I will be writing different content using other methods to the same pdf.
So you can make plain HTML by concatenating html string and finally use one fromhtml to print it..
I have updated the answer now using 'addHTML' function. @vinesh
Thanks. I'll check and let you know
1

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

Comments

1

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.

1 Comment

Note that this worked for me only if both scripts are included in exact this order shown here. It doesn't work if you include html2canvas first. The reason is, that jsPDF overwrites some html2canvas methods, what I consider bad practice. So it only seems to work if html2canvas is included afterwards to re-define its methods properly. The same issue is discussed here: stackoverflow.com/questions/28807755/…
0

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

1 Comment

For some reason anything inside the function() ins't executing. The canvas is being rendered but the pdf.save() isn't being called.
0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.