1

i got this problem when i try to export a datatable information to PDF, the method do all, but it doesn't download the PDF or even generate.

Netbeans version: 8.2

JSF: 2.2

Primefaces: 5.3

Libraries used:
Jasperreports 6.3
poi 3.14
commons-beanutils-1.9.0
commons-collection-3.2.2
commons-digester-2.1
commons-logging-1.1.1
groovy-all-2.4.0
itext-2.1.7.js5
jaxp-ri
jcommon-1.0.23
jfreechart-1.0.19

And my method is:

public void exportpdf(OrdenRetiro or) throws JRException, IOException {
    conexion con = new conexion();
    Map<String, Object> parametros = new HashMap<String, Object>();
    FacesContext context = FacesContext.getCurrentInstance();
    ServletContext servleContext = (ServletContext) context.getExternalContext().getContext();
    parametros.put("RutaImagen", servleContext.getRealPath("/reportes/"));
    parametros.put("cod_ordenretiro", or.getCod_ordenretiro());
    
    String temperatura = or.getEs_temperado()==1?"ReporteFreezer.jasper":"ReporteNoFreezer.jasper";
    
    String dirReporte = servleContext.getRealPath("/reportes/"+temperatura);
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.addHeader("Content-disposition", "attachment;filename=Orden de Retiro"+or.getCod_ordenretiro()+".pdf");
    response.setContentType("application/pdf");

    JasperPrint impres = JasperFillManager.fillReport(dirReporte, parametros, con.getConnection());
    JasperExportManager.exportReportToPdfStream(impres, response.getOutputStream());
    context.responseComplete();            
}

Any idea?

Community
  • 1
  • 1
Jorge
  • 261
  • 1
  • 2
  • 12
  • Did you try to generate report with simple test (export to file)? – Alex K Jan 26 '17 at 15:25
  • I already got other project that has the same structure and the export works, you mean download the file on a folder directly? – Jorge Jan 26 '17 at 15:28
  • You can create a simple console application to check the part of code related to the export report as pdf (with JasperReports API). – Alex K Jan 26 '17 at 15:35
  • Please, always, always, always post an [mcve]. What was wrong cannot be seen on your question. – Kukeltje Jan 26 '17 at 18:47

1 Answers1

1

Got the solution.

When the method works but doesn't export is because the ajax on the xhtml. After doing some research got the answer here

I make the puntual quote.

The first problem is that the <p:commandLink> sends by default an Ajax request. This request is fired by JavaScript code. However, JavaScript can't do anything with a response which contains a file download. Due to security restrictions JavaScript can't spawn a Save As dialogue or something. The response is basically totally ignored.

You need to add ajax="false" to <p:commandLink> to turn ajax off so that the button fires a normal synchronous HTTP request, or you need to replace it by standard <h:commandButton>

Community
  • 1
  • 1
Jorge
  • 261
  • 1
  • 2
  • 12