Error exporting report to pdf

0

I have an error when exporting a report to pdf I am using Jasper Report 5.6.0 before the report I exported it well but now I do not want to do it and in the log I do not have any error this is the code to print

    public void PDF() throws JRException, IOException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("plantaPU");
    EntityManager em = emf.createEntityManager();
    em.getEntityManagerFactory().getCache().evictAll();
    Query query = em.createQuery("select s from ticket s where s.id='" + ticketID + "'");
    List<ticket> listTicket = (List<ticket>) query.getResultList();
    JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(listTicket);
    String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/reports/report1.jasper");
    JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, new HashMap(), beanCollectionDataSource);
    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
    ServletOutputStream servletOutputStream;
    servletOutputStream = httpServletResponse.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
    FacesContext.getCurrentInstance().responseComplete();

}

and that's what I call from the page

  <p:commandButton value="Imprimir" action="#{verTicketController.PDF()}"/>  
    
asked by Alexander Villalobos 06.07.2017 в 21:05
source

1 answer

1

The problem is that you are writing the content of the server response to download a file. The PrimeFaces button, by default, always triggers an ajax action, and by nature (and security) of the browsers, you can not download files using ajax.

You have two alternatives to solve the problem:

  • Add ajax="false" in your <p:commandButton> to disable the ajax functionality and you can download the file.
  • Use <h:commandButton> of JSF that does not provide ajax.
  • answered by 07.07.2017 / 15:19
    source