Customize PDF file name made in jaspersoft, java and spring

0

I have several reports made in JasperSoft that generate PDF and when generated in Java, when downloading the report on my pc, it is like "document.pdf". How do I give a different name to this report, for example "report_clients.pdf"? The reports have been assembled by jaspersoft and my application is in Java and spring to call the report I do the following:

@GetMapping("/clientes")
public ModelAndView gerarRelatoriosClientes(@AuthenticationPrincipal UsuarioSistema usuarioSistema) throws MalformedURLException, IOException {

    HashMap<String, Object> parametros = new HashMap<String, Object>();

    parametros.put("codigoEmpresa", usuarioSistema.getUsuario().getEmpresa().getCodigo());
    parametros.put("format", "pdf");
    return new ModelAndView("relatorio_clientes", parametros);
}

Can anyone help me?

    
asked by anonymous 19.07.2017 / 19:17

1 answer

0

JasperReports has method JasperExportManager.exportReportToPdfFile(print, "nome_arquivo"); .

Where the second argument is the name of the file. I'll put an example:

public void relatorioTeste(String parametro) {
    parametros.put("parametro", parametro);
    try {
        JasperReport report = (JasperReport)JRLoader.loadObject(getClass().getResourceAsStream("/jasperteste/RelatorioTeste.jasper"));
        geraRelatorio(report);
    } catch (JRException e) {
        //tratar exceção
    }
}

private void geraRelatorio(JasperReport report) {
    try {
        JasperPrint print = JasperFillManager.fillReport(report, parametros, conexao);
        JasperExportManager.exportReportToPdfFile(print, "nome_arquivo");
        con.close();
    } catch (JRException e) {
        //tratar exceção
    } catch (SQLException e) {
        //tratar exceção
    }
    parametros.clear();
}

Perhaps the creation of the PDF on your system is different, but this may help you.

    
19.07.2017 / 19:42