Jasper报告无法导出为PDF

时间:2019-01-27 21:55:31

标签: java spring spring-boot tomcat jasper-reports

我正在尝试将jasper报告导出为PDF,但是它不起作用。

这是我正在使用的代码。
注意:contextPath变量包含实际上下文。它具有指向.war文件的绝对路径的值

  

C:\ Program Files \ Apache Software Foundation \ Tomcat   8.5 \ webapps \ myWarFile \

String sourceFileName = 
    contextPath + "WEB-INF/classes/report/ClsReportBean.jasper";
JasperFillManager.fillReportToFile(sourceFileName, "C://sample_report.pdf", 
    parameters, new JRBeanCollectionDataSource(lstDataSource));

问题是当我尝试将报告导出为PDF时。这是错误消息。

  

net.sf.jasperreports.engine.JRException:保存文件时出错:   C:\ sample_report.pdf。

Error part 1
Error part 2

谢谢。

2 个答案:

答案 0 :(得分:0)

JasperFillManager.fillReportToFile不会生成PDF,而是会生成序列化的填充报告对象。由于某种原因,已填充的报告引用了DefaultJasperReportsContext实例,该实例不可序列化。那不应该发生,但是要了解为什么会发生,我们需要有关此案的更多细节。

但是,如果要生成PDF,则无需序列化已填充的报告。您可以执行以下操作直接生成PDF:

JasperRunManager.runReportToPdfFile(sourceFileName, "C://sample_report.pdf", 
    parameters, new JRBeanCollectionDataSource(lstDataSource));

答案 1 :(得分:0)

我使用此代码解决了问题...

try {

   File file = new File(exportPdfPath);
        if (!file.exists()) {
            LOGGER.info("creating folder");
            file.mkdir();
            file.setWritable(true);
        }


        JasperPrint something = JasperFillManager.fillReport(sourceFileName, parameters, new JRBeanCollectionDataSource(lstDataSource));
        OutputStream output = new FileOutputStream(new File(exportPdfPath+"/test.pdf")); 
        JasperExportManager.exportReportToPdfStream(something, output); 
            output.close();

    }
相关问题