1

Heading:

<head> <script src="jquery.min.js"></script> <script src="jspdf.min.js"></script> <script src="html2canvas.min.js"></script> <script src="jspdf.plugin.addhtml.js"></script> </head> 

HTML:

<div id="wrapper"> <div class="red b"></div> <div class="yellow b"></div> <div class="blue b"></div> <div class="green b"></div> </div> 

JavaScript:

$(".button").click(function() { var pdf = new jsPDF('p', 'pt', 'a4'); pdf.addHTML($("#wrapper"), function () { pdf.save(); }); }); 

I want to add the div #wrapper into the pdf file when I press the button to generate it. But then when I press the button and inspect element in Chrome, it always show an unloaded events in sources.

Any help would be appreciated. Thanks!

2 Answers 2

2

There are a few things to do:

  1. Use the first element of array returned by jQuery selector $('#wrapper')[0]
  2. Define the name of the pdf file you want to save pdf.save('Test.pdf');
$('#downloadPdf').on('click', function() { var pdf = new jsPDF('p', 'pt', 'a4'); pdf.addHTML($('#wrapper')[0], function() { pdf.save('Test.pdf'); }); }); 

You can check Demo Fiddle here

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

2 Comments

Thanks for the demo. And I further figure out one of my problem is that the source of my "jspdf.min.js" is not updated and when I use the source in your demo, it could run properly.
Demo isn't working. I can't find jspdf.plugin.addhtml.js file. Can you give me cdn link?
0

I'm trying to use just that in my code but clicking on the button that would generate the PDF does not occur at all. I wonder if it would be possible to provide the source code to power try to manipulate and implement.Thank U.

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.