ireport生成pdf文件
生成jrxml
工具:iReport-4.6.0
范例如下:
<jasperReport
name="a"
columnCount="1"
printOrder="Vertical"
orientation="Portrait"
pageWidth="535"
pageHeight="533"
columnWidth="532"
columnSpacing="0"
leftMargin="0"
rightMargin="0"
topMargin="0"
bottomMargin="0"
whenNoDataType="NoPages"
isTitleNewPage="false"
isSummaryNewPage="false">
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<parameter name="JasperfishSubReportLocation" isForPrompting="true" class="net.sf.jasperreports.engine.JasperReport"/>
<parameter name="JasperfishSubReportDatasource" isForPrompting="true" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<parameter name="JasperOrderInfoDTO" isForPrompting="true" class="com.guohualife.ebiz.bpm.order.dto.OrderInfoDTO"/>
<parameter name="SUBREPORT_DIR" isForPrompting="true" class="java.lang.String">
<defaultValueExpression >defaultValueExpression>
parameter>
<field name="time" class="java.lang.String"/>
<background>
<band height="0" isSplitAllowed="true" >
band>
background>
<title>
<band height="86" isSplitAllowed="true" >
<image evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="0"
y="0"
width="535"
height="86"
key="image-1"/>
<box>box>
<graphicElement stretchType="NoStretch"/>
<imageExpression class="java.lang.String">imageExpression>
image>
band>
title>
<pageHeader>
<band height="29" isSplitAllowed="true" >
<staticText>
<reportElement
x="166"
y="0"
width="169"
height="29"
key="staticText-1"/>
<box leftPadding="1" rightPadding="1" topPadding="1" bottomPadding="1">box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="黑体" size="15" isItalic="false" isUnderline="false"/>
textElement>
<text>text>
staticText>
band>
pageHeader>
<columnHeader>
<band height="27" isSplitAllowed="true" >
<staticText>
<reportElement
x="166"
y="0"
width="169"
height="24"
key="staticText-2"/>
<box leftPadding="1" rightPadding="1" topPadding="1" bottomPadding="1">box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="11" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
band>
columnHeader>
<detail>
<band height="390" isSplitAllowed="true" >
<staticText>
<reportElement
x="15"
y="11"
width="51"
height="22"
key="staticText-3"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="10" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="176"
y="11"
width="53"
height="22"
key="staticText-6"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="10" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="349"
y="11"
width="42"
height="22"
key="staticText-5"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="10" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="391"
y="11"
width="144"
height="22"
key="orderNo"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<staticText>
<reportElement
x="15"
y="43"
width="51"
height="24"
key="staticText-7"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="10" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="176"
y="43"
width="53"
height="24"
key="staticText-8"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="10" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="229"
y="43"
width="110"
height="24"
key="successDate"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<staticText>
<reportElement
x="349"
y="43"
width="47"
height="24"
key="staticText-9"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="10" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="396"
y="43"
width="124"
height="24"
key="orderStatus"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<staticText>
<reportElement
x="15"
y="102"
width="124"
height="30"
key="staticText-24"/>
<box> <pen lineWidth="0.5" lineStyle="Solid"/>
<topPen lineWidth="0.5" lineStyle="Solid"/>
<leftPen lineWidth="0.5" lineStyle="Solid"/>
<bottomPen lineWidth="0.5" lineStyle="Solid"/>
<rightPen lineWidth="0.5" lineStyle="Solid"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="139"
y="102"
width="125"
height="30"
key="staticText-23"/>
<box> <pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="264"
y="102"
width="125"
height="30"
key="staticText-22"/>
<box> <pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="389"
y="102"
width="125"
height="30"
key="staticText-16"/>
<box> <pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="15"
y="132"
width="124"
height="30"
key="realName"/>
<box> <pen lineWidth="0.5" lineStyle="Solid"/>
<topPen lineWidth="0.5" lineStyle="Solid"/>
<leftPen lineWidth="0.5" lineStyle="Solid"/>
<bottomPen lineWidth="0.5" lineStyle="Solid"/>
<rightPen lineWidth="0.5" lineStyle="Solid"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="139"
y="132"
width="125"
height="30"
key="productName"/>
<box> <pen lineWidth="0.5" lineStyle="Solid"/>
<topPen lineWidth="0.5" lineStyle="Solid"/>
<leftPen lineWidth="0.5" lineStyle="Solid"/>
<bottomPen lineWidth="0.5" lineStyle="Solid"/>
<rightPen lineWidth="0.5" lineStyle="Solid"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="264"
y="132"
width="125"
height="30"
key="investmount"/>
<box> <pen lineWidth="0.5" lineStyle="Solid"/>
<topPen lineWidth="0.5" lineStyle="Solid"/>
<leftPen lineWidth="0.5" lineStyle="Solid"/>
<bottomPen lineWidth="0.5" lineStyle="Solid"/>
<rightPen lineWidth="0.5" lineStyle="Solid"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="389"
y="132"
width="125"
height="30"
key="valueDate"/>
<box> <pen lineWidth="0.5"/>
<topPen lineWidth="0.5"/>
<leftPen lineWidth="0.5"/>
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<staticText>
<reportElement
mode="Transparent"
x="15"
y="215"
width="630"
height="15"
key="staticText-21"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="15"
y="274"
width="169"
height="17"
key="staticText-10"/>
<box>box>
<textElement verticalAlignment="Bottom">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
x="15"
y="302"
width="188"
height="19"
key="staticText-11"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<image evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="429"
y="235"
width="90"
height="90"
key="image-2"/>
<box>box>
<graphicElement stretchType="NoStretch"/>
<imageExpression class="java.lang.String">imageExpression>
image>
<frame> <reportElement
mode="Opaque"
x="0"
y="350"
width="535"
height="40"
backcolor="#B5B5B5"
key="frame-1"
positionType="FixRelativeToBottom"/>
<box>box>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="254"
y="6"
width="95"
height="25"
forecolor="#C6090F"
key="textField-9"/>
<box>box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font pdfFontName="Helvetica" size="11" isItalic="false" isUnderline="true"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<staticText>
<reportElement
x="0"
y="6"
width="258"
height="25"
key="staticText-33"/>
<box>box>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="11" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
<staticText>
<reportElement
mode="Transparent"
x="345"
y="6"
width="108"
height="25"
key="staticText-34"/>
<box>box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font pdfFontName="STSong-Light" size="11" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<text>text>
staticText>
frame> <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="229"
y="11"
width="110"
height="24"
key="voucherDesc"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="66"
y="43"
width="95"
height="24"
key="voucherName"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="66"
y="11"
width="95"
height="24"
key="voucherSource"/>
<box>box>
<textElement verticalAlignment="Middle">
<font pdfFontName="STSong-Light" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
textElement>
<textFieldExpression class="java.lang.String">textFieldExpression>
textField>
band>
detail>
<columnFooter>
<band height="0" isSplitAllowed="true" >
band>
columnFooter>
<pageFooter>
<band height="0" isSplitAllowed="true" >
band>
pageFooter>
<summary>
<band height="0" isSplitAllowed="true" >
band>
summary>
jasperReport>```
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
/**
* * irport生成pdf
* 引入的包:(1) jasperreports-3.7.6.jar 依赖的包
* commons-collections-3.2.1.jar commons-logging-1.1.1.jar commons-beanutils-1.8.3.jar commons-digester-2.1.jar
* (2) itext-2.1.7.jar iTextAsianCmaps.jar iTextAsian.jar
*
* @version 1.0
* 创建时间:2016年12月16日 下午7:04:45
* 类说明
*/
public class TestIReport {
private static String jrxmlSourcePath = "/home/weblogic/test/ireport/a.jrxml";
private static String jasperFilePath = "/home/weblogic/test/ireport/a.jasper";
private static String pdfFilePath = "/home/weblogic/test/ireport/a.pdf";
public static void main(String[] args) throws Exception {
//1.将jrxml编译生成a.jasper
System.out.println(jrxmlSourcePath);
JasperCompileManager.compileReportToFile(jrxmlSourcePath, jasperFilePath);
/*
* 将a.jasper-->生成pdf文件
*/
//2.
Map parameterMap = new HashMap();
// parameterMap.put("dto", dto);
// 3:JasperRunManager生成PDF文件
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameterMap, new JREmptyDataSource());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
exporter.exportReport();
byte[] bytes = baos.toByteArray();
// 4.确定写出文件的位置
File file = new File(pdfFilePath);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
// System.out.println("写入成功");
fos.close();
}
}