Reputation: 631
I'm getting below response of axios call:
Click here to download response (PDF)
When I'm trying to generate PDF from above link response PDF generated with blank pages
var fs = require('fs');
fs.writeFileSync("12345678.pdf", response.data, 'binary');
axios call:
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);
});
Can anyone please help me to generate correct PDF?
Upvotes: 5
Views: 3758
Reputation: 2577
The correct responseType
value in the axios
request config needs to be set to stream
as well as pipe
ing the response into a writable stream.
axios({
method:'get',
url: 'someUrl',
responseType: 'stream' // #1
})
.then(function (response) {
response.data.pipe(fs.createWriteStream('12345678.pdf')) // #2
});
Upvotes: 11