4

How do I force the browser to display the pdf instead of downloading ? Here is the controller

 @RequestMapping(value = "/preview.pdf", method = RequestMethod.GET)
protected String preivewSection(      
    HttpServletRequest request,
        HttpSession httpSession,
    HttpServletResponse response) {
    try {
        byte[] documentInBytes = getDocument();         
        response.setHeader("Content-Disposition", "inline; filename=\"report.pdf\"");
        response.setDateHeader("Expires", -1);
        response.setContentType("application/pdf");
        response.setContentLength(documentInBytes.length);
        response.getOutputStream().write(documentInBytes);
    } catch (Exception ioe) {
    } finally {
    }
    return null;
}

4 Answers 4

4

If you remove this line, the pdf will open in the browser itself.

response.setHeader("Content-Disposition", "inline; filename=\"report.pdf\"");
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like the above mentioned controller is all we need from the server side, the problem is the browser doesn't support viewing PDF files.

Comments

0

Use this google chrome browser not support preview It.

response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());

But I changed this header as :

response.setHeader("Content-Disposition", "inline; filename=" + file.getName());

preview ok !

Because inline (default value, indicating it can be displayed inside the Web page, or as the Web page)

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Comments

0

after checking the response from the postman, if result like this will work on chrome

Content-Disposition: name="inline"; filename="XXX.pdf"

i think the questioner param less the "name="

Content-Disposition: inline; filename="XXX.pdf" (not working on chrome)

if not set Content-Disposition some framework will auto add a default become

Content-Disposition: name="attachment"; filename="f.txt" (not working on chrome)

some plugin auto add "form-data; "before the header become:

Content-Disposition: form-data;name="inline"; filename="XXX.pdf" (not working on chrome)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.