3

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.

15
  • yes. I have a oos.close() at the end. Commented Feb 7, 2017 at 9:27
  • 3
    If you have a response from a server, you have bytes. You should not convert those bytes to a string and then convert back to bytes if you can avoid it. That's just asking for bugs. Commented Feb 7, 2017 at 9:34
  • 2
    The header should look like %PDF-1.1 %¥±ë. Those question marks tell me you've corrupted the bytes when converting to a string. Commented Feb 7, 2017 at 9:37
  • Actually, I just copied the from my console. I think that is because my console does not show some characters ? Commented Feb 7, 2017 at 9:40
  • Actually no, the Windows console doesn't replace bytes it doesn't understand with question marks. That's a Java thing. So they were converted wrong in Java. Commented Feb 7, 2017 at 9:43

1 Answer 1

3

After the discussion in the comments, this should solve your immediate problem:

byte[] entityBytes = EntityUtils.toByteArray(httpResponse.getEntity());
Files.write(Paths.get("test.pdf"), entityBytes);

Anything coming into your program will always come in as bytes.

If you're not getting bytes from outside the program, that means some framework or library has already done the decoding, possibly incorrectly.

Be aware as a general rule you should always avoid converting to a String unless:

  1. You're 100% sure you need a string. (PDF, JSON, or XML should be stored and parsed as bytes, not as a string)
  2. You're 100% sure the bytes actually represent a string. This is not the case here, since the bytes represent a PDF, not a string.
  3. You're 100% sure which encoding to use to decode the bytes to a string. A PDF is not a UTF-8 string.
Sign up to request clarification or add additional context in comments.

3 Comments

@Aaron It was in response to OP saying that sometimes the data from the server is not a PDF, but XML or JSON instead. I'm trying to be as heavy as possible in my suggestion not to convert bytes to string unless you absolutely have to, since it so often leads to bugs, as demonstrated.
Right, I forgot the other kind of data he was receiving was specifically XML and JSON, so I agree it makes no sense converting them to String as they are received from the server.
That's right. The byte to string conversion was creating the problem !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.