How to inform download PDF file name using Itext/Lowagie and Primefaces?

Asked

Viewed 554 times

1

I am able to download the generated file, but I can’t set the name nor the . pdf, just download as index.xhtml.

Does anyone know how to fix it?

I call the method to generate PDF like this:

<h:body>
    <h:form>
        <p:panel header="Test Report PDF">
            <p:commandButton value="Report PDF 1 (FileDownload PrimeFaces)" ajax="false">
                <p:fileDownload value="#{reportController.generateReportPDF()}" />
            </p:commandButton>
            <p:commandButton value="Report PDF 2" ajax="false" action="#{reportController.generateReportPDF()}" />
        </p:panel>
    </h:form>   
</h:body>

And this is the way that the PDF:

private String reportName = "Report" + day + month + year + ".pdf" ;

public void generateReportPDF() throws DocumentException {
    FacesContext context = FacesContext.getCurrentInstance(); 
    HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();  
    response.setContentType("application/pdf");  
    response.setHeader("Content-disposition",  "inline=filename=" + reportName);

    Document documentPDF = new Document(PageSize.A4.rotate(), 5f, 5f, 5f, 5f);
    PdfWriter.getInstance(documentPDF, new FileOutputStream(reportName));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(documentPDF, baos);

    documentPDF.open();
    documentPDF.add(new Paragraph("Hello World"));
    documentPDF.add(new Paragraph(new Date().toString()));
    documentPDF.close();

    response.setContentLength(baos.size());
    ServletOutputStream os = response.getOutputStream();
    baos.writeTo(os);
    os.flush();
    os.close();
}
  • 1

    Have you tried with the Content-Disposition attachment? Something like that: response.setHeader("Content-Disposition", "attachment; filename=" + reportName); in place of response.setHeader("Content-disposition", "inline=filename=" + reportName);.

  • It worked... thank you very much friend.

  • Okay, it includes an answer that maybe helps other people too

1 answer

1


Although not standard HTTP specification, the header Content-Disposition is documented to be widely used and search tell the HTTP client how the content that is going in the response should be treated.

In the case of content that are attachments is normally used attachment, may in this case suggest a name for such an attachment, but the HTTP client is not required to use this suggestion.

See the section 19.5.1, where you will see that this specification is derived from RFC 1806, which deal with email message, and therein said the difference between the types of content display. Quoting briefly inline and attachment:

  • Display type inline:

The part of the message shall be marked as inline whether it is intended to be automatically displayed after the main message.

  • Display type attachment:

Parts of the answer can be marked as attachment to indicate that they are separated from the main part of the message and their display should not be automatic, but depends on some action of the user.

In your case you are forming the header incorrectly, trying to suggest something the customer is probably not understanding. As I said the type of content is application/pdf it may be that the HTTP client inferred that it must be saved as an attachment and, in the absence of a name, used the name of the page being displayed.

To tell the HTTP client its name preference for the file to be downloaded, instead of this snippet:

response.setHeader("Content-disposition", "inline=filename=" + reportName);

Use this:

response.setHeader("Content-Disposition", "attachment; filename=" + reportName);

There are many other Rfcs (such as RFC 6266) dealing since specific header, if you want to know more details just a quick search on the internet =)

  • Muito Obrigadoooo

  • 1

    @Mamga OK, if it has been helpful, consider accepting the answer :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.