1
I have an app that uses Jasper-Reports version 6.2.0 with spring-mvc version 3.2.14, java-ee-7, Tomcat 8 and at front-end we use Angularjs. The requisitions Rest are made via ajax.
Spring understands the object perfectly json that comes in the URL and the report seems to be being generated in the right way too, as we test by passing a poorly formatted JSON and we naturally take an exception.
The problem is that when returning to the browser, the request is not interpreted in any way as to download the file and spit only the contents of the file, including without the header that Jasper-Reports generates for PDF files.
When I write the contents of the byte array on Outputstream do Httpservletresponse the file header exits as I can see in the Answer that Chrome shows on the console. This part of the code is visible in the code below the method, but commented.
Method receiving the request:
@RequestMapping(value = RequisicaoPoRestService.ROOT_MAPPING_CIA_REQUISICOES_PO + "/pdfdetalhepedido", method = RequestMethod.GET)
@ResponseBody
@Secured({ "ROLE_CONSULTARPO" })
public ResponseEntity<byte[]> gerarPDFDetalhePedido(final HttpServletResponse response, @PathVariable(RequisicaoPoRestService.ID_CIA) final Long idCia,
@RequestParam("detelhePedidoJson") final String detelhePedidoJson) {
// Parametrizar via properties.
final String caminhoTemplate = "C:\\Projetos\\wasp\\wasp-api\\src\\main\\webapp\\WEB-INF\\pdftemplates\\Blank_A4.jasper";
final byte[] relatorio = PdfUtils.gerarPDFViaJSON(caminhoTemplate, detelhePedidoJson, null);
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
headers.setCacheControl("must-revalidate, no-cache, no-store");
headers.add("Cache-Control", "post-check=0, pre-check=0");
headers.setPragma("no-cache");
headers.setDate(0);
headers.setContentLength(relatorio.length);
headers.setExpires(0);
headers.setLastModified(0);
headers.set("Content-Disposition", "attachment; filename=DetalhePedido.pdf");
// response.reset();
// response.setHeader("Content-Disposition", "inline; filename=" +
// filename);
// response.setContentType("application/pdf");
// response.setContentLength(relatorio.length);
return new ResponseEntity<>(relatorio, headers, HttpStatus.OK);
}
Method generating the report
@SuppressWarnings("unused")
public static byte[] gerarPDFViaJSON(final String caminhoDoTemplateDeNFe, final String json, final String logo) {
try (final ByteArrayInputStream stream = new ByteArrayInputStream(json.getBytes())) {
// Fonte de Dados.
final JsonDataSource ds = new JsonDataSource(stream);
// Parâmetros
final Map<String, Object> params = new HashMap<>();
// params.put("Logo", logo);
// Gerando o relatório
final JasperPrint print = JasperFillManager.fillReport(caminhoDoTemplateDeNFe, params, ds);
// Exportando em pdf.
final byte[] relatorio = JasperExportManager.exportReportToPdf(print);
return Arrays.copyOf(relatorio, relatorio.length);
} catch (final JRException | IOException e) {
PdfUtils.LOGGER.error(e.toString());
return null;
}
}
Any suggestions as to why the request is not being interpreted to download the file, even if I am clearing the cache, setting that in the header of the same one that is a file to download, its size and etc?