dcsimg

When downloading a PDF document that is less than 8000 bytes long, the document is downloaded but is not displayed. How can I fix this?

Alessandro A. Garbagnati

I've had a similar problem before when I'm sending a PDF that I'm generate. The problem seems to happen when the PDF file is smaller than the buffer size of the response object (8K by default).

We've solved the problem with a little help from the buglist of Microsoft IE knowledge base. Setting the Content-Length in the response object, shoud do the trick.

Working code:

byte[] data = ...;
response.setContentType("application/pdf");
response.setContentLength(data.length);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
out.write(data);
out.flush();
out.close();

For more discussion, see these two threads in the Servlets FAQ and the JSP FAQ.