Unable to set Default file name using Jasper report

1.6k views Asked by At

I am creating api for downloading my report in pdf format. For that I am using java (spark framework) and jasper reporting tool everything is working fine.

Problem is that when I am hitting my api from postman the default name for the PDF is "response.pdf.pdf" and I want it to be "report.pdf"

here is my method.

private Route getReport = (req,res)->{
        try{
            res.raw().setHeader("Content-Disposition", "attachment; filename= \"report.pdf\"");
            res.raw().setContentType("application/pdf");
            JRDataSource dataSource = new JREmptyDataSource();
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("cin", "556293-9982");
            dataMap.put("financialYear","2009-05-01 - 2010-04-30");
            JasperPrint jasperPrint = JasperFillManager.fillReport("reports/front-page.jasper", dataMap,dataSource);
            JasperExportManager.exportReportToPdfStream(jasperPrint,res.raw().getOutputStream());
        }catch(Exception ex){
            res.type(ApplicationConstants.JSON_APPLICATION_CONTENT_TYPE);
            throw new BusinessExceptions(ex, ApiErrorEnumerations.ERR_DOWNLOADING_REPORT);
        }finally{
            res.raw().flushBuffer();
            res.raw().getOutputStream().close();
        }
        return res.raw();
    };

Please suggest where I am going wrong.

2

There are 2 answers

0
Amit Jamwal On BEST ANSWER

I think there is a problem with postman with renaming pdf file.

I tried with a sample app and try to download my report from Browser and it is working fine with the name.

thanks for help Alex k and m 1987 really appreciate.

0
m 1987 On
  • For jasper report API

If you are using JasperReports API you should set report name with help of JasperPrint.setName(java.lang.String name) method.

  • For iReport (editing report template) set report name in report template with help of name attribute: jasperReport .. name="Sample report name to print"