简体   繁体   English

jspdf-保存pdf

[英]jspdf - saving a pdf

I am new at Javascript. 我是Java语言的新手。 And i want to print using javascript using jspdf.js. 而且我想使用jspdf.js使用javascript打印。

I have this code. 我有这个代码。

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

But it is not working. 但这是行不通的。 Do I have to do anything? 我需要做什么吗? Thanks! 谢谢! Please help me.. Thanks in advance. 请帮助我..在此先感谢。

The downloadify plugin is meant to build the button for you to click on at page run time, so setting it up to run the downloadify script onclick is not going to work, instead add this and get rid of the button you made: downloadify插件旨在构建供您在页面运行时单击的按钮,因此将其设置为运行downloadify脚本onclick不会起作用,而是添加它并摆脱您所做的按钮:

<body onload="downloadPDF()">

My only other suggestion would be to use the jspdf.min.js found in the dist folder instead of just the standard jspdf.js file. 我唯一的其他建议是使用dist文件夹中的jspdf.min.js而不是仅使用标准jspdf.js文件。 The min folder contains all the plugins/functions needed to build the PDF. min文件夹包含构建PDF所需的所有插件/功能。 Keep including downloadify and swfobject though, they aren't included in that min build package. 尽管继续包括downloadify和swfobject,但它们不包含在该min build软件包中。

Note: I'm still trying to figure out how to get downloadify to add actual page content myself, the documentation is woefully poor. 注意:我仍在尝试自己如何获取downloadify来添加实际的页面内容,该文档非常糟糕。 When I put the code in to do so, literally nothing happens when I click the Save to Disk button, no console errors, no download happening, no nothing. 当我放入代码时,单击“保存到磁盘”按钮几乎没有任何反应,没有控制台错误,没有下载,也没有任何反应。

Also, you said you want to PRINT using jsPDF...but all jsPDF and downloadify do, is convert your page into a PDF file to download, not print. 另外,您说过要使用jsPDF进行打印...但是所有jsPDF和downloadify都可以将页面转换为PDF文件进行下载, 而不是打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM