2013-06-18 126 views
1

我是Javascript新用戶。 而我想用jspdf.js使用javascript進行打印。jspdf - 保存pdf

我有這段代碼。

<!doctype> 
<html> 
<head> 
    <title>Generate PDF</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <script type="text/javascript" src="js/libs/swfobject.js"></script> 
    <script type="text/javascript" src="js/libs/downloadify.min.js"></script> 
    <script type="text/javascript" src="js/jspdf/jspdf.js"></script> 
    <script type="text/javascript"> 


    function downloadPdf(){ 

     Downloadify.create('downloadify',{ 
      filename: 'Simple.pdf', 
      data: function() 
      { 
        var doc = new jsPDF(); 
        doc.setFontSize(22); 
        doc.text(20, 20, 'My First PDF'); 
        doc.addPage(); 
        doc.setFontSize(16); 
        doc.text(20, 30, 'This is some normal sized text underneath.'); 
       return doc.output(); 
      }, 
      onComplete: function(){ alert('Your File Has Been Saved!'); }, 
      onCancel: function(){ alert('You have cancelled the saving of this file.'); }, 
      onError: function(){ alert('You must put something in the File Contents or there will be nothing to save!'); }, 
      downloadImage: 'images/download.png', 
      swf: 'images/downloadify.swf', 
      width: 100, 
      height: 30, 
      transparent: true, 
      append: false 
     }); 
    } 
</script> 
<body> 
To generate PDF Click Here. 
<input type="button" value="Generate" onclick="downloadPdf()" /> 
<br/> 
<div id="downloadify"></div> 

但它不工作。我需要做什麼嗎?謝謝! 請幫助我..在此先感謝。

+0

是對[樣品jspdf(http://parall.ax/products/jspdf)在瀏覽器的工作? – Anusha

回答

0

downloadify插件的目的是爲了在頁面運行時建立一個按鈕,因此設置它運行downloadify腳本onclick不會起作用,而是添加它並擺脫您製作的按鈕:

<body onload="downloadPDF()"> 

我唯一的另一項建議是使用在dist文件夾中,而不是僅僅的標準jspdf.js文件中找到的jspdf.min.js。 min文件夾包含構建PDF所需的所有插件/函數。保持包括downloadify和swfobject雖然,他們不包括在最小生成包。

注:我仍然在試圖找出如何讓downloadify增加實際的頁面內容自己,文檔是可悲可憐。當我把代碼放到這裏時,從字面上看,沒有任何事情發生,當我點擊保存到磁盤按鈕,沒有控制檯錯誤,沒有下載發生,沒有任何東西。

而且,你說你想用jsPDF列印...但所有jsPDF和downloadify要做的,就是將您的頁面轉換成PDF文件下載,打印。