jspdf功能不起作用

时间:2015-02-02 08:10:28

标签: php jspdf

我现在必须将HTML页面下载到PDF中。我知道JSPDF是最好的解决方案,我已经实现了以下代码,但它不起作用......

<head>
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

    <title>Invoice</title>
    <link rel="stylesheet" type="text/css" href="/jspdf/examples/css/smoothness/jquery-ui-1.8.17.custom.css">
    <link rel="stylesheet" type="text/css" href="/jspdf/examples/css/smoothness/css/main.css">

    <script type="text/javascript" src="/jspdf/examples/js/jquery/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="/jspdf/examples/js/jquery/jquery-ui-1.8.17.custom.min.js"></script>
    <script type="text/javascript" src="jspdf/dist/jspdf.debug.js"></script>
    <script type="text/javascript" src="/jspdf/examples/js/basic.js"></script>
  <script type="text/javascript">
var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});
  </script>    

</script>
</head>
<body id="target">
<div id="content">
     <h3>H3 tag</h3>

    <p>a Para</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

</body>
</html>

请帮我解决这个问题...

1 个答案:

答案 0 :(得分:-1)

以下是如何实现jspdf.js库以便从当前网站创建PDF页面的示例。

所需的JS库可以下载here

如果您发现任何错误,请与我联系或写下您的评论。

<script  type="text/javascript" src='jspdf.min.js'></script>

 <script  type="text/javascript" src='html2canvas.js'></script>


<script type="text/javascript">


function genPDF()

{


 html2canvas(document.body, {


  onrendered : function(canvas)

    { 

     try

       {     

        var canvas1 = document.createElement('canvas');

        canvas1.width = canvas.width;    canvas1.height = canvas.height;

        var context1 = canvas1.getContext('2d');     

        context1.drawImage(canvas, 0,0);

        options = {orientation: "0", unit: "mm",format: "a4" };     

        var doc = new jsPDF(options,'p', 'pt', 'a4');  

        var number_needed_pages = canvas.height / 900;

        if(number_needed_pages < 1) number_needed_pages = 1; 

        for(var x=0; x < number_needed_pages ; x++) 

        {  

         var canvas2 = document.createElement('canvas');  

         canvas2.width = 794; canvas2.height = 900;  

         var context2 = canvas2.getContext('2d');    

         context2.drawImage(canvas1, 0 , 0 + 900*x , canvas1.width, canvas2.height, 50, 0,  canvas.width, canvas2.height); 

        doc.addImage(canvas2.toDataURL('image/png'), 'JPEG', '', '', '');  

        doc.addPage();

        }  



    doc.save("output_file.pdf");  

      } catch(e){alert(e);} 



    }




                            });



}