title: Node express save pdf from binary string
body: <p>I have a problem saving a pdf file from binary data. I get the binary from a web service and my express server (as middleware) should send the file to the client.
  The problem is that client and adobe acrobat reader show a blank pdf, so I'm thinking that I done some error saving/encoding the binary data.</p>
  
  <pre><code>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!");
    });      
  
    // for the moment I only save the file on server file system
    res.end();
  
  
    });
  }
  </code></pre>
  
  <p>I also tried to use createWriteStream instead of writeFile, with and without encoding option</p>
  
  <pre><code>  var options = { encoding: 'binary' };
    var wstream = fs.createWriteStream(file_);
    wstream.write(body);
    wstream.end();
  </code></pre>
  
  <p>The string that I get from web service is something like this:</p>
  
  <pre><code>%PDF-1.5
  %����
  2 0 obj
  &lt;&lt;/Length1 17948/Length 9587/Filter/FlateDecode&gt;&gt;stream
  x��{y`՝���!ɖ-Y�iɖF�e˖,۲lَ��v�$���s8v��B�$�(4nHiI�RP��@�n�����׶���-P�-Wbi����H��������y��|���^��}3�p9� -[3�~�+���&lt; ��l�E���E7�7��Њ�+�l\�1��?P��rt�
  �G��!}ؿj���ɿ~�@�+H߰
  o�t�&amp;��u��5m�??ن�5�~���e�����й��޼�3����#��~���ԷX��%����������
  k\��\z���d���O�x@�A9(-�A&gt;�@`�B0�  �`+ؠ���Q��\ �&lt;��R�A��*��*A5�@-��"P
  �Fh�f�-� 
  </code></pre>
  
  <p>[.....]</p>
  
  <pre><code>0000042111 00000 n 
  0000042252 00000 n 
  0000042393 00000 n 
  trailer
  &lt;&lt;/Root 7 0 R/ID [&lt;10edca6daaad5a49919bad108ba77f0a&gt;&lt;492e2d9a8ca810421f41667217724e69&gt;]/Info 4 0 R/Size 183&gt;&gt;
  %iText-5.5.8
  startxref
  173008
  %%EOF
  </code></pre>
  
  <p>What I'm doing wrong? I just have a function that get an image (from the same web service) in base64 and I save it on server file system with writeFile and it works well.</p>
  
  <p>Thanks for help</p>