如何在新标签页中显示Jasper报告PDF流,ajax请求成功

时间:2018-10-24 11:47:24

标签: jasper-reports

在下面的代码中,我能够将jasper报告(.jasper)文件作为HTML或PDF文件导出到本地文件系统,但无法在新窗口中将报告显示为pdf /标签。

我该怎么做?

处理Jasper报告pdf创建的Spring Controller类方法

    @Controller
@RequestMapping("/MySearchCtrl")
            MySearchCtrl {

            @Autowired
            ServletConext servletContext;

            @RequestMapping(value="/loadJasperReport", method=RequestMethod.GET)
            public void loadJaspeeReport(HttpServletResponse response, HttpServletRequest request){
            String reportSrcFile = "/WEB-INF/MyReport.jasper";
            try {
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition","attachment;inline=My_"Report.pdf");
            List<MyReportBean> beanList = new ArrayList<MyReportBean>();
            // add the beans to the beanList here
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(servletContext.getRealPath(reportSrcFile));
            JasperReport jasperReport = (JasperReport)JRLoader.loadObjct(is);
            JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(beanList);
            Map<String,Object> parameters =  new HashMap<String,Object>();
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,beanCollectionDataSource);
            JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream()); // DOES NOT WORK. NO ERROR Either
            JasperExportManager.exportReportToHtmlFile(jasperPrint,"C:\\Users\dipak\Deskop\report.html"); // DOES WORK
            JasperExportManager.exportReportToPdfFile(jasperPrint,"C:\\Users\dipak\Deskop\report1.pdf"); //DOES WORK
            } catch(Exception e){
             e.printStackTrace();
            }        
            }
        }

我的Java代码调用上述Spring方法

        function generateJasperReport(){
        $.ajax({
        type:"GET",
        async:false,
        url:"/MySearch/SearchResulkts/MySearchCtrl/loadJasperReport",
        data:{},
        success:function(data){
// TODO data is the pdf stream exported by Jasperreports and i need to display it in a new tab
        console.log(data); 
        },
        })}

1 个答案:

答案 0 :(得分:0)

Js文件更改

    function generateJasperReport(){
        window.open('/MySearch/SearchResults/MySearchCtrl/loadJasperReport/'+requestParameter,'_blank');
//_blank is to open in a new tab
            }

Servlet Ctrl更改

     @RequestMapping(value="/loadJasperReport/{requestParam}", method=RequestMethod.GET)
            public void loadJaspeeReport(@PathVariable String requestParam, HttpServletResponse response, HttpServletRequest request){
          // introduced @PathVariable and added requestParam to @RequestMapping
           // remaining code remains same as in question
        }