Generate PDF with Jasper and download without storing it

5

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>
    
asked by anonymous 08.07.2015 / 04:01

2 answers

1

Change your FileInputStream to ByteArrayOutputStream ; save the byte array generated by it, and then use ByteArrayInputStream instead of FileInputStream ; in this way, the whole process will be performed in RAM.

Keep in mind, however, that for large reports this will "devour" the server's memory; it may be more appropriate to use File.createTempFile() .

    
05.01.2017 / 13:41
0

You can change the header by placing:

            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.reset();
            response.setContentType("application/pdf");//inline;attachment;
            response.setHeader("Content-disposition", "inline; filename=" + nomedegravacaodorelatorio + ".pdf");
            response.setContentLength(bytes.length);
            servletOutputStream = response.getOutputStream();
            JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
            servletOutputStream.write(bytes, 0, bytes.length);
            servletOutputStream.flush();
            servletOutputStream.close();
            facesContext.renderResponse();
            facesContext.responseComplete();

The command "Content-disposition", "inline; filename=" in the header tells the browser not to save, other than the response.setHeader ("Content-Disposition", "attachment; filename = \" report.pdf \ ""); which in this case the code asks to save the pdf. I hope I have helped.

    
18.07.2015 / 12:52