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?
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?
Share Improve this question edited Mar 18, 2019 at 12:55 Milan asked Mar 18, 2019 at 10:13 MilanMilan 6311 gold badge5 silver badges21 bronze badges 9-
It looks like you should be streaming the
axios
response into aBuffer
or writable stream. Alternatively, pipe your response intofs
- github./axios/axios#axiosconfig – Älskar Commented Mar 18, 2019 at 10:28 - @ethane What should I do? Can you please suggest ? – Milan Commented Mar 18, 2019 at 10:29
-
Could you please add your
axios
request code to your question. – Älskar Commented Mar 18, 2019 at 10:31 - Sure, give me some time. – Milan Commented Mar 18, 2019 at 10:43
- @ethane I've updated my question with axios request code. – Milan Commented Mar 18, 2019 at 12:56
1 Answer
Reset to default 11The 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
});