streaming pdf files in datapower gateway

254 Views Asked by At

i want to download a pdf which is present in datapower local directory i created a serice and wrote a small gateway script but the pdf in downloading in unreadable format i want the pdf to be downloaded in .pdf format

var urlopen = require('urlopen');
var fs = require('fs');
urlopen.open("local:///pdf.pdf", function (error, response) {
  if (error) {
    session.output.write("openCallback error: " + error.errorMessage+"\n");
  }
  else {
    if (response.statusCode == 200) {
      // You have a 200, so you can read the file
      fs.readFile("local:///pdf.pdf", function(error,data) {
        session.output.write(data);
      });
    }
  }
});
1

There are 1 best solutions below

0
On

Well, first of all, you are reading the the PDF twice with your code, you should use either urlopen, or fs.readFile, but not both...

Make sure that your PDF is downloadable and readable if you download it directly from the file-system (through the UI) to make sure that it has not been "destroyed" while adding it to the DP box.

Secondly, it's always safer to store the PDF as base64 so if you can alter the way the PDF is added, encode it to base64 prior to saving it, then fetch the base64 and decode it just prior to returning it.

You must include the content-type for PDF, "application/pdf", in the response, else DataPower will return it as XML (which is the default), so add a response header for content-type: application/pdf.

I am a bit uncertain if DataPower can handle "application/PDF" (don't remember) but as long as it's not XML or JSON it shouldn't touch it... Else try with "application/octet-stream" if PDF doesn't work.