使用jsPDF下载PDF文件

时间:2015-08-04 20:25:19

标签: jspdf

尝试使用https://parall.ax/products/jspdf

中的示例下载PDf文件

相关HTML代码如下所示。

它一直给出错误" Uncaught TypeError:无法读取属性' config'未定义的jspdf.debug.js 未捕获的TypeError:无法读取属性' canvas'未定义"并且不会生成任何PDF文件。

感谢您的帮助。

<script type="text/javascript" language="javascript" src="#{applicationConfiguration.appStaticFileContextPath}/desktop/js/jsPDF-master/dist/jspdf.debug.js"></script>

<script type="text/javascript" language="javascript" src="#{applicationConfiguration.appStaticFileContextPath}/desktop/js/jsPDF-master/plugins/from_html.js?"></script>
<script type="text/javascript" language="javascript" src="#{applicationConfiguration.appStaticFileContextPath}/desktop/js/jsPDF-master/plugins/canvas.js?"></script>

function downloadPDF() {
   var doc = new jsPDF('p', 'in', 'letter');
     var source = $('#overview_tab').first();
     var specialElementHandlers = {
         '#banking_tab': function(element, renderer) {
             return true;
         }
     };

     doc.fromHTML(
         source, // HTML string or DOM elem ref.
         0.5,    // x coord
         0.5,    // y coord
         {
             'width': 7.5, // max width of content on PDF
             'elementHandlers': specialElementHandlers
         });

     doc.output('dataurl');

}

1 个答案:

答案 0 :(得分:1)

此代码适用于我:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello world</title>
</head>

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


<script type="text/javascript">
function saveAspdf() {
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.body,function() {
    pdf.save('web.pdf');
});
}
</script>


<body>
    <p id="to-pdf">Generating PDF</p>
</body>

</html>



<input type="button" value="save" onclick="javascript:saveAspdf()"/>