Tuesday, 16 August 2011

How to show a generated PDF file with in the browser or outside the browser in windows?


You can suggest to the browser that it should show the PDF in a window rather than offering to download it via the Content-Disposition header:
response.addHeader("Content-Disposition", "inline");
The "inline" value suggests that the content be rendered inline (as opposed to the "attachment" value suggesting the browser offer to download it).
Once you've made the suggestion to the browser, the browser may or may not accept your suggestion, depending on its implementation and possibly its user preferences.Please see following lines of code :-



        String pdfFileName = "/pdf-test.pdf";
        String contextPath = getServletContext().getRealPath(File.separator);
        File pdfFile = new File(contextPath+"/"+pdfFileName);
         response.addHeader("Content-Disposition", "inline");
        response.setContentType("application/pdf");
        response.setContentLength((int) pdfFile.length());

        FileInputStream fileInputStream = new FileInputStream(pdfFile);
        OutputStream responseOutputStream = response.getOutputStream();
        int bytes;
        while ((bytes = fileInputStream.read()) != -1) {
            responseOutputStream.write(bytes);
        }

No comments:

Post a Comment