简体   繁体   中英

jspdf - saving a pdf

I am new at Javascript. And i want to print using javascript using jspdf.js.

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:

<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. The min folder contains all the plugins/functions needed to build the PDF. Keep including downloadify and swfobject though, they aren't included in that min build package.

Note: I'm still trying to figure out how to get downloadify to add actual page content myself, the documentation is woefully poor. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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