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?
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With