解决jpivot1.8.0输出PDF表格不支持显示中文字符的问题

目前(2011年8月)JPivot的最新版本1.8.0,在PDF格式输出的表格中还不支持中文字体,需要手工添加中文字库。

1. 查看JPivot源码(1.6.0以上)com.tonbeller.jpivot.print.PrintServlet

有一个init()方法

 

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try {
      // set base FOP FONT directory.  The font config  stuff will be looked for here
      Configuration.put("fontBaseDir", config.getServletContext().getRealPath("/WEB-INF/jpivot/print/"));
      // get the physical path for the config file
      String fopConfigPath = config.getServletContext().getRealPath("/WEB-INF/jpivot/print/userconfig.xml");
      // load the user proerties, contining the CustomFont font.
      new Options(new File(fopConfigPath));

    } catch (FOPException e) {
      e.printStackTrace();
      logger.info("FOP user config file not loaded");
    } catch (Exception e) {
      e.printStackTrace();
      logger.info("FOP user config file not loaded");
    }
  }
 

 

可以看到当打印PDF文档时,系统会尝试去fopConfigPath下加载自定义的font配置文件userconfig.xml
2. OK,我们现在就去添加这么一个FONT configuration,内容为JPivot中定义的Italic/ normal 与中文字体SimHei(黑体)映射关系:

   【userconfig.xml】

<configuration>
  <fonts>  
    <font metrics-file="simhei.xml" kerning="yes" embed-file="simhei.ttf">  
      <font-triplet name="SimHei" style="normal" weight="normal"/>
      <font-triplet name="SimHei" style="normal" weight="bold"/>  
      <font-triplet name="SimHei" style="italic" weight="normal"/>  
      <font-triplet name="SimHei" style="italic" weight="bold"/>  
    </font>  
  </fonts>  
</configuration>
 

3. 使用Apache FOP(lastest stable version is 1.0.0) 转换windows系统自带的字体库simhei.ttf为xml文件(simhei.xml)

具体方法:

java org.apache.fop.fonts.apps.TTFReader -ttcname SimHei C:\WINDOWS\Fonts\simhei.ttf simhei.xml

运行成功后生成simhei.xml

将simhei.tff和simhei.xml复制到/WEB-INF/jpivot/print目录下。


4. 编辑/WEB-INF/jpivot/table/fomdxtable.xsl

将SimHei添加为所有font-family的首选值,OK,至此问题搞定!


致谢 参考文档:http://www.iteye.com/topic/80801

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值