fop生成pdf的中文乱码问题

1.建立font metrics文件,对ttc或ttf有不同的方法。


确定系统字体文件夹下的字体文件(微软雅黑字体的路径为C:\WINDOWS\Fonts\msyh.ttf)。


然后在命令提示符下执行如下命令:



TTF字体文件


java -cpc:\fop\build\fop.jar;c:\fop\lib\avalon-framework-4.2.0.jar;c:\fop\lib\commons-logging-1.0.4.jar;c:\fop\lib\commons-io-1.3.1.jar;c:\fop\lib\xmlgraphics-commons-1.4.jarorg.apache.fop.fonts.apps.TTFReader c:\windows\fonts\msyh.ttfmsyh.xml



TTC字体文件


java -cpc:\fop\build\fop.jar;c:\fop\lib\avalon-framework-4.2.0.jar;c:\fop\lib\commons-logging-1.0.4.jar;c:\fop\lib\commons-io-1.3.1.jar;c:\fop\lib\xmlgraphics-commons-1.4.jarorg.apache.fop.fonts.apps.TTFReader -ttcname "SimSun"c:\windows\fonts\simsun.ttc simsun.xml

  最近在做fop生成pdf的相关内容,出现了生成中文乱码的问题,在网上找了相关资料,终于解决了问题,在这里写出来和大家分享。
 
     错误就不展示了。解决办法
     
     一:中文字体包。  
    中文字体包一般以ttc或ttf作为后缀,如simkai.ttc楷体。可以到网上下载,也可以在本机C:\Windows\Fonts下查找。

    二:生成字体矩阵xml文件
    cid-fonts.pdf中给出了使用命令行的办法,觉得麻烦,再这里决定使用java程序生成,如下代码,其中"C:\\Windows\\Fonts\\simkai.ttf", "E:simkai.xml" 如下代码,分别是字体原路径和目标文件路径

点击(此处)折叠或打开

  1. import org.apache.fop.fonts.apps.TTFReader;


  2. public class Fonts {
  3.     public static void main(String args[]){
  4.         String[] parameters = {
  5.         "-ttcname",
  6.         "simkai",
  7.         "C:\\Windows\\Fonts\\simkai.ttf", "E:simkai.xml", };
  8.         TTFReader.main(parameters);
  9.         }

  10. }
   运行上述代码后将会在e盘看到simkai.xml文件
    三:将字体登记
        建立一个fop.xml文件,代码如下
     

点击(此处)折叠或打开

  1. <?xml version="1.0"?>
  2. <fop version="1.0">

  3.   <!-- Base URL for resolving relative URLs -->
  4.   <base>.</base>
  5.   
  6.   <!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi -->
  7.   <source-resolution>72</source-resolution>
  8.   <!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi -->
  9.   <target-resolution>72</target-resolution>
  10.   
  11.   <!-- Default page-height and page-width, in case value is specified as auto -->
  12.   <default-page-settings height="29.7cm" width="21cm"/>
  13.   
  14.   <!-- Information for specific renderers -->
  15.   <!-- Uses renderer mime type for renderers -->
  16.   <renderers>
  17.     <renderer mime="application/pdf">
  18.       <filterList>
  19.         <!-- provides compression using zlib flate (default is on) -->
  20.         <value>flate</value>
  21.       </filterList>

  22.       <fonts>
  23.         <!-- embedded fonts -->
  24.         <!--
  25.         This information must exactly match the font specified
  26.         in the fo file. Otherwise it will use a default font.

  27.         For example,
  28.         <fo:inline font-family="Arial" font-weight="bold" font-style="normal">
  29.             Arial-normal-normal font
  30.         </fo:inline>
  31.         for the font triplet specified by:
  32.         <font-triplet name="Arial" style="normal" weight="bold"/>

  33.         If you do not want to embed the font in the pdf document
  34.         then do not include the "embed-url" attribute.
  35.         The font will be needed where the document is viewed
  36.         for it to be displayed properly.

  37.         possible styles: normal | italic | oblique | backslant
  38.         possible weights: normal | bold | 100 | 200 | 300 | 400
  39.                           | 500 | 600 | 700 | 800 | 900
  40.         (normal = 400, bold = 700)
  41.         -->
  42.         
  43.         <font metrics-url="conf/fonts/arial.xml" kerning="yes" embed-url="conf/fonts/arial.ttf">
  44.           <font-triplet name="Arial" style="normal" weight="normal"/>
  45.           <font-triplet name="Arial" style="italic" weight="normal"/>
  46.         </font>
  47.         <font metrics-url="conf/fonts/arialb.xml" kerning="yes" embed-url="conf/fonts/arialb.ttf">
  48.           <font-triplet name="Arial" style="normal" weight="bold"/>
  49.           <font-triplet name="Arial" style="italic" weight="bold"/>
  50.         </font>
  51.         <font metrics-url="conf/fonts/SimHei.xml" kerning="yes" embed-url="conf/fonts/SimHei.ttf">
  52.           <font-triplet name="SimHei" style="normal" weight="normal"/>
  53.           <font-triplet name="SimHei" style="normal" weight="bold"/>
  54.           <font-triplet name="SimHei" style="italic" weight="normal"/>
  55.           <font-triplet name="SimHei" style="italic" weight="bold"/>
  56.         </font>
  57.         <font metrics-url="conf/fonts/SimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
  58.          <font-triplet name="SimSun" style="normal" weight="normal" />
  59.          <font-triplet name="SimSun" style="normal" weight="bold" />
  60.          <font-triplet name="SimSun" style="italic" weight="normal" />
  61.          <font-triplet name="SimSun" style="italic" weight="bold" />
  62.         </font>
  63.         <!--新宋体//-->
  64.         <font metrics-url="conf/fonts/NSimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
  65.          <font-triplet name="NSimSun" style="normal" weight="normal" />
  66.          <font-triplet name="NSimSun" style="normal" weight="bold" />
  67.          <font-triplet name="NSimSun" style="italic" weight="normal" />
  68.          <font-triplet name="NSimSun" style="italic" weight="bold" />
  69.         </font>
  70.         <!--宋楷//-->
  71.         <font metrics-url="conf/fonts/simkai.xml" kerning="yes" embed-url="conf/fonts/SIMKAI.ttf">
  72.          <font-triplet name="simkai" style="normal" weight="normal" />
  73.          <font-triplet name="simkai" style="normal" weight="bold" />
  74.          <font-triplet name="simkai" style="italic" weight="normal" />
  75.          <font-triplet name="simkai" style="italic" weight="bold" />
  76.         </font>
  77.         <font metrics-url="conf/fonts/Code39Seven.xml" kerning="yes" embed-url="conf/fonts/Code39Seven.ttf">
  78.          <font-triplet name="Code39Seven" style="normal" weight="normal" />
  79.         </font>
  80.       </fonts>

  81.       <!-- This option lets you specify additional options on an XML handler -->
  82.       <!--xml-handler namespace="http://www.w3.org/2000/svg">
  83.         <stroke-text>false</stroke-text>
  84.       </xml-handler-->

  85.     </renderer>

  86.     <renderer mime="application/postscript">
  87.       <!-- This option forces the PS renderer to rotate landscape pages -->
  88.       <!--auto-rotate-landscape>true</auto-rotate-landscape-->
  89.       
  90.       <!-- This option lets you specify additional options on an XML handler -->
  91.       <!--xml-handler namespace="http://www.w3.org/2000/svg">
  92.         <stroke-text>false</stroke-text>
  93.       </xml-handler-->
  94.     </renderer>

  95.     <renderer mime="image/png">
  96.       <!--transparent-page-background>true</transparent-page-background-->
  97.     </renderer>

  98.     <renderer mime="image/tiff">
  99.       <!--transparent-page-background>true</transparent-page-background-->
  100.       <!--compression>CCITT T.6</compression-->
  101.     </renderer>

  102.     <renderer mime="text/xml">
  103.     </renderer>

  104.     <!-- RTF does not have a renderer
  105.     <renderer mime="text/rtf">
  106.     </renderer>
  107.     -->

  108.   </renderers>

  109. </fop>
       类结构图:
          

    四:在xsl文件中使用

点击(此处)折叠或打开

  1. <fo:block font-size="24pt" font-weight="bold" line-height="20mm" vertical-align="top"
  2.             text-align-last="center" space-before.optimum="12pt" font-family="simkai">
  3.             <xsl:value-of select="Title" />
  4.         </fo:block >

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值