Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open (not save) a blob generated with jspdf with Cordova for Android?

I´m having issues trying to open a PDF file blob generated with jspdf using cordova. I found that there is a lot of 'security' measures on Android that make incredibly difficult to open from a saved file, more for me because I´m noob with cross platforms apps, so I need to find a way to open the blob without saving.

I tried but the console shows me: "Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided."

This is my function to generate the pdf blob:

   function createPDF(){
      console.log("generating pdf...");
      var doc = new jsPDF();

      doc.text(20, 20, 'Document title');

      doc.setFont("courier");
      doc.setFontType("normal");
      doc.text(20, 30, 'test first line');
      doc.text(20, 50, 'test second line');

      var blobPDF = doc.output();

      var blobUrl = URL.createObjectURL(blobPDF);  <--- THE ERROR APPEARS HERE
      window.open(blobUrl,'_system','location=yes');
    }

What I need is open the file so Android gives me the optional apps to open (Adobe reader, etc).

How can I make this code work for Android? I tried a lot of examples but always there's some kind of problem.

Just in case I'm using Intel XDK (ver 3522) and I included the File, File-Transfer, InAppBrowser, FileOpener2 plugins.

like image 275
Alan Alvarez Avatar asked Sep 17 '16 03:09

Alan Alvarez


1 Answers

Try this, works for me :

var blobPDF =  new Blob([ doc.output() ], { type : 'application/pdf'});
var blobUrl = URL.createObjectURL(blobPDF);  //<--- THE ERROR APPEARS HERE

window.open(blobUrl);  // will open a new tab

//window.open(blobUrl,'_system','location=yes'); will open a new window
like image 168
Douae Mahroug Avatar answered Nov 15 '22 05:11

Douae Mahroug