解决jpivot1.8.0输出PDF表格不支持显示中文字符的有关问题
解决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