Returning a PDF from a java servlet to IE(Internet Explorer)
This one was a pita because no one had all the information in the right place plus ton's of articles were old. So I figured out a solution, taking into account my findings, which works in firefox, chrome and most importantly IE. Below is the code with my explanation.
byte[] pdfBytes = PDFBytes;
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=RANDOMFILENAME.pdf");
response.setContentLength(pdfBytes.length);
response.addHeader("Accept-Ranges", "bytes");
ServletOutputStream sos = response.getOutputStream();
sos.write(pdfBytes);
sos.flush();
Fixes for IE
First "Content-Disposition", I read this everyone had it and it worked on my local deployment, but not in a test environment so it solved one problem.
"Content-Length", every article had it you can find some info at this link http://www.javakb.com/Uwe/Forum.aspx/java-setup/2168/Serving-PDF-from-a-servlet-blank-screen-in-IE. The premise is IE needs to know the size of the pdf.
"Accept-Ranges: bytes", now this one is what did it for me. I don't know why it works, but I went to a website which displayed a pdf in the browser and saw that in its header. I also read in another website about letting IE know the range of where to find the information, I don't know how to do that, but I tried it and it worked.
Fixes for other browsers
What you will notice is I don't close the ServletOutputStream. I found when I did close it the pdf did not render in Chrome. To be honest I never had it there before, but troubleshooting the IE issue I added it which broke the pdf in Chrome.
All in all I hope I help someone with this. I would like to know why these things worked now that I solved the what.