我iReport Designer 4.7
用来创建.jrxml
. 它非常.jrxml
简单,显示从 JAVA 传递的参数。测试 pdf 是从 iReport Designer 生成的,我可以使用 Adobe Reader 查看它。
现在,这是java代码
我createReport(String reportName, Map params)
从 Servlet 打电话。参数是从 Servlet 设置的。总会有参数传递。
public void generateReport(String fileName, String outFileName, Map paramerterMap,
String reportFormat) throws JRException{
String reportSource = "C:\\jrxml\\" + fileName;
String outFile = null;
downloadPath = "C:\\pdfs\\";
outFile = downloadPath + outFileName;
JRExporter exporter = new JRPdfExporter();
JasperPrint jasperPrint = JasperFillManager.fillReport(reportSource, paramerterMap);
exporter.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING,"UTF-8");
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFile);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.exportReport();
}
public Boolean createReport(String reportName, Map params) {
try {
String jrFile = reportName + ".jasper";
String outFileName = String.valueOf(7884);
outFileName += "_" + reportName + ".pdf";
generateReport(jrFile, outFileName, params, ".pdf");
return true;
} catch (JRException e) {
e.printStackTrace();
log.error("ReportManager--generateReport--JRException: " + e);
return false;
} catch (Exception e) {
e.printStackTrace();
log.error("ReportManager--generateReport--Exception: " + e);
return false;
}
}
我使用的.jar
是 iReport Designer 使用的同一组。(实际上我已经.jar
从 iReport 安装目录复制了文件)
问题:使用 JAVA 代码生成的 pdf 文件损坏。 当我尝试在创建的位置打开 PDF 时,Adobe 阅读器说:
Adobe Reader 无法打开“test.pdf”,因为它不是受支持的文件类型或文件已损坏(例如,它作为电子邮件附件发送并且未正确解码)。
编辑:我没有得到任何异常,生成的文件大小约为 5 KB。
EDIT2:添加 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="Certificate" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7367a6fb-f8da-4ba3-b90d-319807f92789">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<property name="ireport.scriptlethandling" value="0"/>
<property name="ireport.encoding" value="UTF-8"/>
<import value="net.sf.jasperreports.engine.*"/>
<import value="java.util.*"/>
<import value="net.sf.jasperreports.engine.data.*"/>
<parameter name="name" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="27" splitType="Stretch">
<textField pattern="MMMMM dd, yyyy" isBlankWhenNull="false">
<reportElement uuid="b72c0bad-3935-40ba-8d6d-3993cb5122d2" key="textField" x="413" y="0" width="140" height="20"/>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement>
<font size="12"/>
</textElement>
<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="21" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="22" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="141" splitType="Stretch">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement uuid="cbaae0d4-53da-420e-8efa-7f4fcc6900f0" key="name" x="0" y="10" width="530" height="20"/>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Arial" size="11" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="20" splitType="Stretch"/>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>