6

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alan Alvarez
  • 646
  • 2
  • 11
  • 32

2 Answers2

8

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
Bruno Pinto
  • 2,013
  • 3
  • 23
  • 33
Douae Mahroug
  • 81
  • 1
  • 2
  • 1
    Worked fine for me. Just needed to add 'blob' in new Blob([ doc.output() ], { type : 'application/pdf'}); So that would be ==> new Blob([ doc.output('blob') ], { type : 'application/pdf'}); – kunj choksi Jan 22 '21 at 11:56
  • check this for reference https://github.com/MrRio/jsPDF/issues/942. – kunj choksi Jan 22 '21 at 11:58
1

jsPDF's output has an interface that accepts a bloburl as the parameter.

output(type: "bloburi" | "bloburl"): URL

Using your example:

function createPDF(){
  console.log("generating pdf...");
  const 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');

  const blobPDF = doc.output('bloburl');
  window.open(blobUrl,'_system','location=yes');
}
Kyle
  • 376
  • 3
  • 5