Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 235
Erica Stockwell-Alpert Avatar asked Dec 02 '25 05:12

Erica Stockwell-Alpert


1 Answers

jsPDF won't download files on mobile apps by this pdf.save(). I have tried and searched for this issue but could not find a complete solution in one place. I am using the file and file-opener plugin. I have developed the solution in Ionic React. Install below modules.

npm i jspdf
npm install cordova-plugin-file
npm install @ionic-native/file
npm install cordova-plugin-file-opener2
npm install @ionic-native/file-opener
ionic cap sync
  1. Go to your file and add these import statements.

import { jsPDF } from "jspdf";
import 'jspdf-autotable';
import { FileOpener } from '@ionic-native/file-opener;
import { File } from '@ionic-native/file';
import { isPlatform } from "@ionic/react";
  1. Check the pdfDownload function

const pdfDownload = async () => {
var doc = new jsPDF();
doc.setFontSize(40);
doc.text("Example jsPDF", 35, 25)
let pdfOutput = doc.output();
    if (isPlatform("android")) {
      // for Android device
      const directory = File.externalRootDirectory + 'Download/';
      const fileName = `invoice-${new Date().getTime()}.pdf`
      File.writeFile(directory, fileName, pdfOutput, true).then(succ => {
        FileOpener.showOpenWithDialog(directory + fileName, 'application/pdf')
          .then(() => console.log('File opened'))
          .catch(error => console.log('Error opening file', error));
      },
        err => {
          console.log(" writing File error : ", err)
        })
    } else if (isPlatform("ios")) {
      // for iOS device
      console.log('ios device')
      const directory = File.tempDirectory;
      const fileName = `invoice-${new Date().getTime()}.pdf`
      File.writeFile(directory, fileName, pdfOutput, true).then(success => {
        FileOpener.showOpenWithDialog(directory + fileName, 'application/pdf')
          .then(() => console.log('File opened'))
          .catch(e => console.log('Error opening file', e));
      },
        err => {
          console.log(" writing File error : ", err)
        })
    } else {
      // for desktop
      doc.save("invoice.pdf");
    }

}
like image 115
Ankit Kumawat Avatar answered Dec 03 '25 23:12

Ankit Kumawat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!