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 Cannot open PDF directly, save works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cannot open PDF directly, save works" Watch "Cannot open PDF directly, save works" New topic
Author

Cannot open PDF directly, save works

Henrik Bentel
Greenhorn

Joined: Jan 29, 2004
Posts: 6
Hi

I'm having a strange problem

I have a webapp that can generate different file types(PDF, XLS, CSV)
Once file is generated, the response is sent where browser asked to open or save file. Save works for all files(including PDF), no problem.

Open however, works for every type except PDF. When clicking open, adobe reader starts, then it displays a popup error message saying: "There was an error opening this document. This file cannot be found".

I set content type to application/pdf. Although I've tried a bunch more:
application/download, application/force-download, application/octet-stream
application/x-unknown, multipart/download, etc.

I set Cache-control header to rivate, no-store, no-cache,
must-revalidate.
Pragma = no-cache

Content-disposition is set to : "attachment;filename="someFileName.pdf"

content-length is correctly set.

To work around an Excel problem I had to add extra url mapping where a request url which ended in the filename that matched the filename passed in content-disposition would generate the same file again. This is a known workaround for how retarded windows/Office/IE works, as Excel retrieves the file itself from the web server using the filename even if the browser has retrieved the file already. This only seems to happen with attached, not inline. Inline is not an option for me however.

This "filename URL" workaround is also available to whatever other programs opens PDF files, like Adobe reader. And looking at my logging I see adobe reader sending the request to get the document with the filename as the last part of the URI. The document is generated and sent back. So in effect it is generated twice. Which his fine, if it only worked!!

On linux everything works fine, PDFs opens fine in both ghost view or acroread. Quality programs!

I've run out of ideas, anyone see what I've forgotten?
Help!
Henrik Bentel
Greenhorn

Joined: Jan 29, 2004
Posts: 6
In case someone else wonders, and just to reply to myself,

I found the solution at http://www.lowagie.com/iText/faq.html#msi.

Turns out my "Cache-Control" header was wrong.

In the servlet(actually, a struts action) that generate the files, I now set the headers to:
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");

And everything finally works.
Sergio Pereira
Greenhorn

Joined: Nov 10, 2004
Posts: 1
Henrik, you just saved my day!
It took me forever searching the Net until I found your post. Great job.
Linh Hong
Greenhorn

Joined: Nov 18, 2004
Posts: 1
Henrik,
Thank you very much.
Your reply help me a lot.

LH
[ November 18, 2004: Message edited by: Linh Hong ]
 
wood burning stoves
 
subject: Cannot open PDF directly, save works