0

I am building a server using sails which is supposed to pull the pdf content from an endpoint and send it in the response. How do I do this This is what I have tried so far:

fn: async function (inputs, exits) {
    const downloadLink = this.req.param('link');
    const pdf = (await axios.get(downloadLink, { responseType: 'blob' 
    })).data
    if (!downloadLink || !pdf) {
       throw 'not found';
    }
    this.res.attachment('abc').send(pdf);
    return exits.success();
}

This, however, does not seem to work. I have pdf.js installed in my chrome which displays pdf content. When I try to run the code above, it tries to open a file but ends up showing the error "invalid pdf content"

Nahush Farkande
  • 5,290
  • 3
  • 25
  • 35

1 Answers1

0

The following seems to do the trick:

fn: async function (inputs, exits) {
    const downloadLink = this.req.param('link');
    const pdf = (await axios.get(downloadLink, { responseType: 'arraybuffer' 
    })).data
    if (!downloadLink || !pdf) {
       throw 'not found';
    }
    this.res.attachment('abc.pdf').send(pdf);
    return exits.success();
}

Note the change in response type from blob to arraybuffer.

Nahush Farkande
  • 5,290
  • 3
  • 25
  • 35