javascript - Converting HTML content to PDF file using jsPDF - Stack Overflow

Trying to automatically convert thiscontent to a pdf file. Based on how the rest of my code is setup,

Trying to automatically convert this content to a pdf file. Based on how the rest of my code is setup, I have to have the button to click INSIDE the div that I want to export to pdf. Is this a problem? Here's what I have so far. When I click the button, it does nothing.

JS:

<script type="text/javascript" src=".3.4/jspdf.debug.js"></script>

<script>
jQuery('#generatereport').click(function () {
    doc.fromHTML(jQuery('#lppresults').html(), 15, 15, {
        'width': 170
    });
    doc.save('sample-file.pdf');
});
</script>

HTML:

<div class="survey-results" style="display: none;" id="lppresults">
   <button id="generatereport">Download Report</button>
   TEST CONTENT
</div>

Trying to automatically convert this content to a pdf file. Based on how the rest of my code is setup, I have to have the button to click INSIDE the div that I want to export to pdf. Is this a problem? Here's what I have so far. When I click the button, it does nothing.

JS:

<script type="text/javascript" src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script>

<script>
jQuery('#generatereport').click(function () {
    doc.fromHTML(jQuery('#lppresults').html(), 15, 15, {
        'width': 170
    });
    doc.save('sample-file.pdf');
});
</script>

HTML:

<div class="survey-results" style="display: none;" id="lppresults">
   <button id="generatereport">Download Report</button>
   TEST CONTENT
</div>
Share Improve this question asked Jul 6, 2017 at 21:07 mickdeezmickdeez 5113 gold badges8 silver badges20 bronze badges 3
  • I guess you have JS into .html file? – VictorArcas Commented Jul 6, 2017 at 21:11
  • @VictorArcas yes, that is correct – mickdeez Commented Jul 7, 2017 at 18:41
  • When I tried there is an error saying .fromHTML is not a function. What could be wrong? – Liaqat A. Commented Aug 23, 2022 at 10:42
Add a ment  | 

2 Answers 2

Reset to default 2

No, this is not a problem. You can have the button inside the div element. Though you have some other issues. Here is the revised version of your code ...

var doc = new jsPDF();
$('#generatereport').click(function() {
   doc.fromHTML($('#lppresults')[0], 15, 15, {
      width: 170
   }, function() {
      doc.save('sample-file.pdf');
   });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>

<div class="survey-results" id="lppresults">
  <button id="generatereport">Download Report</button> TEST CONTENT
</div>

You need to initialize the jsPDF class : var doc = new jsPDF();

var doc = new jsPDF();
jQuery('#generatereport').click(function() {
  doc.fromHTML(jQuery('#lppresults').html(), 15, 15, {
    'width': 170
  });
  doc.save('sample-file.pdf');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>


<div class="survey-results" id="lppresults">
  <button id="generatereport">Download Report</button> TEST CONTENT
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744214771a4563500.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信