Last Updated: February 25, 2016
· cauchyriemann

Making work cordova/phonegap + jsPDF

Spend some time to integrate those things together, in other words to generate pdf in javascript and save it to filesystem.

Actually there is nothing hard, but I will describe how you can make it work. Hope this will save somebody's time.

At first you need to add to cordova FileSystem plugin. It's can be easily done with command line:

cordova plugin add org.apache.cordova.file

Also you need to patch jsPDF (I'm talking about version 0.9.0), because it doesn't work on android with default blob creation.
You need to replace commented line with following code (thanks to stackoverflow answer

//                    blob = new Blob([array], {type: "application/pdf"});

    blob = new Blob([array], {type: "application/pdf"});
    console.debug("case 1");
 catch (e)
     window.BlobBuilder = window.BlobBuilder ||
                                          window.WebKitBlobBuilder ||
                                          window.MozBlobBuilder ||
     if ( == 'TypeError' && window.BlobBuilder)
         var bb = new BlobBuilder();
         blob = bb.getBlob("application/pdf");
         console.debug("case 2");
      else if ( == "InvalidStateError")
          // InvalidStateError (tested on FF13 WinXP)
          blob = new Blob([array], {type: "application/pdf"});
          console.debug("case 3");
           // We're screwed, blob constructor unsupported entirely

And the last step will be actually generate and save created file!

// Somewhere in your code
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
    fileSystem.root.getFile("test.pdf", {create: true, exclusive: false}, gotFileEntry, fail);

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);

function gotFileWriter(writer) {
    var doc = new jsPDF();

    doc.text(20, 20, 'Hello world!');

UPDATE 21.03.2014
Last version was producing corrupted PDF, below is updated code which works properly with images using jsPDF.

var sourcer = $('#imageForDocument');

function writeReport(doc) {
    var data = doc.output();
    var buffer = new ArrayBuffer(data.length);
    var array = new Uint8Array(buffer);
    for (var i = 0; i < data.length; i++) {
        array[i] = data.charCodeAt(i);

if ($("#doclogo").length > 0) {

    window.canvasplugin(sourcer.attr('src'), function (val) {
        var doc = makePdfReport(val);

12 Responses
Add your response


Have you tried it on android phonegap ? does it create PDF file ? because I want to know how does writting of PDF header takes place (buildDocument()) in your solution?

Vinod Maralad

over 1 year ago ·

Hi. I'm using cordova, I'm pretty sure it will be the same with phonegap. PDF file looks valid, and opened with mac os preview and adobe reader. Seems that jspdf generates header by himself. Also the problem can be that you have to be sure that there are no errors in JS which generates PDF.

over 1 year ago ·

Be careful - there is problem when inserting images in PDF. it won't work, I have a workaround. Will post it later

over 1 year ago ·

Have you posted the work around? I am unable to find it and trying to generate a PDF with an image. Any help would be appreciated.

over 1 year ago ·

Hi Jon! I've updated article, please check it!

over 1 year ago ·

Thanks for the response back. I am using the jsPDF version from MrRio, located here: and noticed that the update you posted was already in the code however it did point me to the issue. I noted the issue and my fix here:
Thanks again for the help.

over 1 year ago ·

Yeah. It was done 4 months ago. so most likely the library was updated already. But for me this solution worked fine. Glad to help!

over 1 year ago ·

Hello can u give example full for jspdf ...

over 1 year ago ·

I'm sorry if is a stupid question (I'm new to javascript), but what does #doclogo is used for and what kind of element is it? sorry, just cannot understand.

over 1 year ago ·

doclogo - is a jquery selector for your image. you can use any other selector you want.

over 1 year ago ·

Hi ,
need help in writing arraybuffer object to pdf/image file .
able to add text to pdf but not arraybuffer data .

over 1 year ago ·

I am trying to create the pdf my current view onclick of button but it is not giving error nor alert ..can anyone help

I have included the following plugins using CLI
file transfer

the current version m using is cordova 5.4.1

over 1 year ago ·