简体   繁体   English

使用jsPDF将HTML内容转换为PDF文件

[英]Converting HTML content to PDF file using jsPDF

Trying to automatically convert this content to a pdf file. 尝试自动将此内容转换为pdf文件。 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. 根据其余代码的设置方式,我必须具有单击要导出到pdf的div内的按钮。 Is this a problem? 这有问题吗? Here's what I have so far. 到目前为止,这就是我所拥有的。 When I click the button, it does nothing. 当我单击按钮时,它什么也没做。

JS: JS:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/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: HTML:

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

No, this is not a problem. 不,这不是问题。 You can have the button inside the div element. 您可以将按钮放在div元素内。 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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/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(); 您需要初始化jsPDF类: 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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM