选择显示字体大小

在servlet中使用jfreereport直接产生pdf文件报表

首先说些废话。
基于java开发的web的应用系统的打印问题一直困扰着我。原因是基于web的应用是瘦客户端应用,逻辑集中在服务器上,而打印是基于客户端的行为。

目前基于web的精确打印解决方案主要有两类:一是加强客户端的逻辑;二是在服务器端产生适于精确打印的文件。
流行的打印工具中用applet实现、用浏览器插件实现或是开发嵌入浏览器的客户端的方法属于第一类;而在服务器端产生图片文件、pdf或ps等

精确打印文件的方法属于第二类。

以上流行的解决方案的比较:

方案                打印效果                        浏览器依赖                部署代价                        维护代价
applet实现        页面控制困难                需要java plug-in插件                无需客户端部署                客户端调整
浏览器插件实现        同本地打印,效果好        依赖特定类型版本的浏览器        客户端动态部署                客户端调整
图片文件实现        页面控制困难                无                        无                        无
pdf文件实现        同本地打印,效果好        需要acroreader插件                无需客户端部署                无

pdf文件是流行的文件格式,浏览器和pdf阅读器已成为客户端必备的安装内容。对于通用的打印解决方案我认为pdf文件的方式具有更大的灵活性和可用性。

废话到此为止,下面就给出一个servlet通过模板生成pdf文件的实现:

我选择的是报表工具是jfreereport,当然若简单的文本可以使用itext直接产生。

1. jfreereport与was5.0.x的集成

was5.0.x依据j2ee 1.3规范,jaxp/dom/sax的版本分别为1.1/2/2,与jfreereport依赖的gunjaxp.jar中的一致,其他的包也不存在冲突。因此可以将这些

包直接加到web项目的包目录/web-inf/lib中。
jfreereport的包很多,与读取模板、生成报表和生成pdf文件相关的有:
gnujaxp.jar
itext-1.2.2.jar
itextasian.jar
jcommon-0.9.7.jar
jfreereport-0.8.4_11-core.jar
jfreereport-0.8.4_11-misc-configstore-base.jar
jfreereport-0.8.4_11-output-pageable-base.jar
jfreereport-0.8.4_11-output-pageable-pdf.jar
jfreereport-0.8.4_11-output-support-itext.jar
jfreereport-0.8.4_11-output-support-pagelayout.jar
jfreereport-0.8.4_11-parser-base.jar
jfreereport-0.8.4_11-parser-ext.jar
pixie-0.8.1.jar

jfreereport中自带的itext-1.02b.jar版本低,可以替换为新版本,itextasian.jar是生成中文所必须的。


2. 编码和汉字问题
由于我的环境是英文系统(win2k en version + sp4)加装中文支持,为了在开发环境中支持中文必须使用utf-8编码。因此需要对jfreereport中的一些参数进行配置。
需要将org\jfree\report\modules\output\support\itext\configuration.properties文件中的org.jfree.report.modules.output.support.itext.encoding参数设为gbk (或gb18030,请量力而行,我的环境未安装gb18030的支持)。
需要将显示中文的区域的字体和编码分别设为"stsong-light"和"unigb-ucs2-h"。此字体和编码是生成pdf文件时显示中文所必须的,与jfreereport的gui工具的显示无关。

3. 程序示例
我选用了jfreereport中的java look and feel graphics repository的例子,并将部分内容换为中文。以下是直接生成pdf的servlet代码和我变更的报表模板。



//- genpdfbyjfreereporttemplateservlet.java

package acme.web.servlets;

import java.io.ioexception;
import java.net.url;

import javax.servlet.servlet;
import javax.servlet.servletexception;
import javax.servlet.servletoutputstream;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.jfree.report.boot;
import org.jfree.report.jfreereport;
import org.jfree.report.demo.swingiconsdemotablemodel;
import org.jfree.report.modules.output.pageable.base.pageablereportprocessor;
import org.jfree.report.modules.output.pageable.pdf.pdfoutputtarget;
import org.jfree.report.modules.parser.base.reportgenerator;
import org.jfree.report.util.log;
import org.jfree.report.util.reportconfiguration;
import org.jfree.xml.elementdefinitionexception;

/**
* @version         1.0
* @author         lee s
*/
public class genpdfbyjfreereporttemplateservlet extends httpservlet implements servlet
{
        /**
        * @see javax.servlet.http.httpservlet#void (javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse)
        */
        public void doget(httpservletrequest req, httpservletresponse resp)
                throws servletexception, ioexception
        {
                resp.setcontenttype("application/pdf");

                servletoutputstream out = resp.getoutputstream();
                
                try
                {
                        // initialize jfreereport
                        boot.start();

                        reportconfiguration.getglobalconfig().setloglevel("error");
                        // update the log system to use the new settings ...
                        log.getjfreereportlog().init();

                        final url in =
                                getclass().getresource("/acme/web/resources/swing-icons.xml");

                        if (in == null)
                        {
                                throw new exception("swing-icons.xml can't be found.");
                        }

                        final jfreereport report = parsereport(in);
                        report.setdata(new swingiconsdemotablemodel());

                        pdfoutputtarget target =
                                new pdfoutputtarget(out, report.getdefaultpageformat());
                        target.configure(report.getreportconfiguration());
                        
                        target.open();
                        pageablereportprocessor proc = new pageablereportprocessor(report);
                        proc.setoutputtarget(target);
                        proc.processreport();
                        target.close();


                }
                catch (exception ex)
                {
                        system.err.println("generate pdf failed: " + ex);
                }
                finally
                {
                        try
                        {
                                out.close();
                        }
                        catch (exception e)
                        {
                                system.err.println("output pdf failed: " + e);
                        }
                }

        }

        /**
        * @see javax.servlet.http.httpservlet#void (javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse)
        */
        public void dopost(httpservletrequest req, httpservletresponse resp)
                throws servletexception, ioexception
        {
                doget(req, resp);
        }


        /**
         * reads the report from the swing-icons.xml report template.
         *
         * @param templateurl the template location.
         *
         * @return a report.
         * @throws elementdefinitionexception if the report generator encountered an error.
         * @throws ioexception if there was an io error while reading from the url.
         */
        private jfreereport parsereport(final url templateurl)
                 throws ioexception, elementdefinitionexception
        {
          final reportgenerator generator = reportgenerator.getinstance();
          return generator.parsereport(templateurl);
        }


}




//- swing-icons.xml

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<!doctype report-definition
   public &quot;-//jfreereport//dtd report definition//en//extended&quot;
          &quot;http://jfreereport.sourceforge.net/extreport.dtd&quot;>

<!-- ***************************************************************** -->
<!-- *  swing-icons.xml                                              * -->
<!-- *  a sample report definition used as an introduction to        * -->
<!-- *  jfreereport.                                                 * -->
<!-- ***************************************************************** -->

<report-definition name=&quot;first report&quot;>

  <!-- ************************ -->
  <!-- * parser configuration * -->
  <!-- ************************ -->
  <parser-config>
    <object-factory class=&quot;org.jfree.report.modules.parser.ext.factory.objects.defaultclassfactory&quot;/>
    <element-factory class=&quot;org.jfree.report.modules.parser.ext.factory.elements.defaultelementfactory&quot;/>
    <stylekey-factory class=&quot;org.jfree.report.modules.parser.ext.factory.stylekey.defaultstylekeyfactory&quot;/>
    <stylekey-factory class=&quot;org.jfree.report.modules.parser.ext.factory.stylekey.pageablelayoutstylekeyfactory&quot;/>
    <template-factory class=&quot;org.jfree.report.modules.parser.ext.factory.templates.defaulttemplatecollection&quot;/>
    <datasource-factory class=&quot;org.jfree.report.modules.parser.ext.factory.datasource.defaultdatasourcefactory&quot;/>
  </parser-config>

  <!-- ************************ -->
  <!-- * report configuration * -->
  <!-- ************************ -->
  <report-config>
    <defaultpageformat orientation=&quot;portrait&quot;
      pageformat=&quot;letter&quot;
      topmargin=&quot;72&quot;
      bottommargin=&quot;72&quot;
      leftmargin=&quot;72&quot;
      rightmargin=&quot;72&quot;/>
  </report-config>

  <!-- ********** -->
  <!-- * styles * -->
  <!-- ********** -->
  <styles>
    <style name=&quot;medium-line&quot;>
      <compound-key name=&quot;min-size&quot;>
        <basic-object name=&quot;height&quot;>0.0</basic-object>
        <basic-object name=&quot;width&quot;>-100.0</basic-object>
      </compound-key>
      <basic-key name=&quot;paint&quot;>black</basic-key>
      <basic-key name=&quot;stroke&quot; class=&quot;java.awt.basicstroke&quot;>0.5</basic-key>
      <basic-key name=&quot;draw-shape&quot;>true</basic-key>
      <basic-key name=&quot;scale&quot;>true</basic-key>
    </style>

    <style name=&quot;background-rectangle&quot;>
      <compound-key name=&quot;absolute_pos&quot;>
        <basic-object name=&quot;x&quot;>0.0</basic-object>
        <basic-object name=&quot;y&quot;>0.0</basic-object>
      </compound-key>
      <compound-key name=&quot;min-size&quot;>
        <basic-object name=&quot;height&quot;>-100.0</basic-object>
        <basic-object name=&quot;width&quot;>-100.0</basic-object>
      </compound-key>
      <basic-key name=&quot;fill-shape&quot;>true</basic-key>
      <basic-key name=&quot;scale&quot;>true</basic-key>
    </style>
  </styles>

  <!-- ********************** -->
  <!-- * report description * -->
  <!-- ********************** -->
  <report-description>

    <!-- ================= -->
    <!-- = report header = -->
    <!-- ================= -->
    <report-header name=&quot;report-header-band&quot;>
      <style>
        <compound-key name=&quot;min-size&quot;>
          <basic-object name=&quot;height&quot;>112.0</basic-object>
          <basic-object name=&quot;width&quot;>0.0</basic-object>
        </compound-key>
      </style>

      <default-style>
        <basic-key name=&quot;font&quot;>sansserif</basic-key>
        <basic-key name=&quot;font-size&quot;>10</basic-key>
        <basic-key name=&quot;valignment&quot;>bottom</basic-key>
      </default-style>

      <element name=&quot;title1&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>2.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>18.0</basic-object>
            <basic-object name=&quot;width&quot;>-100.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>center</basic-key>
          <basic-key name=&quot;font-size&quot;>18</basic-key>
          <basic-key name=&quot;font-bold&quot;>true</basic-key>
        </style>
        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>java look and feel graphics repository</basic-object>
        </template>
      </element>

      <element name=&quot;description1&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>32.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>10.0</basic-object>
            <basic-object name=&quot;width&quot;>-100.0</basic-object>
          </compound-key>
        </style>

        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>this report lists the icons contained in the java look and feel graphics repository.</basic-object>
        </template>
      </element>

      <element name=&quot;description2&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>44.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>10.0</basic-object>
            <basic-object name=&quot;width&quot;>-100.0</basic-object>
          </compound-key>
        </style>
        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>for more information about the repository, refer to:</basic-object>
        </template>
      </element>

      <element name=&quot;url1&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>68.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>10.0</basic-object>
            <basic-object name=&quot;width&quot;>-100.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>center</basic-key>
          <basic-key name=&quot;font&quot;>monospaced</basic-key>
          <basic-key name=&quot;font-size&quot;>9</basic-key>
        </style>
        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>http://developer.java.sun.com/developer/techdocs/hi/repository/</basic-object>
        </template>
      </element>

      <element name=&quot;description3&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>92.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>10.0</basic-object>
            <basic-object name=&quot;width&quot;>-100.0</basic-object>
          </compound-key>
        </style>
        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>the design of this report is described in the jfreereport pdf documentation.</basic-object>
        </template>
      </element>

    </report-header>

    <!-- ================= -->
    <!-- = report footer = -->
    <!-- ================= -->
    <report-footer name=&quot;report-footer-band&quot;>

      <style>
        <compound-key name=&quot;min-size&quot;>
          <basic-object name=&quot;height&quot;>30.0</basic-object>
          <basic-object name=&quot;width&quot;>0.0</basic-object>
        </compound-key>
      </style>

      <default-style>
        <basic-key name=&quot;font&quot;>sansserif</basic-key>
        <basic-key name=&quot;font-size&quot;>12</basic-key>
        <basic-key name=&quot;font-bold&quot;>true</basic-key>
        <basic-key name=&quot;valignment&quot;>bottom</basic-key>
      </default-style>

      <element name=&quot;rect1&quot; type=&quot;shape/generic&quot;>
        <style>
          <extends name=&quot;background-rectangle&quot;/>
          <basic-key name=&quot;paint&quot;>#ccffcc</basic-key>
        </style>
        <template references=&quot;rectangle&quot;/>
      </element>

      <element type=&quot;shape/generic&quot;>
        <style>
          <extends name=&quot;medium-line&quot;/>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>0.0</basic-object>
          </compound-key>
        </style>
        <template references=&quot;horizontal-line&quot;/>
      </element>

      <element type=&quot;shape/generic&quot;>
        <style>
          <extends name=&quot;medium-line&quot;/>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>30.0</basic-object>
          </compound-key>
        </style>
        <template references=&quot;horizontal-line&quot;/>
      </element>

      <element name=&quot;reporttotallabel&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>10.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>12.0</basic-object>
            <basic-object name=&quot;width&quot;>-50.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>left</basic-key>
        </style>
        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>report total:</basic-object>
        </template>
      </element>

      <element name=&quot;reportcountfield&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>-50.0</basic-object>
            <basic-object name=&quot;y&quot;>10.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>9.0</basic-object>
            <basic-object name=&quot;width&quot;>-30.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>center</basic-key>
          <basic-key name=&quot;font-size&quot;>9</basic-key>
        </style>
        <template references=&quot;number-field&quot;>
          <basic-object name=&quot;field&quot;>overallcount</basic-object>
          <basic-object name=&quot;format&quot;>#0</basic-object>
          <basic-object name=&quot;nullvalue&quot;>-</basic-object>
        </template>
      </element>

      <element name=&quot;reportsumfield&quot; type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>-80.0</basic-object>
            <basic-object name=&quot;y&quot;>10.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>9.0</basic-object>
            <basic-object name=&quot;width&quot;>-20.0</basic-object>
          </compound-key>
          <basic-key name=&quot;font-size&quot;>9</basic-key>
          <basic-key name=&quot;alignment&quot;>right</basic-key>
          <basic-key name=&quot;valignment&quot;>bottom</basic-key>
        </style>
        <template references=&quot;number-field&quot;>
          <basic-object name=&quot;field&quot;>overallsum</basic-object>
          <basic-object name=&quot;format&quot;>#,##0</basic-object>
          <basic-object name=&quot;nullvalue&quot;>-</basic-object>
        </template>
      </element>
    </report-footer>

    <!-- =============== -->
    <!-- = page header = -->
    <!-- =============== -->
    <page-header>

      <style>
        <compound-key name=&quot;min-size&quot;>
          <basic-object name=&quot;height&quot;>18.0</basic-object>
          <basic-object name=&quot;width&quot;>0.0</basic-object>
        </compound-key>
        <basic-key name=&quot;display-on-firstpage&quot;>false</basic-key>
        <basic-key name=&quot;display-on-lastpage&quot;>true</basic-key>
      </style>

      <default-style>
        <basic-key name=&quot;font-bold&quot;>true</basic-key>
        <basic-key name=&quot;font-size&quot;>9</basic-key>
        <basic-key name=&quot;font&quot;>sansserif</basic-key>
        <basic-key name=&quot;font-italic&quot;>false</basic-key>
        <basic-key name=&quot;paint&quot;>black</basic-key>
        <basic-key name=&quot;valignment&quot;>bottom</basic-key>
      </default-style>

      <element type=&quot;shape/generic&quot;>
        <style>
          <extends name=&quot;background-rectangle&quot;/>
          <basic-key name=&quot;paint&quot;>#afafaf</basic-key>
        </style>
        <template references=&quot;rectangle&quot;/>
      </element>

      <element type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>0.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>14.0</basic-object>
            <basic-object name=&quot;width&quot;>-50.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>left</basic-key>
        </style>
        <template references=&quot;label&quot;>
          <basic-object name=&quot;content&quot;>java look and feel graphics repository</basic-object>
        </template>
      </element>

      <element type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>-80.0</basic-object>
            <basic-object name=&quot;y&quot;>0.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>14.0</basic-object>
            <basic-object name=&quot;width&quot;>-20.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>right</basic-key>
        </style>
        <template references=&quot;date-field&quot;>
          <basic-object name=&quot;nullvalue&quot;>-</basic-object>
          <basic-object name=&quot;format&quot;>d-mmm-yyyy</basic-object>
          <basic-object name=&quot;field&quot;>report.date</basic-object>
        </template>
      </element>

      <element type=&quot;shape/generic&quot;>
        <style>
          <extends name=&quot;medium-line&quot;/>
          <basic-key name=&quot;stroke&quot; class=&quot;java.awt.basicstroke&quot;>2.0</basic-key>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>16.0</basic-object>
          </compound-key>
          <basic-key name=&quot;paint&quot;>#cfcfcf</basic-key>
        </style>
        <template references=&quot;horizontal-line&quot;/>
      </element>
    </page-header>

    <!-- =============== -->
    <!-- = page footer = -->
    <!-- =============== -->
    <page-footer>

      <style>
        <basic-key name=&quot;display-on-lastpage&quot;>true</basic-key>
        <basic-key name=&quot;display-on-firstpage&quot;>true</basic-key>
        <compound-key name=&quot;min-size&quot;>
          <basic-object name=&quot;height&quot;>14.0</basic-object>
          <basic-object name=&quot;width&quot;>0.0</basic-object>
        </compound-key>
      </style>

      <default-style>
        <basic-key name=&quot;font-bold&quot;>false</basic-key>
        <basic-key name=&quot;font-size&quot;>9</basic-key>
        <basic-key name=&quot;font&quot;>sansserif</basic-key>
        <basic-key name=&quot;font-italic&quot;>false</basic-key>
      </default-style>

      <element type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>3.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>9.0</basic-object>
            <basic-object name=&quot;width&quot;>-100.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>center</basic-key>
          <basic-key name=&quot;valignment&quot;>bottom</basic-key>
        </style>
        <template references=&quot;number-field&quot;>
          <basic-object name=&quot;nullvalue&quot;>-</basic-object>
          <basic-object name=&quot;format&quot;>page #0</basic-object>
          <basic-object name=&quot;field&quot;>pagenumber</basic-object>
        </template>
      </element>

    </page-footer>

    <!-- ========== -->
    <!-- = groups = -->
    <!-- ========== -->
    <groups>

      <!-- category group -->
      <group name=&quot;category&quot;>
        <fields>
          <field>category</field>
        </fields>

        <group-header>
          <style>
            <compound-key name=&quot;min-size&quot;>
              <basic-object name=&quot;height&quot;>30.0</basic-object>
              <basic-object name=&quot;width&quot;>0.0</basic-object>
            </compound-key>
          </style>

          <default-style>
<!-- use such font name & encoding for displaying chinese -->
            <basic-key name=&quot;font&quot;>stsong-light</basic-key>
            <basic-key name=&quot;font-encoding&quot;>unigb-ucs2-h</basic-key>
            <basic-key name=&quot;font-size&quot;>12</basic-key>
            <basic-key name=&quot;font-bold&quot;>true</basic-key>
            <basic-key name=&quot;font-italic&quot;>false</basic-key>
            <basic-key name=&quot;paint&quot;>black</basic-key>
            <basic-key name=&quot;alignment&quot;>left</basic-key>
            <basic-key name=&quot;valignment&quot;>bottom</basic-key>
          </default-style>

          <element type=&quot;shape/generic&quot;>
            <style>
              <extends name=&quot;background-rectangle&quot;/>
              <basic-key name=&quot;paint&quot;>#ccccff</basic-key>
            </style>
            <template references=&quot;rectangle&quot;/>
          </element>

          <element type=&quot;shape/generic&quot;>
            <style>
              <extends name=&quot;medium-line&quot;/>
              <basic-key name=&quot;paint&quot;>black</basic-key>
            </style>
            <template references=&quot;horizontal-line&quot;/>
          </element>

          <element type=&quot;shape/generic&quot;>
            <style>
              <extends name=&quot;medium-line&quot;/>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>0.0</basic-object>
                <basic-object name=&quot;y&quot;>30.0</basic-object>
              </compound-key>
              <basic-key name=&quot;paint&quot;>black</basic-key>
            </style>
            <template references=&quot;horizontal-line&quot;/>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>0.0</basic-object>
                <basic-object name=&quot;y&quot;>3.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>12.0</basic-object>
                <basic-object name=&quot;width&quot;>92.0</basic-object>
              </compound-key>
            </style>
            <template references=&quot;label&quot;>
              <basic-object name=&quot;content&quot;>分类:</basic-object>
            </template>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>96.0</basic-object>
                <basic-object name=&quot;y&quot;>3.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>12.0</basic-object>
                <basic-object name=&quot;width&quot;>120.0</basic-object>
              </compound-key>
            </style>
            <template references=&quot;string-field&quot;>
              <basic-object name=&quot;nullvalue&quot;>-</basic-object>
              <basic-object name=&quot;field&quot;>category</basic-object>
            </template>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <basic-key name=&quot;font-size&quot;>8</basic-key>
              <basic-key name=&quot;font-bold&quot;>false</basic-key>
              <basic-key name=&quot;font-italic&quot;>true</basic-key>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>0.0</basic-object>
                <basic-object name=&quot;y&quot;>20.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>9.0</basic-object>
                <basic-object name=&quot;width&quot;>-50.0</basic-object>
              </compound-key>
            </style>
            <template references=&quot;label&quot;>
              <basic-object name=&quot;content&quot;>名称:</basic-object>
            </template>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <basic-key name=&quot;font-size&quot;>8</basic-key>
              <basic-key name=&quot;font-bold&quot;>false</basic-key>
              <basic-key name=&quot;font-italic&quot;>true</basic-key>
              <basic-key name=&quot;paint&quot;>black</basic-key>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>-50.0</basic-object>
                <basic-object name=&quot;y&quot;>20.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>9.0</basic-object>
                <basic-object name=&quot;width&quot;>-30.0</basic-object>
              </compound-key>
            </style>
            <template references=&quot;label&quot;>
              <basic-object name=&quot;content&quot;>图标:</basic-object>
            </template>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <basic-key name=&quot;font-size&quot;>8</basic-key>
              <basic-key name=&quot;font-bold&quot;>false</basic-key>
              <basic-key name=&quot;font-italic&quot;>true</basic-key>
              <basic-key name=&quot;alignment&quot;>right</basic-key>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>-80.0</basic-object>
                <basic-object name=&quot;y&quot;>20.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>9.0</basic-object>
                <basic-object name=&quot;width&quot;>-20.0</basic-object>
              </compound-key>
            </style>
            <template references=&quot;label&quot;>
              <basic-object name=&quot;content&quot;>文件大小:</basic-object>
            </template>
          </element>
        </group-header>

        <group-footer>
          <style>
            <compound-key name=&quot;min-size&quot;>
              <basic-object name=&quot;height&quot;>30.0</basic-object>
              <basic-object name=&quot;width&quot;>0.0</basic-object>
            </compound-key>
            <basic-key name=&quot;pagebreak-before&quot;>false</basic-key>
          </style>
          <default-style>
            <basic-key name=&quot;font&quot;>sansserif</basic-key>
            <basic-key name=&quot;font-size&quot;>11</basic-key>
            <basic-key name=&quot;font-bold&quot;>true</basic-key>
            <basic-key name=&quot;font-italic&quot;>false</basic-key>
            <basic-key name=&quot;valignment&quot;>bottom</basic-key>
          </default-style>

          <element type=&quot;text/plain&quot;>
            <style>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>0.0</basic-object>
                <basic-object name=&quot;y&quot;>5.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>11.0</basic-object>
                <basic-object name=&quot;width&quot;>100.0</basic-object>
              </compound-key>
              <basic-key name=&quot;alignment&quot;>left</basic-key>
            </style>
            <template references=&quot;label&quot;>
              <basic-object name=&quot;content&quot;>group total:</basic-object>
            </template>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>-50.0</basic-object>
                <basic-object name=&quot;y&quot;>5.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>9.0</basic-object>
                <basic-object name=&quot;width&quot;>-30.0</basic-object>
              </compound-key>
              <basic-key name=&quot;alignment&quot;>center</basic-key>
              <basic-key name=&quot;font-size&quot;>9</basic-key>
            </style>
            <template references=&quot;number-field&quot;>
              <basic-object name=&quot;nullvalue&quot;>-</basic-object>
              <basic-object name=&quot;format&quot;>#0</basic-object>
              <basic-object name=&quot;field&quot;>groupcount</basic-object>
            </template>
          </element>

          <element type=&quot;text/plain&quot;>
            <style>
              <compound-key name=&quot;absolute_pos&quot;>
                <basic-object name=&quot;x&quot;>-80.0</basic-object>
                <basic-object name=&quot;y&quot;>5.0</basic-object>
              </compound-key>
              <compound-key name=&quot;min-size&quot;>
                <basic-object name=&quot;height&quot;>9.0</basic-object>
                <basic-object name=&quot;width&quot;>-20.0</basic-object>
              </compound-key>
              <basic-key name=&quot;font-size&quot;>9</basic-key>
              <basic-key name=&quot;alignment&quot;>right</basic-key>
            </style>
            <template references=&quot;number-field&quot;>
              <basic-object name=&quot;nullvalue&quot;>-</basic-object>
              <basic-object name=&quot;format&quot;>#,##0</basic-object>
              <basic-object name=&quot;field&quot;>groupsum</basic-object>
            </template>
          </element>
        </group-footer>
      </group>
    </groups>

    <!-- ============= -->
    <!-- = item band = -->
    <!-- ============= -->
    <itemband>

      <style>
        <compound-key name=&quot;min-size&quot;>
          <basic-object name=&quot;height&quot;>26.0</basic-object>
          <basic-object name=&quot;width&quot;>0.0</basic-object>
        </compound-key>
      </style>

      <default-style>
        <basic-key name=&quot;font&quot;>stsong-light</basic-key>
        <basic-key name=&quot;font-encoding&quot;>unigb-ucs2-h</basic-key>
        <basic-key name=&quot;font-size&quot;>10</basic-key>
        <basic-key name=&quot;valignment&quot;>bottom</basic-key>
      </default-style>

      <element type=&quot;shape/generic&quot;>
        <style>
          <extends name=&quot;medium-line&quot;/>
          <basic-key name=&quot;stroke&quot; class=&quot;java.awt.basicstroke&quot;>0.1</basic-key>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>25.0</basic-object>
          </compound-key>
          <basic-key name=&quot;paint&quot;>#dfdfdf</basic-key>
        </style>
        <template references=&quot;horizontal-line&quot;/>
      </element>

      <!-- element to display the name from the tablemodel -->
      <element type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>0.0</basic-object>
            <basic-object name=&quot;y&quot;>8.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>10.0</basic-object>
            <basic-object name=&quot;width&quot;>-50.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>left</basic-key>
        </style>
        <template references=&quot;string-field&quot;>
          <basic-object name=&quot;nullvalue&quot;>-</basic-object>
          <basic-object name=&quot;field&quot;>name</basic-object>
        </template>
      </element>

      <!-- element to display icon from the tablemodel -->
      <element type=&quot;image/generic&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>-50.0</basic-object>
            <basic-object name=&quot;y&quot;>1.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>24.0</basic-object>
            <basic-object name=&quot;width&quot;>-30.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>center</basic-key>
          <basic-key name=&quot;valignment&quot;>middle</basic-key>
        </style>
        <template references=&quot;image-field&quot;>
          <basic-object name=&quot;field&quot;>icon</basic-object>
        </template>
      </element>

      <!-- element to display the file size from the tablemodel -->
      <element type=&quot;text/plain&quot;>
        <style>
          <compound-key name=&quot;absolute_pos&quot;>
            <basic-object name=&quot;x&quot;>-80.0</basic-object>
            <basic-object name=&quot;y&quot;>8.0</basic-object>
          </compound-key>
          <compound-key name=&quot;min-size&quot;>
            <basic-object name=&quot;height&quot;>10.0</basic-object>
            <basic-object name=&quot;width&quot;>-20.0</basic-object>
          </compound-key>
          <basic-key name=&quot;alignment&quot;>right</basic-key>
        </style>
        <template references=&quot;number-field&quot;>
          <basic-object name=&quot;nullvalue&quot;>-</basic-object>
          <basic-object name=&quot;format&quot;>#,##0</basic-object>
          <basic-object name=&quot;field&quot;>size</basic-object>
        </template>
      </element>
    </itemband>
  </report-description>

  <!-- ************* -->
  <!-- * functions * -->
  <!-- ************* -->
  <functions>
    <property-ref name=&quot;report.date&quot;/>

    <function name=&quot;pagenumber&quot; class=&quot;org.jfree.report.function.pagefunction&quot;/>
    <function name=&quot;groupcount&quot; class=&quot;org.jfree.report.function.itemcountfunction&quot;>
      <properties>
        <property name=&quot;field&quot;>name</property>
        <property name=&quot;group&quot;>category</property>
      </properties>
    </function>

    <function name=&quot;groupsum&quot; class=&quot;org.jfree.report.function.itemsumfunction&quot;>
      <properties>
        <property name=&quot;field&quot;>size</property>
        <property name=&quot;group&quot;>category</property>
      </properties>
    </function>

    <function name=&quot;overallcount&quot; class=&quot;org.jfree.report.function.itemcountfunction&quot;>
      <properties>
        <property name=&quot;field&quot;>name</property>
      </properties>
    </function>

    <function name=&quot;overallsum&quot; class=&quot;org.jfree.report.function.itemsumfunction&quot;>
      <properties>
        <property name=&quot;field&quot;>size</property>
      </properties>
    </function>
  </functions>

</report-definition>



4. 总结
这是自认为目前最佳的web应用的打印解决方案,虽然仅使用jfreereport产生pdf文件的功能,但是jfreereport提供的页面自动布置的功能使产生报表的工作较之使用itext直接产生要方便许多。以上的例子理论上可以在任何的符合j2ee 1.3规范(或部分符合,如tomcat 4.x)的应用服务器上运行,我仅仅在was 5.0.x上进行了测试。仅供大家参考。

参考
jfreereport 0.8.3 reference guide -  http://www.object-refinery.com/ jfreereport/jfreereport-0.8.3-a4.pdf
java实现pdf报表 - http://blog.csdn.net/legendinfo/articles/176184.aspx





 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons