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();
}