I'm trying to generate a pdf in my application made in angular 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;
});
}
RestService code:
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 the test by Postman, it generates the report's pdf and opens the save document window. I need in my application it also from this save option or open a new window displaying the report.
Igaveaconsole.log(response)toseeifitwasbringingthearrayofbytesandIgottheresult:
Test done by postman: [! [insert description of image here] [3]] [3]
I believe that for some detail I am not able to get this byte array from api java and in the angular convert to a pdf document. If anyone has the knowledge to help me I will be very grateful. ^^