harrison
harrison

Reputation: 27

Fail to save pdf file using addHTML in jspdf

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!

Upvotes: 1

Views: 4565

Answers (2)

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.

Upvotes: 0

Ivan Gerasimenko
Ivan Gerasimenko

Reputation: 2428

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

Upvotes: 2

Related Questions