how to convert byte array to pdf and download

后端 未结 1 1959
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 23:23

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

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 00:05

    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')
      });
    

    0 讨论(0)
提交回复
热议问题