4
I am trying to generate a pdf in my angular application and I have the following code in controller.js:
function gerarPdf(){
restService.relatorio.save({
id: controller.contrato.id,
folha: controller.receita.folha,
descricao: controller.receita.desc},
function (response){
var file = new Blob([response], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL, "EPrescription");
},
function(err){
throw err;
});
}
Code of the restService:
service.relatorioPart = $resource(REST_API.url + '/relatorios/participacao', {
id: '@id',
folha: '@folha',
desc: '@descricao'
},
{
responseType: 'arraybuffer'
});
The method code in the java controller:
@POST
@Secured
@Path("/participacao")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf")
public Response imprimirRelatorioExtratoCoparticipacao(@Context HttpServletRequest request, HashMap<String, Object> filtros) throws PadraoException, Exception {
try {
Map dadosUsuario = restUtil.dadosUsuario(request);
//Retorna um array de byte com os dados buscados no banco
byte[] bytesRelatorio = nrelatorio.gerarBytesRelatorioExtratoCoparticipacao(filtros, dadosUsuario, "C:\\xxxx\\Desenvolvimento\\as-aplicacao\\");
if (bytesRelatorio == null) {
return null;
}
return Response.ok(bytesRelatorio).build();
} catch (Exception e) {
return Response.status(INTERNAL_SERVER_ERROR).build();
}
}
Performing test by Postman it normally generates the pdf of the report and already opens the window to save the document. I need that in my application it also save this option or open a new window displaying the report.
I gave a console.log(Response) to see if it was bringing the array of bytes and had the result:
Test done by Postman: [! [insert image description here][3][3]
I believe that for some detail I’m not getting to take this byte array of the java api and angular convert to a pdf document. If anyone has the knowledge to help me I would be very grateful. ^^