我正在尝试使用Spring生成带有Jasper Reports的PDF报告。但报告总是空的。我搜索了很多,找不到问题。我尝试将byte [] pdfReport写入OutputStream,但结果相同,报告始终为空。
我有两个参数,一个是testName,其中一个是图表。两者都无法在pdf报告中显示。
非常感谢你的帮助,
以下是生成报告的代码。
//My class extends MultiActionController
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue(String.format("%s, %s", "pie1", "pie1"),20);
dataset.setValue(String.format("%s, %s", "pie2", "pie2"),80);
JFreeChart chart = ChartFactory.createPieChart("testPie", dataset, true, true, false);
Map model = new HashMap();
model.put("chart", chart.createBufferedImage(200, 200));
model.put("testName", "test report");
model.put("format", "pdf");
AbstractJasperReportsView view = new JasperReportsMultiFormatView();
view.setUrl("/WEB-INF/classes/reports/"+"test1"+".jasper");
view.setApplicationContext(getApplicationContext());
view.setContentType("application/pdf");
Properties header = new Properties();
view.setHeaders(header);
ModelAndView mv = new ModelAndView(view, model);
JasperReport report = (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));
JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);
return mv;
这是jrxml文件
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test1" language="groovy" pageWidth="700" pageHeight="842" columnWidth="660" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="2"/>
<parameter name="testName" class="java.lang.String"/>
<parameter name="chart" class="java.awt.Image" isForPrompting="false"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="48" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="14" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="229" splitType="Stretch">
<image>
<reportElement x="162" y="13" width="200" height="200"/>
<imageExpression class="java.awt.Image"><![CDATA[$P{chart}]]></imageExpression>
</image>
<textField>
<reportElement x="37" y="61" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$P{testName}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="112" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="76" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
答案 0 :(得分:2)
你为什么要这样做?
ModelAndView mv = new ModelAndView(view, model);
JasperReport report = (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));
JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);
return mv;
使用JasperReportsMultiFormatView()已经结合了报告的填充和导出。不需要以下内容:
JasperReport report = (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));
JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);
您必须在资源包中声明JRXML文件和报告数据密钥。
我建议你查看我为 Spring 3 MVC编写的指南 - Jasper Integration Tutorial: http://krams915.blogspot.com/2010/12/spring-3-mvc-jasper-integration.html
只需修改视图即可使用JasperReportsMultiFormatView()。
查看“教程”部分了解更多信息。如果我以后有时间,我将使用JasperReportsMultiFormatView()制作一个教程。如果不是明天也许。如果这有助于你,请告诉我。
答案 1 :(得分:1)
jasper报告中有一个名为'whenNoDataType'的属性。你尝试过这个吗? whenNoDataType = “AllSectionsNoDetail”
报告语言也是'groovy'。为什么呢?