I have a problem saving a pdf file from binary data. I get the binary from the web service, and my express server (as middleware) should send the file to the client. The problem is that the client and adobe acrobat reader shows a clean pdf file, so I think I made some mistake saving / encoding binary data.
exports.downloadReceipt = function(req, res) {
var idPrenotazione = req.params.idPrenotazione;
var options = {
'method': 'GET',
'uri': api_url + '/ricevute/'+idPrenotazione,
'headers': {
'Authorization': req.get('Authorization')
}
}
request(options, function(error, response, body) {
var date = new Date().getTime();
var filename = 'ricevuta_'+idPrenotazione+'_'+date+'.pdf';
var file = folderPath + filename;
fs.writeFile(file, body, function(err) {
if(err)
console.log(err);
else
console.log("The file was saved!");
});
res.end();
});
}
I also tried using createWriteStream instead of writeFile, with and without encoding option
var options = { encoding: 'binary' };
var wstream = fs.createWriteStream(file_);
wstream.write(body);
wstream.end();
The line I get from the web service is something like this:
%PDF-1.5
%
2 0 obj
<</Length1 17948/Length 9587/Filter/FlateDecode>>stream
x {y`՝ !ɖ-Y iɖF e˖,۲lَ v $ s8v B $ (4nHiI RP @ n -P -Wbi H y | ^ }3 p9 -[3 ~ + < l E E7 7 Њ + l\ 1 ?P rt
G !}ؿj ɿ~ @ +H߰
o t & u 5m ??ن 5 ~ e 3 # ~ ԷX %
k\ \z d O x@ A9(- A> @` B0 `+ؠ Q \ < R A * *A5 @- "P
Fh f -
[.....]
0000042111 00000 n
0000042252 00000 n
0000042393 00000 n
trailer
<</Root 7 0 R/ID [<10edca6daaad5a49919bad108ba77f0a><492e2d9a8ca810421f41667217724e69>]/Info 4 0 R/Size 183>>
%iText-5.5.8
startxref
173008
%%EOF
What am I doing wrong? I have a function that receives an image (from the same web service) in base64, and I save it to the server file system using writeFile, and it works fine.
thanks for the help