Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open pdf file in new window using servlet

I am generating a pdf file for gate pass from my web application through servlet. I want to open this newly generated pdf in new window/tab and user should come back to the application from servlet. How can i open pdf in new window/tab? I am generating pdf from itext api. My servlet code snippet is:

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control","must-revalidate, post-check=0,precheck=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/pdf");
        response.setContentLength(baos.size());
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close(); 
like image 211
user3150808 Avatar asked Dec 03 '22 20:12

user3150808


2 Answers

In case if you are using GET request to make a servlet call

GET set the target of link to target="_blank"

<a href="/url/to/servlet" target="_blank"/>

POST

<form method="post" action="url/to/servlet"
  target="_blank">

so browser will make a new GET/POST request in new window/tab and then set the header Content-disposition to inline to render pdf inline instead of prompting a download window

like image 92
jmj Avatar answered Dec 08 '22 00:12

jmj


/*create jsp page*/   
<form action="OpenPdfDemo" method="post" target="_blank">
        <input type="submit" value="post">
    </form>
/* create servlet (servlet name = OpenPdfDemo)*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");



        ServletOutputStream  outs =  response.getOutputStream ();
//---------------------------------------------------------------
// Set the output data's mime type
//---------------------------------------------------------------
response.setContentType( "application/pdf" );  // MIME type for pdf doc
//---------------------------------------------------------------
// create an input stream from fileURL
//---------------------------------------------------------------

File file=new File("X://Books/struts book/sturts_Books/struts-tutorial.pdf");


//------------------------------------------------------------
// Content-disposition header - don't open in browser and
// set the "Save As..." filename.
// *There is reportedly a bug in IE4.0 which  ignores this...
//------------------------------------------------------------
response.setHeader("Content-disposition","inline; filename=" +"Example.pdf" );

BufferedInputStream  bis = null; 
BufferedOutputStream bos = null;
try {

    InputStream isr=new FileInputStream(file);
    bis = new BufferedInputStream(isr);
    bos = new BufferedOutputStream(outs);
    byte[] buff = new byte[2048];
    int bytesRead;
    // Simple read/write loop.
    while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
    }
} 
catch(Exception e)
{
    System.out.println("Exception ----- Message ---"+e);
} finally {
    if (bis != null)
        bis.close();
    if (bos != null)
        bos.close();
}


    }
like image 36
NITIN RATHOUR Avatar answered Dec 07 '22 23:12

NITIN RATHOUR