如何将字节数组转换为pdf并下载 [英] how to convert byte array to pdf and download

查看:116
本文介绍了如何将字节数组转换为pdf并下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将http响应下载到pdf。我正在生成一个pdf文件,但我收到无法打开PDF的错误。

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


推荐答案

响应中的字符串似乎是Base-64编码的(参考小提琴)。您可以使用 fetch()(或旧版浏览器的 XMLHttpRequest())对其进行解码:

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

这篇关于如何将字节数组转换为pdf并下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆