I must to migrate PF 5.3 to 7.0 and i have an issue with a XmlHttpRequest.
In 5.2 and 6.1 xhr of callPrint function return the correct XML response but since 6.2 it return only the PDF file with parse error.
This code is used to generate and download a PDF file.
Has anyone ever had this issue ?
Code: Select all
<p:commandButton id="impression" action="#{beanPrint.imprimer(false)}"
oncomplete="callPrint(xhr, status, args);"/>
Code: Select all
function callPrint(xhr, status, args) {
if (xhr.getResponseHeader("Content-Disposition") == null) {
return;
}
var contentDisposition = xhr.getResponseHeader("Content-Disposition").split(";");
var filename = "";
var type = "";
var option = "";
var i = 0;
while (i < contentDisposition.length) {
if (contentDisposition[i].indexOf("filename=") > -1) {
filename = contentDisposition[i].replace("filename=", "");
} else if (contentDisposition[i].indexOf("type=") > -1) {
type = contentDisposition[i].replace("type=", "");
} else if (contentDisposition[i].indexOf("option=") > -1) {
option = contentDisposition[i].replace("option=", "");
}
i++;
}
...
}
Code: Select all
public void imprimer(boolean apercu) {
InputStream inputStream;
... Code to generate InputStream from PDF File ...
printService.directPrint(inputStream, "pdf", "");
}
Code: Select all
public void directPrint(InputStream inputStream, String typeFichier, String option) throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setCharacterEncoding("ISO-8859-1");
response.setContentType("application/pdf");
response.addHeader("Content-disposition", "inline;filename=directPrint.pdf" + (typeFichier != null ? (";type=" + typeFichier + ";option=" + option) : "") + ";");
response.setHeader("Cache-Control", "");
response.setHeader("Pragma", "");
DataOutputStream os = new DataOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
response.getOutputStream().flush();
response.getOutputStream().close();
context.responseComplete();
inputStream.close();
}