I've implemented a mechanism to export and some information in a PDF in this way:
public void generatePdf() {
String fileName = "Bolla_" + productionOrder.getOrderNumber();
writePDFToResponse(new GenerateStatusPDF(companyInfo).generate(productionOrder), fileName);
}
private void writePDFToResponse(ByteArrayOutputStream baos, String fileName) {
try {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.responseReset();
externalContext.setResponseContentType("application/pdf");
externalContext.setResponseHeader("Expires", "0");
externalContext.setResponseHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
externalContext.setResponseHeader("Pragma", "public");
externalContext.setResponseHeader("Content-disposition", "attachment, filename=" + fileName + ".pdf");
externalContext.setResponseContentLength(baos.size());
java.io.OutputStream out = externalContext.getResponseOutputStream();
baos.writeTo(out);
externalContext.responseFlushBuffer();
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
This is my .xhtml
file usage:
<h:commandLink>
<p:graphicImage name="/ultima-layout/images/pdf_icon.png"
width="64px" />
<p:fileDownload
value="#{productionOrderStatusView.generatePdf()}" />
</h:commandLink>
I don't why, but I'm facing some problem in the last days with this error:
ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION
Where am I wrong? May be some new update in Google Chrome?
Filename may have comma.
Add a double quotes to your file name in general to avoid repeating characters, something like ("Content-Disposition", "attachment;filename=\"" + filename + "\"")
Please have a look at the standard. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition