Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating blank PDF file from response in nodejs

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?

like image 943
Milan Avatar asked Mar 18 '19 10:03

Milan


1 Answers

The correct responseType value in the axios request config needs to be set to stream as well as pipeing 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
});
like image 120
ethane Avatar answered Nov 13 '22 22:11

ethane