java – Problems displaying report with JasperReport

Question:

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

I would like to know the best way to do this, I have seen some places 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 I pass these filters through a POST to the web service that returns me like this (I think this 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 temp folder on the system.

Answer:

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

@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();
}

Since 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 what is expected and returning the report will depend on the front end technology you are using to define how to display it.

Scroll to Top
AllEscort