2013-11-05 223 views
0

我試圖在用戶單擊按鈕時將網頁導出爲pdf文件,但下面的代碼不適用於我。任何人都可以幫助我,我哪裏出錯了。我正在使用jsPDF將網頁導出爲pdf。使用jsPDf將網頁導出爲PDF

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>html2canvas example</title> 
    <script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script> 
    <script type="text/javascript" src="js/jspdf.js"></script> 
    <script type="text/javascript" src="libs/Deflate/adler32cs.js"></script> 
    <script type="text/javascript" src="libs/FileSaver.js/FileSaver.js"></script> 
    <script type="text/javascript" src="libs/Blob.js/BlobBuilder.js"></script> 
    <script type="text/javascript" src="jspdf.plugin.addimage.js"></script> 
    <script type="text/javascript" src="jspdf.plugin.standard_fonts_metrics.js"></script> 
    <script type="text/javascript" src="jspdf.plugin.split_text_to_size.js"></script> 
    <script type="text/javascript" src="jspdf.plugin.from_html.js"></script> 
    <script type="text/javascript" src="js/basic.js"></script> 
<script type="text/javascript"> 
$(function() 
{ 
     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> 
</head> 
<body id="target" > 
    <div id="content"> 
     <h3>Hello, this is a H3 tag</h3> 
     <p>a pararaph</p> 
    </div> 
    <div id="editor"></div> 
    <button id="cmd">generate PDF</button> 
</body> 
</html> 
+0

我可以看到這個代碼來自文檔,你可以你看到在JavaScript控制檯 –

+0

可能重複錯誤添加[導出HTML頁面的PDF用戶點擊使用javascript]( http://stackoverflow.com/questions/19786113/export-html-page-to-pdf-on-user-click-using-javascript) –

回答

3

腳本中存在錯誤。請試試這個代碼

<script> 
$(document).ready(function(){ 
          $('#cmd').click(function() { 
          var specialElementHandlers = 
          function (element,renderer) { 
          return true; 
          } 
          var doc = new jsPDF(); 
          doc.fromHTML($('#content').html(), 15, 15, { 
          'width': 170, 
          'elementHandlers': specialElementHandlers 
          }); 
          doc.output('datauri'); 
         }); 
         }); 
</script>