Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66636
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-19 22:12
个人简介

hello、、、、

文章分类

全部博文(17)

分类: Java

2013-11-07 20:56:36

    

         最近在做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="">
  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="">
  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 >

      运行后效果
        


     
阅读(3479) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~