• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PDF download fails in IE8 but works in Mozilla.

 
Mi Jo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing this in a servlet, right? And not in a JSP?
 
Mi Jo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes using Servlet. res object is servlet HTTPServletResponse object.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic