File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes PDF download fails in IE8 but works in Mozilla. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "PDF download fails in IE8 but works in Mozilla." Watch "PDF download fails in IE8 but works in Mozilla." New topic
Author

PDF download fails in IE8 but works in Mozilla.

Mi Jo
Greenhorn

Joined: Jan 24, 2011
Posts: 7
I am encountering one problem where PDF download is failing some times in IE8 (rarely) but always works in Mozilla.

After some research we modified our code to add some headers but still no luck. when we open the file in acrobat reader it says "File is corrupted and acrobat reader fails to open it".


resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment; filename="test.pdf");



ByteArrayOutputStream osb = new ByteArrayOutputStream();




makepdf.writePdf("C:\test.pdf", osb);



resp.setHeader("Pragma", "no-cache");
resp.setHeader("Content-Transfer-Encoding", "binary");
resp.setHeader("Cache-Control" , "no-cache, must-revalidate, post-check=0, pre-check=0");
resp.setHeader("Content-Description", "File Transfer");
resp.setHeader("Expires", "0");
//resp.setBufferSize(osb.toByteArray().length+5);
resp.setHeader("Content-Length",String.valueOf(osb.toByteArray().length));
resp.setContentLength(osb.toByteArray().length);
//resp.setHeader("Connection", "close");

Logger.error(CLASS_ID, "Content Length got for print PDF data " +osb.toByteArray().length, null);

os=resp.getOutputStream();

os.write(osb.toByteArray());

os.flush();
os.close()
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1084
    
  10

Why do you write the PDF into a ByteArrayOutputStream and then write the ByteArrayOutputStream content to the response? Why not just write the PDF straight to the response? Note - you can get the length from a File object. And why to you write the content length out twice?
Mi Jo
Greenhorn

Joined: Jan 24, 2011
Posts: 7
Actually I am generating this PDF on the fly using Apache FOP. I removed that peace of code due as it is quite huge.


And I found if we donot set content-length header success rate of download was very low in IE-8 (40%). Also I checked FOP is retuning proper formatted output all the time with constant content-length for similar data.

I am not able to understand why IE says it is corrupted ( content-lenght is same as successful download)

Any help will be highly appriciated as scenario is happening in production environment.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

You are doing this in a servlet, right? And not in a JSP?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mi Jo
Greenhorn

Joined: Jan 24, 2011
Posts: 7
Yes using Servlet. res object is servlet HTTPServletResponse object.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1084
    
  10

Mi Jo wrote:Actually I am generating this PDF on the fly using Apache FOP. I removed that peace of code due as it is quite huge.


The problem we now have is that your problem could be outside of the code you have posted.


And I found if we donot set content-length header success rate of download was very low in IE-8 (40%). Also I checked FOP is retuning proper formatted output all the time with constant content-length for similar data.


But why set the content length twice?


I am not able to understand why IE says it is corrupted ( content-lenght is same as successful download)

Any help will be highly appriciated as scenario is happening in production environment.


Since that is not your actual code I don't think we have enough to go on.
 
jQuery in Action, 2nd edition
 
subject: PDF download fails in IE8 but works in Mozilla.