File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes When i try to open pdf, it shows 'There was an error opening this document.' message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "When i try to open pdf, it shows Watch "When i try to open pdf, it shows New topic
Author

When i try to open pdf, it shows 'There was an error opening this document.' message

Sujatha Anandasabarinathan
Greenhorn

Joined: Oct 07, 2008
Posts: 2
I am trying to open a pdf from jsf. Acrobat is opened but it shows error as 'There was an error opening this document. This

file cannot be found.'. When I click the button to open the pdf, it shows 'File Download' window with open, save and cancel

button. After I click Open button, system automatically opens and closes something and then it displays this error message.

Please see below the code.

try {

FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setHeader("Content-Disposition:", "attachment;filename=test.pdf");
response.setHeader("Cache-Control:", "public,max-age=604800");
response.setContentType("application/pdf");
response.setContentLength(pdf.length);

response.getOutputStream().write(pdf);
context.responseComplete();
}
catch (UnsupportedEncodingException e) {
//TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}


Instead of Content-Disposition as attachment if I give 'inline', it works but we should open pdf as an attachment not inline.

response.setHeader("Content-Disposition:", "inline;filename=test.pdf");

Could you please provide solution for this.

Thanks,
Sujatha.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16094
    
  21

Welcome to the JavaRanch!

I think what you're describing is a bug with Acrobat on a certain browser/OS. At least it sounds familiar. If that's true, there's nothing you can do about it. The cure was to have the user download the file and then open the downloaded copy.

The problem seems to be that the attachment went into the temporary downloads directory, but Acrobat was looking either for a different name or a different location, I'm not sure which.


Customer surveys are for companies who didn't pay proper attention to begin with.
Piotr Swiecicki
Greenhorn

Joined: Nov 16, 2006
Posts: 17
Yes, it sounds like relating to Acrobat/MSIE bug. The bug is caused by having spaces in the temporaty folder (like for example TMP="C:\Documents and Settings\User\Temp". The downloaded file is stored in that folder and passed to the acrobat executable in not quoted form, like this:
acrobat.exe C:\Documents and Settings\User\Temp\file.pdf

Invoked with such a command acrobat thinks it was passed 3 separate arguments "C:\Documents", "and" and "Settings\User\Temp\file.pdf" and complains about non existing file.

The workaround I am using for that is serve the files from a link. So in my app instead of sending the content type header and the pdf body I am sending html response with the link to generated document. The user then can click on the link and decide whether he wants to open it or save it. Fortunatelly the open option works in this approach, don't sure what the difference is (file is probably still saved in temp folder and passed to the acrobat executable as a parameter).
Sujatha Anandasabarinathan
Greenhorn

Joined: Oct 07, 2008
Posts: 2
Thanks a lot for your reply. I will try it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
Where does this code live - in a JSP/JSP page? If so, it probably won't work, since you can't really stream binary content (like a PDF) from a JSP page. You need to use a servlet instead.

You should also at least print a message to the log files in your exception handlers. How else are you going to find out about problems?


Ping & DNS - my free Android networking tools app
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16094
    
  21

Ulf brings up a good point - there's no benefit to embedding the PDF code in a JSF process, and I'm surprised it worked at all.

JSF isn't the type of system where JSF has to be in control all the time. If a given web page makes more sense as a plain old JSP or servlet, just do a plain JSP/servlet. You don't need to shoehorn it into JSF.

If this is the error we think it is, it won't show up in the server log, though, since the problem is on the client.
 
GeeCON Prague 2014
 
subject: When i try to open pdf, it shows 'There was an error opening this document.' message