Problems in displaying the report with Jasperreport

Asked

Viewed 105 times

3

In my work, I am developing a Java EE application, with web service REST (Jersey), Hibernate and Jquery on the front end. All my system requests use ajax and I am having difficulty generating the report and displaying it on the screen for the user (either by opening in the 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 am saving it in a temporary system folder.

  • From what I understand is missing attach the bytes of the file to the object response, would that be? It would be a response.contentLocation(new URI(relatorio.getPath())); ? It’s just a kick, the right way to do it might be something quite different than that!

1 answer

1


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

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

Once 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.