Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download a pdf with filesaver.js and blob

I'm trying to download a file with Filesaver.js. And when I try to do this with csv, it works fine. But I need to download a pdf now.

How can I do that? I use Filesaver.js with a blob object, my code looks something like this:

var filename = "myfile.csv";
var s = "my csv text content";
var blob = new Blob([s], {type "text/csv;charset=utf-8"});
var filesaver = saveAs(blob,filename);

I'd like to know what kind of data type should I pass to the blob onject, when I'd like to download the pdf.

like image 442
madca0313 Avatar asked Nov 03 '15 08:11

madca0313


2 Answers

 var blob = new Blob([response.data], {type: "application/pdf;charset=utf-8"});
 var blob = new Blob([response.data], {type: "text/plain;charset=utf-8"});

Both are ok,but header should be

{responseType:"arraybuffer"}
like image 156
Ado Avatar answered Oct 23 '22 19:10

Ado


Data type should be application/pdf of course.

The content should be on base64 so the blob should start with

data:application/pdf;base64,

As you can see in the jspdf library source code.

Enjoy the demo.

like image 34
Ruslan López Avatar answered Oct 23 '22 18:10

Ruslan López