1

我是 Javascript 的新手。我想使用 jspdf.js 使用 javascript 进行打印。

我有这个代码。

    <!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>

但它不起作用。我必须做些什么吗?谢谢!请帮助我..提前谢谢。

4

1 回答 1

0

downloadify 插件旨在为您构建在页面运行时单击的按钮,因此将其设置为运行 downloadify 脚本 onclick 是行不通的,而是添加它并删除您制作的按钮:

<body onload="downloadPDF()">

我唯一的其他建议是使用 dist 文件夹中的 jspdf.min.js 而不仅仅是标准的 jspdf.js 文件。min 文件夹包含构建 PDF 所需的所有插件/功能。尽管继续包括 downloadify 和 swfobject,但它们不包含在该最小构建包中。

注意:我仍在试图弄清楚如何让 downloadify 自己添加实际的页面内容,文档非常糟糕。当我将代码放入这样做时,当我单击“保存到磁盘”按钮时,实际上什么都没有发生,没有控制台错误,没有下载发生,什么也没有。

另外,您说您想使用 jsPDF 打印...但是所有 jsPDF 和 downloadify 所做的是将您的页面转换为 PDF 文件以供下载,而不是打印。

于 2014-12-10T19:21:27.820 回答