Customize pdf file name made in Jaspersoft, java and spring

Asked

Viewed 334 times

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 as "Document.pdf". How do I name this report differently, for example "reportario_clients.pdf"? The reports were assembled by Jaspersoft and my application is in Java and spring to call the report 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 someone help me?

  • Where is the code using jasperReport that you create the PDF?. Post it please

  • Thus, it is a jrxml made in Jaspersoft, it is very extensive for all here, follows the beginning and end of it:

  • In the application has a class called Webconfig, where I have all the cofigurations, the responsible method for detecting japer is this:

  • Jasperreportsviewresolver resolver = new Jasperreportsviewresolver(); resolver.setPrefix("classpath:/reports/"); resolver.setSuffix(".Jasper"); resolver.setViewNames("report_*"); resolver.setViewClass(Jasperreportsmultiformatview.class); resolver.setJdbcDataSource(datasource); resolver.setOrder(0); Return resolve;

1 answer

0


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

Where the second argument is the file name. 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();
}

Maybe creating the PDF on your system is different, but this can help you.

Browser other questions tagged

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