带有OutputStream的JasperReport无法导出到PDF [英] JasperReport with OutputStream not exporting to PDF

查看:97
本文介绍了带有OutputStream的JasperReport无法导出到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JasperReport将报告导出为PDF.该代码运行良好,控制台/日志中未显示任何异常消息.但是,该报告不会导出到浏览器.换句话说,报告正在创建中,我只是无法下载或访问该报告.

I am using JasperReport to export a report to a PDF. The code runs fine with no exception messages showing up in the console/log. However, the report does not export to the browser. In other words, the report is being created, I just cannot download or gain access to it.

这是导出代码:

public void generatePDFReport(Map<String, Object> parameters, JRDataSource jrDataSource, String resource, String filename)
{
    OutputStream os = null;
    try{
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        os = response.getOutputStream();

        InputStream reportTemplate = this.getClass().getClassLoader().getResourceAsStream(resource);
        byte[] pdf = null;

        try {
            JasperDesign masterDesign = JRXmlLoader.load(reportTemplate);
            masterReport = JasperCompileManager.compileReport(masterDesign);
            masterReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
            JasperPrint masterPrint = JasperFillManager.fillReport(masterReport, parameters, jrDataSource);
            pdf = JasperExportManager.exportReportToPdf(masterPrint);
        } catch (JRException e) {
            log.error(e);
        }
        response.setContentType("application/pdf");
        response.setContentLength(pdf.length);
        response.setHeader("Content-disposition", "attachment; filename=\""+filename+"\"");

        context.responseComplete();

        os.write(pdf);

        pdf = null;
    }catch(Exception e){
        log.error(e);
    }finally{
        try{
            os.flush();
            os.close();
        }catch(IOException e){
            log.error(e);
        }
    }
}

我几乎100%确信代码没有问题,因为它适用于不同的报告(我为其他几个报告运行相同的导出代码,并且除此报告外,对所有其他报告都可以正常工作).

I am almost 100% certain that there is nothing wrong with the code as it works fine for different reports (I run the same export code for several other reports and it works as expected for all of them except for this one).

知道这一点,我认为它必须与报告本身有关.该报告是jrxml JasperReport文件.该报告是使用iReport创建的.但是,我修改了上面的代码,只是将其保存到downloads文件夹中,然后就可以很好地创建报告了.

Knowing this, I figured it must have something to do with the report itself. The report is a jrxml JasperReport file. The report was created using iReport. However, I modified the above code to simply save it to the downloads folder and the report is being created perfectly fine.

因此,问题在于该报告已在后端成功创建,但没有按预期发送到前端(浏览器).

So, the problem is that the report is successfully being created in the backend but it is not being sent to the front-end (browser) as expected.

对于本报告为何行不通的建议,我持开放态度.

I am open to any suggestions as to why this report would not be working.

推荐答案

我想出了解决我问题的方法.最终,我发现报告生成代码或报告没有任何问题,但是存在一个ajax问题,阻止了输出流将报告导出到浏览器.

I figured out a solution to my problem. Ultimately, I found that there was nothing wrong with the report generation code or the reports, but there was an ajax issue that was preventing the outputstream from exporting the report to the browser.

这篇关于带有OutputStream的JasperReport无法导出到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆