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" 如下代码,分别是字体原路径和目标文件路径
点击(此处)折叠或打开
- import org.apache.fop.fonts.apps.TTFReader;
- public class Fonts {
- public static void main(String args[]){
- String[] parameters = {
- "-ttcname",
- "simkai",
- "C:\\Windows\\Fonts\\simkai.ttf", "E:simkai.xml", };
- TTFReader.main(parameters);
- }
- }
三:将字体登记
建立一个fop.xml文件,代码如下
点击(此处)折叠或打开
- <?xml version="1.0"?>
- <fop version="1.0">
- <!-- Base URL for resolving relative URLs -->
- <base>.</base>
-
- <!-- Source resolution in dpi (dots/pixels per inch) for determining the size of pixels in SVG and bitmap images, default: 72dpi -->
- <source-resolution>72</source-resolution>
- <!-- Target resolution in dpi (dots/pixels per inch) for specifying the target resolution for generated bitmaps, default: 72dpi -->
- <target-resolution>72</target-resolution>
-
- <!-- Default page-height and page-width, in case value is specified as auto -->
- <default-page-settings height="29.7cm" width="21cm"/>
-
- <!-- Information for specific renderers -->
- <!-- Uses renderer mime type for renderers -->
- <renderers>
- <renderer mime="application/pdf">
- <filterList>
- <!-- provides compression using zlib flate (default is on) -->
- <value>flate</value>
- </filterList>
- <fonts>
- <!-- embedded fonts -->
- <!--
- This information must exactly match the font specified
- in the fo file. Otherwise it will use a default font.
- For example,
- <fo:inline font-family="Arial" font-weight="bold" font-style="normal">
- Arial-normal-normal font
- </fo:inline>
- for the font triplet specified by:
- <font-triplet name="Arial" style="normal" weight="bold"/>
- If you do not want to embed the font in the pdf document
- then do not include the "embed-url" attribute.
- The font will be needed where the document is viewed
- for it to be displayed properly.
- possible styles: normal | italic | oblique | backslant
- possible weights: normal | bold | 100 | 200 | 300 | 400
- | 500 | 600 | 700 | 800 | 900
- (normal = 400, bold = 700)
- -->
-
- <font metrics-url="conf/fonts/arial.xml" kerning="yes" embed-url="conf/fonts/arial.ttf">
- <font-triplet name="Arial" style="normal" weight="normal"/>
- <font-triplet name="Arial" style="italic" weight="normal"/>
- </font>
- <font metrics-url="conf/fonts/arialb.xml" kerning="yes" embed-url="conf/fonts/arialb.ttf">
- <font-triplet name="Arial" style="normal" weight="bold"/>
- <font-triplet name="Arial" style="italic" weight="bold"/>
- </font>
- <font metrics-url="conf/fonts/SimHei.xml" kerning="yes" embed-url="conf/fonts/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>
- <font metrics-url="conf/fonts/SimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
- <font-triplet name="SimSun" style="normal" weight="normal" />
- <font-triplet name="SimSun" style="normal" weight="bold" />
- <font-triplet name="SimSun" style="italic" weight="normal" />
- <font-triplet name="SimSun" style="italic" weight="bold" />
- </font>
- <!--新宋体//-->
- <font metrics-url="conf/fonts/NSimSun.xml" kerning="yes" embed-url="conf/fonts/SimSun.ttc">
- <font-triplet name="NSimSun" style="normal" weight="normal" />
- <font-triplet name="NSimSun" style="normal" weight="bold" />
- <font-triplet name="NSimSun" style="italic" weight="normal" />
- <font-triplet name="NSimSun" style="italic" weight="bold" />
- </font>
- <!--宋楷//-->
- <font metrics-url="conf/fonts/simkai.xml" kerning="yes" embed-url="conf/fonts/SIMKAI.ttf">
- <font-triplet name="simkai" style="normal" weight="normal" />
- <font-triplet name="simkai" style="normal" weight="bold" />
- <font-triplet name="simkai" style="italic" weight="normal" />
- <font-triplet name="simkai" style="italic" weight="bold" />
- </font>
- <font metrics-url="conf/fonts/Code39Seven.xml" kerning="yes" embed-url="conf/fonts/Code39Seven.ttf">
- <font-triplet name="Code39Seven" style="normal" weight="normal" />
- </font>
- </fonts>
- <!-- This option lets you specify additional options on an XML handler -->
- <!--xml-handler namespace="http://www.w3.org/2000/svg">
- <stroke-text>false</stroke-text>
- </xml-handler-->
- </renderer>
- <renderer mime="application/postscript">
- <!-- This option forces the PS renderer to rotate landscape pages -->
- <!--auto-rotate-landscape>true</auto-rotate-landscape-->
-
- <!-- This option lets you specify additional options on an XML handler -->
- <!--xml-handler namespace="http://www.w3.org/2000/svg">
- <stroke-text>false</stroke-text>
- </xml-handler-->
- </renderer>
- <renderer mime="image/png">
- <!--transparent-page-background>true</transparent-page-background-->
- </renderer>
- <renderer mime="image/tiff">
- <!--transparent-page-background>true</transparent-page-background-->
- <!--compression>CCITT T.6</compression-->
- </renderer>
- <renderer mime="text/xml">
- </renderer>
- <!-- RTF does not have a renderer
- <renderer mime="text/rtf">
- </renderer>
- -->
- </renderers>
- </fop>

四:在xsl文件中使用
点击(此处)折叠或打开
- <fo:block font-size="24pt" font-weight="bold" line-height="20mm" vertical-align="top"
- text-align-last="center" space-before.optimum="12pt" font-family="simkai">
- <xsl:value-of select="Title" />
- </fo:block >