I'm generating a PDF with Jasper, I'll show you how it's done.
I'm saving to disk, storing the path in the database and then downloading. I would like to know how to do so that at the moment I click to generate, it generate and already download. I'm using the Download file from primefaces
.
How to do it before it does not save, just download it?
public void imprimirRelatorio(GeradorEdital[] g) throws IOException, ParseException {
// O g é o meu dataSource.
private StreamedContent file;
HashMap parametros = new HashMap();
parametros.put("ID_EDITAL", edital.getIdEdital());
FacesContext facesContext = FacesContext.getCurrentInstance();
try {
JRBeanArrayDataSource arrayDs = new JRBeanArrayDataSource(g, false);
String caminhoWebInf = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/WEB-INF/");
String caminhoReports = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/WEB-INF/reports");
JasperPrint impressoraJasper = JasperFillManager.fillReport(caminhoWebInf+"\reports\Edital.jasper", parametros, arrayDs);
String caminhoFinal = "\edital"+""+edital.getTitulo()+""+edital.getDataCriacao().getTime();
File pdf = new File(caminhoReports+caminhoFinal);
pdf.createNewFile();
FileOutputStream arquivo = new FileOutputStream(pdf);
JasperExportManager.exportReportToPdfStream(impressoraJasper, arquivo);
edital.setSrcPDF("//reports/"+caminhoFinal);
editalDAO.atualizarEdital(edital);
edital = null;
disciplinaBean.setDroppedDisciplinas(null);
arquivo.flush();
arquivo.close();
} catch (JRException e) {
e.printStackTrace();
}
}
public void setFile(StreamedContent file) {
this.file = file;
}
public StreamedContent getFile() throws FileNotFoundException {
String caminhoWebInf = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/WEB-INF/");
InputStream stream = new FileInputStream(caminhoWebInf+editalSelecionado.getSrcPDF());
file = new DefaultStreamedContent(stream, "application/pdf", editalSelecionado.getTitulo()+".pdf");
return file;
}
The button in the .xhtml:
<p:commandButton ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);"
icon="ui-icon-arrowthick-1-s">
<p:fileDownload value="#{editalBean.file}" />
</p:commandButton>