我低于axios呼叫的响应:
Click here to download response (PDF)
当我试图从上述链接响应中生成PDF时,空白页面生成的PDF
var fs = require('fs');
fs.writeFileSync("12345678.pdf", response.data, 'binary');
axios通话:
const url = 'url-here'
const headers = {
'headers-here'
};
const axiosConfig = {
headers,
};
axios.get(url, axiosConfig)
.then((response) => {
var fs = require('fs');
fs.writeFileSync("12345678.pdf", response.data, 'binary');
callback(null, response.data);
})
.catch((error) => {
logger.error(error.stack || error.message || error);
callback(error, null);
});
有人可以帮助我生成正确的PDF吗?
答案 0 :(得分:1)
responseType
请求配置中正确的axios
值需要设置为stream
,并且需要pipe
将响应放入流中。
axios({
method:'get',
url: 'someUrl',
responseType: 'stream' // #1
})
.then(function (response) {
response.data.pipe(fs.createWriteStream('12345678.pdf')) // #2
});