How write correctly pdf


#1

I have this data, got it from response and want to write it in pdf file

%PDF-1.4
%???
3 0 obj
<</ColorSpace/DeviceGray/Subtype/Image/Height 128/Filter/FlateDecode/Type/XObject/Width 128/Length 655/BitsPerComponent 8>>stream
.
.
.
.
.
trailer
<</Info 17 0 R/ID [<7e53b5661e4d9e5c16d58815e32b4934><87c2c2316f2ca19b500d84e012f2af17>]/Root 16 0 R/Size 18>>
startxref
444949
%%EOF

I tried this but file it seems to be corrupted, what i’m doing wrong?

var pdfFile = fs.File.fromPath("/storage/emulated/0/Android/data/package/files/cot.pdf");//file created
var file = new java.io.File("/storage/emulated/0/Android/data/package/files/", “cot.pdf”);
var output = new java.io.FileOutputStream(file);
var string = new java.lang.String(data);
var input = new java.io.ByteArrayInputStream(string.getBytes());
var buf = Array.create(“byte”, 1024);
var len;
while((len = input.read(buf)) > 0)
{
//console.log('Bytes: '+len);
output.write(buf, 0, len);
}
output.flush();
output.close();