I have a response from a server which contains the PDF. Something like shown below (just shown a short edited version below):
%PDF-1.4
%????
9 0 obj
%%EOF
Using this string or byte array, how can I create a pdf file ?
I already tried to use the following but it shows blank pages in the pdf
OutputStream oos = new FileOutputStream("test.pdf");
oos.write(b);
I can see the wholeString in console which is a long string starting with %PDF-1.4 and ending with %%EOF. I also tried to read 8192 byte and write. Still same result :
byte[] buf = wholeString.getBytes(Charset.forName("UTF-8"));
OutputStream oos = new FileOutputStream(tpath);
InputStream is = new ByteArrayInputStream(buf);
int c = 0;
while ((c = is.read(buf, 0, buf.length)) > 0) {
System.out.println();
oos.write(buf, 0, c);
oos.flush();
}
oos.close();
is.close();
I am using EntityUtils to get the stream:
String responseBody = EntityUtils.toString(httpResponse.getEntity(), Charset.forName("UTF-8"));
The converted string is then sent to another method where I try to write it to a file.
%PDF-1.1 %¥±ë. Those question marks tell me you've corrupted the bytes when converting to a string.