ireport生成pdf文件

来源:互联网 发布:锥形皮带轮数据 编辑:程序博客网 时间:2024/05/22 14:40

ireport生成pdf文件

  1. 生成jrxml

    工具:iReport-4.6.0
    范例如下:

<?xml version="1.0" encoding="UTF-8"  ?><!-- Created with iReport - A designer for JasperReports --><!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"><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 ><![CDATA[".\\"]]></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"><![CDATA["yiqian-top.jpg"]]></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><![CDATA[一仟金融]]></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><![CDATA[理财交易凭证]]></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><![CDATA[凭证来源:]]></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><![CDATA[项目类型:]]></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><![CDATA[订单号:]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getOrderNo()]]></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><![CDATA[凭证名称:]]></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><![CDATA[交易时间:]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getSuccessDate()]]></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><![CDATA[交易状态:]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getOrderStatus()]]></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><![CDATA[姓名]]></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><![CDATA[项目名称]]></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><![CDATA[交易金额]]></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><![CDATA[计息时间]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getRealName()]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getProductName()]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getInvestAmount()]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getValueDate()]]></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><![CDATA[...................................................................................................................................................................................................................................................................................]]></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><![CDATA[咨询电话:400-1000-948]]></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><![CDATA[资金担保:保本保息,由新理益集团担保]]></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"><![CDATA["yiqian-erweima.jpg"]]></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"><![CDATA["www.my1000.cn"]]></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><![CDATA[上海一仟互联网金融信息服务有限公司]]></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><![CDATA[沪ICP备15020040号]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getVoucherDesc()]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getVoucherName()]]></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"><![CDATA[$P{JasperOrderInfoDTO}.getVoucherSource()]]></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>```
  1. 代码实现:
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<String,Object> parameterMap = new HashMap<String,Object>();        // 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();    }}
0 0