how to convert byte array to pdf and download

坚强是说给别人听的谎言 提交于 2019-12-02 15:54:33

问题


I am trying to do a simple task of downloading a http response to a pdf. I am generating a pdf file but I am getting an error of "Failed to open PDF".

Here is what the response looks like.

And here is what I am doing.

 let blob = new Blob([response.data], { type: 'application/pdf' })
 FileSaver.saveAs(blob, 'foo.pdf')

回答1:


The string from the response seem to be Base-64 encoded (ref. fiddle). You can decode it using fetch() (or XMLHttpRequest() for older browsers):

fetch("data:application/pdf;base64," + response.data)
  .then(function(resp) {return resp.blob()})
  .then(function(blob) {
    FileSaver.saveAs(blob, 'foo.pdf')
  });


来源:https://stackoverflow.com/questions/45867157/how-to-convert-byte-array-to-pdf-and-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!