How to inform downloadable PDF file name using IText / Lowagie and Primefaces?

1

I am able to download the generated file, but I can not name it or .pdf, only download as index.xhtml .

Does anyone know how to handle this?

I call the method to generate the 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 I generate 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();
}
    
asked by anonymous 08.08.2015 / 22:12

1 answer

1

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

In case of content that is attached, usually attachment is used, in which case you can suggest a name for that attachment, but the HTTP client is not obliged to use this suggestion.

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

  • Display type inline :
  

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

  • Display type attachment :
  

Parts of the response can be marked as attachment to indicate that they are separate 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 in the wrong way, trying to suggest something that the customer is probably not understanding. As you said the content type is application/pdf it might be that the HTTP client inferred that it should 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 their name preference for the file to be downloaded, instead of this section:

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 ) that treat from specific header, if you want to know more details just one quick search on the internet =)

    
09.08.2015 / 21:46