How to open a new pdf file in a new browser tab with iText Java -


this question has answer here:

i'm pretty sure it's gonna easy solution can't find issue. i'm creating pdf file , @ moment users have save before opening it. have open before user decides save or not. can't find how tell itext open in new tab. sould do?

here code, call method right after "document.close();" :

public void openpdf(string nomdoc, string nomfichier) throws ioexception {      facescontext facescontext = facescontext.getcurrentinstance();     externalcontext externalcontext = facescontext.getexternalcontext();     httpservletresponse response = (httpservletresponse) externalcontext             .getresponse();      // file file = new file(getfilepath(), getfilename());     file file = new file(nomfichier);     bufferedinputstream input = null;     bufferedoutputstream output = null;      try {          input = new bufferedinputstream(new fileinputstream(file),                 default_buffer_size);          response.reset();         response.setheader("content-type", "application/pdf");         response.setheader("cache-control",                 "must-revalidate, post-check=0, pre-check=0");         response.setheader("content-length", string.valueof(file.length()));         // response.setheader("content-disposition", "inline; filename=\"" +         // nomfichier + "\"");         response.setheader("content-disposition", "form-data;filename=\""                 + nomdoc + "\"");         output = new bufferedoutputstream(response.getoutputstream(),                 default_buffer_size);          byte[] buffer = new byte[default_buffer_size];         int length;         while ((length = input.read(buffer)) > 0) {             output.write(buffer, 0, length);         }         output.flush();      } {         close(output);         close(input);     }     facescontext.responsecomplete(); } 

my command button :

<p:commandbutton process="checkboxcontrats"                         icon="ui-icon-file-pdf" value="editer les fiches de paie"                         action="#{paieagentview.printpaieagent()}" update="@form"                         escape="false" ajax="false" /> 

my view :

public void creerpdf(list<fichepaie> listfdp, date datedebut, date datefin) throws documentexception,         ioexception {     servletcontext servletcontext = (servletcontext) facescontext             .getcurrentinstance().getexternalcontext().getcontext();     string nomrelatif = servletcontext.getrealpath(nomfichier);      date dtt = new date();     phrase para = new phrase("date d'édition : " + sdf.format(dtt),             fontfactory.getfont(fontfactory.helvetica, 8, font.normal));     document document = this.getdocumentportraitsmallmargin(nomrelatif,             para);      document.open();     document.add(new chunk(""));      document.close();     openpdf(filename, nomrelatif );  } 

thank help.

opening pdf file in new window or tab depends upon call html. if using request can like

get

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

in case post request

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

where target="_blank" take care of that.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -