View Full Version : Controller handleRequest returning null and pdf streaming.
martinm
Aug 10th, 2007, 04:25 AM
Hi,
I'm implementing the org.springframework.web.servlet.mvc Controller, where handleRequest returns null for the model and view.
Within this controller I'm streaming a PDF document.My issue is when I call the controller 2 windows open:
1) A browser window with the url that's mapped to the controller with a blank display
2) The pdf document itself.
The javadocs say the following:
"It indicates that this object completed request processing itself, thus there is no ModelAndView to render.
I thought from this that 1) above would not be displayed. I just want 2 to be displayed on it's own. Any ideas why this is happening?
The I/O code is like the following:
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "inline; filename=" + fileName);
public static void writeStream(InputStream in, OutputStream out)
throws Exception {
try {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1)
out.write(buffer, 0, bytesRead);
} catch (Exception e) {
log.throwing(logClass, "writeStream", e);
throw new Exception("could not write stream to output stream. \n"
+ e.toString());
}
Thanks,
Martin
Colin Yates
Aug 10th, 2007, 04:43 AM
(please put all code fragments in code tags)
The browser is probably popping up a "Do you want to save/open" for the PDF stream returned by the server. Therefore there is nothing to display in the original window....
If you want the original window to not be modified, then you need to tell the browser to render the response in a new window. If you are opening the PDF by clicking on a link, simply specify a "target='_blank'" attribute on the link element. If you are selecting the PDF by submitting a form, put the same attribute onto the <form element.
martinm
Aug 10th, 2007, 06:18 AM
Hi Colin,
Thanks for the reply. I'd actually done that with a similar result.
<Code>
<a href="<c:out value='${viewInWindowURL}'/>" target="_blank">
</Code>
I'm trying to open the PDF by clicking on a link as suggested. The link passes through the controller to retrieve the pdf from a database.
e.g. http://localhost:8080/selfsolve/document/KM24926/binary/ACT35_Administration_and_Advanced_Use_Vol1_POL_pdf
If I click on the link it opens the 2 windows. If I just enter the link directly in a browser, it shows a popup with a file download and then it asks me if I want to save the file.
Is there a way to skip "Do you want to save/open" for the PDF stream returned by the server" and just open it as a default?
Thanks,
Martin
martinm
Aug 10th, 2007, 08:29 AM
In Adobe Reader, Edit>Preferences> Internet> deselecting Display PDF in browser solved this issue.
The thing is I'm not sure I can control what a user does with adobe. The question then is whether there is some sort of response.setHeader(" ") value that can be set?
Jörg Heinicke
Aug 10th, 2007, 10:03 AM
You already have the correct headers, but maybe not the values. Try to set the content type to "application/pdf" and the content disposition to "attachment" instead of "inline" and see if this matches your expectations. And where do you set those headers? Maybe they are not applied at all. You have to set the headers before you write the first byte of content.
Joerg
martinm
Aug 10th, 2007, 11:00 AM
Thanks Joerg,
Is there anyway to bypass the pop-up dialog asking to save/open and open the pdf directly?
Martin
pmularien
Aug 10th, 2007, 11:28 AM
Yes - content-disposition: inline should do it, as well as content-type application/pdf. Having dealt with this extensively at a prior company, remember that this will still not always behave the same, because the behavior can and will change based on (1) user's browser (IE/FF/etc), (2) Acrobat version (5, 6, 7, 8), and (3) Individual user's Acrobat settings.
Also, a notable bug for IE is to make sure the filename in the content-disposition header ends in .pdf.
pmularien
Aug 10th, 2007, 11:29 AM
One more thing to remember is to make sure you set the Content-Length header as well to the size of the PDF you are sending. IIRC some combinations of browser/Acrobat don't like it if you forget that :)
Jörg Heinicke
Aug 11th, 2007, 04:40 PM
Martin,
from what I understand you want to display the PDF immediately, but directly in Acrobat and not in the browser via the plug-in? I fear you can't do this. You can only tell the browser if it should handle the response itself (inline) or not (attachment). What happens in the latter case is out of your control and depends on versions and browser settings as Peter already wrote. The dialog you mentioned is the default handling. If the browser is set to "always open this type of files with this application" you get the effect you want. But this is a browser option - for security reasons.
Joerg
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.