I'm trying to save response to a POST request as .pdf file, but it keeps opening as a blank document (with correct number of pages). The code is:

```
var data = "credentials_login=xxxx&credentials_time=xxxxxxxxxxxxx&credentials_random=xxxxx&credentials_signature=xxxxxxxxxxxxxxxxxxxxx&PdfType=xxxxx";

var xhr = new XMLHttpRequest();

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {

  $cordovaFile.writeFile(cordova.file.externalDataDirectory, "dc.pdf", this.responseText, true)
     .then(function(success) {
       console.log("success");
     }, function(error) {
       console.log(error.code);
     });
  }
});

xhr.open("POST", "http://xxxxxxx.ru/api/PdfDownload");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("accept", "application/pdf");

xhr.send(data);

```

Interestingly, the same code, sent using Postman, saves a **correct** PDF.

I've been searching for a solution for a couple of days now, but nothing works. Seems like it's something to do with that response is a string and pdf is not, but I'm not sure what exactly. I tried to convert response using some code, found here on stackoverflow, but PDF is blank anyway:

```
 function stringToArrayBuffer(str) {
     var buf = new ArrayBuffer(str.length);
     var bufView = new Uint8Array(buf);

     for (var i = 0, strLen = str.length; i < strLen; i++) {
         bufView[i] = str.charCodeAt(i);
     }

     return buf;
 }

```

Also I have "ground truth" PDF (eg what my PDF should be like) and it looks very similar to my corrupted one, with almost the same size (960KB/916KB) and some slight changes here and there in between "stream" tags. 

Beginning of the correct PDF:

```
%PDF-1.5
%����
1 0 obj
<</Type/XObject/Subtype/Form/Resources<</Font<</Helv 2 0 R>>>>/BBox[0 0 12.2 12.94]/FormType 1/Matrix [1 0 0 1 0 0]/Length 93/Filter/FlateDecode>>stream
x��;
�0�2�6���m$`cx� T�B<�OYX��L���0
d�Ӹ����0s�D��X��9i�帱Yf(��e׹EՉ�����
endstream
endobj

```

and my corrupted one:

```
%PDF-1.5
%����
1 0 obj
<</Type/XObject/Subtype/Form/Resources<</Font<</Helv 2 0 R>>>>/BBox[0 0 12.2 12.94]/FormType 1/Matrix [1 0 0 1 0 0]/Length 93/Filter/FlateDecode>>stream
x��;
�0�2�6���m$`cx� T�B<�OYX��L���0
d������0s�D��X��9i�1Yf(��e�EI�����
endstream
endobj

```

**UPDATE**

FileTransfer code:

```
 var fileTransfer = new FileTransfer();
 var uri = encodeURI("http://xxxxxx.ru/api/PdfDownload");
 var filePath = cordova.file.externalDataDirectory + "card.pdf";

 var params = {};

 params.credentials_login = "xxxxxxx";
 params.credentials_random = "xxxx";
 params.credentials_time = "xxxxxxxxx";
 params.credentials_signature = "xxxxxxx";
 params.id = "xxxxxx";
 params.PdfType = "1";

 fileTransfer.download(
     uri,
     filePath,
     function(entry) {
         alert("download complete: " + entry.fullPath);
     },
     function(error) {
         alert("download error source " + error.source);
         alert("download error target " + error.target);
         alert("upload error code" + error.code);
     },
     false,
     params
 );

```