-1
public class PdfRH { public void doRequest(ControlBlock oSCB) throws Exception {
        HttpServletResponse response;


        response = oSCB.getHttpServletResponse();
        String pdfPath = oSCB.getRequestParameter("printpdf") ;
        System.out.println("*********************************************"+pdfPath);
        File pdf = new File(pdfPath);
        String pdfName = pdfPath.substring(pdfPath.lastIndexOf("/") + 1, pdfPath.length());
        ServletOutputStream stream = null;
        BufferedInputStream buf = null;
        String code_windowClose = "<script>window.onunload = refreshParent;function refreshParent() {alert('yeah!!!!');window.opener.location.reload();}</script>";
        try{
            stream = response.getOutputStream();
            stream.write(code_windowClose.getBytes());
            //    PrintWriter pw = response.getWriter();
            response.setHeader("Content-type","application/pdf");
            response.setHeader("Content-disposition","inline; filename=" + pdfName);  
            response.setHeader("Cache-Control", "no-cache"); 
            response.setHeader("Pragma", "No-cache");
            response.setDateHeader("Expires", 0);  
            response.setContentType("application/pdf");

            FileInputStream input = new FileInputStream(pdf);
           // response.setContentLength((int) pdf.length());
            buf = new BufferedInputStream(input);
            int readBytes = 0;
            /*pw.println("<html>");
            pw.println("<head><title>Hello World</title></title>");
            pw.println("<body>");
            pw.println("<h1>Hello World</h1>");
            pw.println("</body></html>");*/
            //response.getOutputStream().write(b);
            while ((readBytes = buf.read()) != -1)
                stream.write(readBytes);
        }
        catch(Exception ioe){
            throw new ServletException(ioe.getMessage());

        }
        finally 
        {
            if (stream != null)
                stream.close();
            if (buf != null)
                buf.close();
        }
    }

PDF is not shown . the above java code is called on call of a JavaScript function to show a PDF on the webpage and on closing the window in which PDF is shown the parent jsp should reload so i have taken one variable that is String code_windowClose and i am writing the script function, converting the above variable to byte array and passing byte array to stream.write(b) the unload functionality is working fine but the problem is PDF is not shown and it is showing code like "%PDF-1.5 %µµµµ 1 0 obj <>>> endobj 2 0 obj <> endobj 3 0 obj <>/Pattern<>/Font<>/XObject<>/ProcSet[/PDF/Text/Ibs/S/StructParents 0>> endobj 4 0 obj <> stream xœ­T]kÛ@|è?ìã]¨Ï»÷-ÆIC71ô!ôA8Ži‰ej»”öõ_vO’[9²Ý>aä;öfvgFà œŸÇ£Û+À‹ ¸¼Á—œA•gÛ‹Mž,~#zB»Wý|–gïó ®Ç#€õ˜´µd1MÊív¾ndobj 8 0 obj <> endobj 9 0 obj <> endobj 10 0 obj <> endobj 11 0ñ¹ c"Þ\5ÆhYcPPVAPÑ÷¸F£ˆˆš˜šèU÷“•¢»§ša°~§‡3ýUuuuý" , previously before adding the unload functionality PDF was shown properly

BackSlash
  • 21,927
  • 22
  • 96
  • 136
user4188607
  • 9
  • 1
  • 7

1 Answers1

1

Your problem is that once you start writing to the output, the request content is written to the user and the header fields must be submitted beforehand. So setting the header fields afterwards has no effect.

But anyway, it is not possible to "show a PDF on a webpage". A browser can display a PDF file downloaded from the internet in a window or tab. (Maybe a plugin is needed for this.) Either the file you serve is a PDF file, or it is an HTML file. You cannot combine those two different files into one. If you want to refresh the page when the user view the PDF, do it when he clicks the link to download the file, e. g. via an Ajax request.

esel
  • 901
  • 9
  • 20
  • yes the browser is showing a pdf but in the above scenario where i have defined my problem is not so please tell how i can use ajax at the time of saving file – user4188607 Aug 10 '15 at 08:32
  • note : it is not asking for download link but asking for save as and we have to chose the target folder – user4188607 Aug 10 '15 at 08:34
  • Here is an example of how you can use Ajax if you only use JSP as frontend: http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax?rq=1. You can execute the Javascript on the Button's or Link's onclick event. – esel Aug 10 '15 at 09:22