Reporting problems with JasperReport

3

In my work, I'm developing a Java EE application, with web service REST (Jersey), Hibernate and JQuery on the front end. All of my system requests use ajax and I'm having trouble generating the report and displaying it on the screen for the user (either opening it in the browser or downloading it.)

I would like to know the best way to do this, I have seen some sites saying that it is not possible to open the PDF received by Ajax.

I have some filters that the user can choose to generate the report and pass these filters through a POST to the web service that returns me like this (I believe that part is right):

File relatorio = gerarRelatorioExtratoVendas(movimentos, usuarioLogado, filtros); 
ResponseBuilder response = Response.ok(relatorio); response.type("application/pdf"); 
response.header("Content-Disposition", "attachment; filename=" + relatorio.getName()); 
return response.build();

Now I need to receive it and show it to the user, I'm saving it to a temporary system folder.

    
asked by anonymous 01.08.2018 / 19:52

1 answer

0

The first thing you should do is test to see if your Rest API is returning the data correctly. To return a file via rest using Jax-RS you can do this:

@GET
@Path("/relatorio")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile() {
  File file = gerarRelatorioExtratoVendas(movimentos, usuarioLogado, filtros); 
  return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
      .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional
      .build();
}

Whereas the pdf file is actually being generated locally a call to this url in your browser should return the report.

Since this url is generating the expected and returning the report will depend on the front end technology you are using to define how to display it.

    
01.09.2018 / 02:54