最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - download a pdf with filesaver.js and blob - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Feb 19, 2016 at 0:10 Przemek 3,9752 gold badges27 silver badges34 bronze badges asked Nov 3, 2015 at 8:24 madca0313madca0313 811 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4
 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"}

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.

PDF is pletely another format and FileSaver's only mission is saving files.

You can use jsPDF library to do that. This is how you could use it in your case:

var filename = "myfile.csv";
var s = "my csv text content";

var pdf = new jsPDF();
pdf.text(10, 10, s);
pdf.save(filename.replace(".csv", ".pdf"));
发布评论

评论列表(0)

  1. 暂无评论