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();
}
Have you tried with the Content-Disposition
attachment
? Something like that:response.setHeader("Content-Disposition", "attachment; filename=" + reportName);
in place ofresponse.setHeader("Content-disposition", "inline=filename=" + reportName);
.– Bruno César
It worked... thank you very much friend.
– Mamga
Okay, it includes an answer that maybe helps other people too
– Bruno César