我有一个 Node Js 服务器,在其中,我正在从另一个 Web 服务(用于 PDF 文件)获取 blob 数据,现在在收到 blob 后,我想再次将其转换为 PDF 文件。
任何知道如何实现这一目标的人请提供帮助。
这是我到目前为止尝试过的代码块:
const fetch = require('node-fetch');
const Blob = require('fetch-blob');
const fs = require('fs');
fetch(url, options)
.then(res => {
console.log(res);
res.blob().then(async (data) => {
const result = data.stream();
// below line of code saves a blank pdf file
fs.createWriteStream(objectId + '.pdf').write(result);
})
})
.catch(e => {
console.log(e);
});
最佳答案
修改点:
fs.createWriteStream(objectId + '.pdf').write(data)
,请修改res.blob()
至res.buffer()
. .then(res => {res.blob().then()
至.then(res => res.buffer()).then(
. 修改后的脚本:
fetch(url, options)
.then(res => res.buffer())
.then(data => {
fs.createWriteStream(objectId + '.pdf').write(data);
})
.catch(e => {
console.log(e);
});
笔记:
url
和 options
工作正常。 引用:
关于javascript - Node环境下如何将BLOB转为PDF文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61243648/