2013-06-18 121 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文件下载,打印。