Download using jsPDF on a mobile devices

12,124

Solution 1

Here is the example if you're using the Cordova platform for your development:

https://github.com/saharcasm/Cordova-jsPDF-Email

The workaround of the pdf not being downloaded in the devices is to use cordova-plugin-file.

Use the output method on the doc that will give you the raw pdf which needs to be written & saved in a file.

For example,

var doc = new JsPDF(); 
//... some work with the object
var pdfOutput = doc.output("blob"); //returns the raw object of the pdf file

The pdfOutput is then written on an actual file by using the file plugin.

Solution 2

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.

Solution 3

The easiest way which works on both Desktop and Mobile is to use:

window.open(doc.output("bloburl"), "_blank");

Share:
12,124
Erica Stockwell-Alpert
Author by

Erica Stockwell-Alpert

merge delete

Updated on June 04, 2022

Comments

  • Erica Stockwell-Alpert
    Erica Stockwell-Alpert almost 2 years

    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.

  • geoidesic
    geoidesic almost 3 years
    Gives error: [Vue warn]: Error in v-on handler (Promise/async): "TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed."
  • bloodyKnuckles
    bloodyKnuckles about 2 years
    Received Uncaught TypeError: Blob constructor: Argument 1 can't be converted to a sequence. Solved it with this: window.open(URL.createObjectURL(new Blob([blob])))