Search code examples
javajasper-reportsexport-to-pdf

Unable to set Default file name using Jasper report


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.


Solution

  • 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.