我有以下代码:
InputStream reportFile = MyPage.this.getClass().getResourceAsStream("test.jrxml");
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("StringParameterName", "show me");
try {
JasperReport report = JasperCompileManager.compileReport(reportFile);
JasperPrint print = JasperFillManager.fillReport(report, parameters);
return JasperExportManager.exportReportToPdf(print);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
test.jrxml看起来像这样(我用iReport生成了它的一部分,我唯一做的就是删除language =“groovy”属性):
<?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="test" pageWidth="595" pageHeight="842" columnWidth="555" 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="0"/>
<parameter name="StringParameterName" isForPrompting="false" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="243" y="77" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$P{StringParameterName}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
因此,我总是得到一个空白的pdf文件。可能是什么原因?
答案 0 :(得分:4)
而不是
JasperPrint print = JasperFillManager.fillReport(report, parameters);
我用过
JasperPrint print = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
并且有效。
谢谢!