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

javascript - Converting from blob to pdf - Stack Overflow

programmeradmin4浏览0评论

I'am able to convert my pdf files into blob. But what i want to do is the opposite by converting it from blob to pdf.

Here is my code converting it from pdf to blob. I just need some advice where i can return from blob to pdf.

var xhr=new XMLHttpRequest();

xhr.open("GET","./template.pdf");
xhr.responseType="arraybuffer";

xhr.onload = function (e){
    var blob = new Blob([xhr.response]);
    var url = URL.createObjectURL(blob)

    console.log(url);

    var embed=document.getElementById("template");
    embed.src = url;
}

xhr.send();

The result will return a blob from console=> blob:http://localhost:8081/d42939da-e318-4d88-b46f-8240efaa7b1c and once i paste this on the url, it will show me a huge blob text

I'am able to convert my pdf files into blob. But what i want to do is the opposite by converting it from blob to pdf.

Here is my code converting it from pdf to blob. I just need some advice where i can return from blob to pdf.

var xhr=new XMLHttpRequest();

xhr.open("GET","./template.pdf");
xhr.responseType="arraybuffer";

xhr.onload = function (e){
    var blob = new Blob([xhr.response]);
    var url = URL.createObjectURL(blob)

    console.log(url);

    var embed=document.getElementById("template");
    embed.src = url;
}

xhr.send();

The result will return a blob from console=> blob:http://localhost:8081/d42939da-e318-4d88-b46f-8240efaa7b1c and once i paste this on the url, it will show me a huge blob text

Share Improve this question asked Jan 9, 2019 at 18:56 Brandon YuBrandon Yu 591 gold badge1 silver badge5 bronze badges 1
  • Possible duplicate of How to go from Blob to ArrayBuffer – jtate Commented Jan 9, 2019 at 19:02
Add a ment  | 

1 Answer 1

Reset to default 3

You can set the content type when creating the blob to tell it is a pdf.

var blob = new Blob([xhr.response], { type: 'application/pdf' });
发布评论

评论列表(0)

  1. 暂无评论