2

I'm trying to show a web printed report made with JasperReports 6.2.0 in an application with JSF 2.2. Happens that the report is correctly shown, in a new tab, after setting target="_blank" on my h:form, but the download button doesn't work.

Here's the code:

HttpServletResponse response = (HttpServletResponse) FacesContext context = FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.setContentType("application/pdf");
ServletOutputStream responseStream = response.getOutputStream();
ByteArrayInputStream relatorioSourceStream = new ByteArrayInputStream(reportJasper);
JasperPrint jp = JasperFillManager.fillReport(relatorioSourceStream, parameters, getConnection());
File file = new java.io.File(path);
if (file.exists()) {
  file.delete();
} else if (file.getParentFile() != null) {
  file.getParentFile().mkdirs();
  file.createNewFile();
}
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
SimplePdfExporterConfiguration conf = new SimplePdfExporterConfiguration();
exporter.setConfiguration(conf);
exporter.exportReport();

InputStream is = new FileInputStream(file);
int read = 0;
byte[] bytes = new byte[4096];
while ((read = is.read(bytes)) != -1) {
  responseStream.write(bytes, 0, read);
}
responseStream.flush();
responseStream.close();

Happens that the report is correctly shown, but the download won't do anything.

It's not quite a "save as" option that I want to be shown when the report tab is loaded, It happens that the chrome api's download button tries to save my page (html) instead of saving the content as a .pdf file.

Thanks in advance.

Alex K
  • 22,315
  • 19
  • 108
  • 236
  • I edited the question's text, but it's not the same problem. I want to show the stream pdf inline but, also, I want the download button from chrome API to download the generated PDF, not to download the html from my page. Thanks for your attention. – Rafael Fialho Jul 06 '16 at 18:39

1 Answers1

0

Successfully generated your error: on chomre:Version 51.0.2704.106 m, You have one alternate way on this. You can directly download such file by using

httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + outputFileName);

command also define file name. Hope will find solution to the problem.

Sarz
  • 1,970
  • 4
  • 23
  • 43
  • Thanks for your answer Sarz. Actually I'm currently using two different buttons in my application because of that: one to show the report and another to download using the attachment header. Thanks anyway. – Rafael Fialho Jul 06 '16 at 18:46