Spring and Jasper - Browser does not understand streaming for download

Asked

Viewed 412 times

1

I have an app that uses version 6.2.0 with version 3.2.14, , 8 and at front-end we use . The requisitions are made via .

Spring understands the object perfectly 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?

1 answer

0


I discovered after much research, trial and error that the problem was not in the code, but in my forgetfulness that requisitions return only text, that is, impossible to return an array of bytes.

Called with a "common" get instead of using calls that are always via Ajax everything came to work.

Browser other questions tagged

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