Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

pdf - Download using jsPDF on a mobile devices

I have a page that includes a download button using jsPDF. On desktop machines it downloads the page as it should. However, pdf.save() does not work on my tablet or phone.

I tried to add a special case for mobile devices to open the PDF in a new window, since mobile devices don't download things the same as desktops, with the idea being that once the PDF is open in a new window the user can choose to save it manually.

var pdf = new jsPDF('p', 'pt', 'letter');
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

html2canvas($("#pdf-area"), {
    onrendered: function (canvas) {
        $("#pdf-canvas").append(canvas);
        $("#pdf-canvas canvas").css("padding", "20px");
    }
});

var options = {
    pagesplit: true
};

function download(doctitle) {
    pdf.addHTML($("#pdf-area")[0], options, function () {
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
            pdf.output('dataurlnewwindow');           
        } else {
            pdf.save(doctitle);
        }        
    });
}

But the download function still does nothing on my tablet/phone. I tested it with this to make sure the pdf.output() function was working:

    pdf.addHTML($("#pdf-area")[0], options, function () {
        pdf.output('dataurlnewwindow');                
    });

and it does still work on desktop, but does nothing on mobile.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had similar issue.

jsPDF won't download file on phones/ tablets / ipads using "pdf.save()".

Do it through File plugin if you are using cordova/phonegap, this will save pdf file in downloads folder (Android) - for the ios you can access pdf file through a path (which is saved somewhere in temp directory) and can send or share.

Hope this helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...