0

I'm pretty sure it's gonna be an easy solution but i can't find any help about my issue. I'm creating a pdf file and at the moment users have to save it before opening it. But i have to open it before the user decides to save it or not. But i can't find how to tell iText to open it in a new tab. What sould i do?

Here is my code, i call this 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();

    } finally {
        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 you for your help.

coshikipix
  • 59
  • 1
  • 9

3 Answers3

3

Opening PDF file in new window or tab depends upon your call from HTML. if you are using GET request you can do it 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" will take care of that.

Umais Gillani
  • 608
  • 4
  • 9
  • @coshikipix please look the answer [here](http://stackoverflow.com/questions/11611296/open-new-window-by-post-using-hcommandbutton) by BalusC. this might give you the idea. I believe you have to add `target="_blank"` in form tag or `onclick="this.form.target='_blank'"` in command button. – Umais Gillani Nov 10 '16 at 08:12
  • Thank you a lot, combined with Henry hint it works perfectly! – coshikipix Nov 10 '16 at 08:42
2

Thank you guys,

First i had to change the content disposition to inline :

response.setHeader("Content-Disposition", "inline;filename=\""
                + nomDoc + "\"");

and then i had to tell my command button to open it in a new tab with the target in the onclick :

<p:commandButton process="checkboxContrats"
                        icon="ui-icon-file-pdf" value="Editer les fiches de paie"
                        action="#{paieAgentView.printPaieAgent()}" update="@form"
                        escape="false" ajax="false" onclick="this.form.target='_blank'"/>
coshikipix
  • 59
  • 1
  • 9
-1

Try using these headers:

  Response.ContentType = "application/" & "octet-stream" 
                Response.AddHeader("Content-disposition", "attachment; filename="myfile.pdf")
                Response.AddHeader("Content-Length", buffer.Length.ToString())

                '****  Change next line to use response output stream
                Response.BinaryWrite(buffer)
                Response.Flush()
                Response.Close()
                Response.End()

(Code is in vb but shouldn't be much different in C#). This should download the file in an icon in the bottom of your browser and if opened, open to a new tab or the default application for pdf reading.

Haim Katz
  • 455
  • 3
  • 15