在FOP元件安裝完成, 可順利產生PDF後, 進一步試著輸出繁體中文的PDF內容時可依下步驟產生。
(FOP元件如未安裝完成, 請參考簡易使用FOP)。
1.取得Windows下字型檔
於C:\WINNT\Fonts下以"內容"方式得知標楷體字型檔檔名為kaiu.ttf。
2.於JBuilder下建立一個Class以產生font metrics檔simhei.xml, Class檔內容如下:
FontInfo.class
import java.io.*; import org.apache.fop.fonts.apps.*; public class FontInfo { public static void main(String[] args) { try { TTFReader ttfr = new TTFReader(); String[] getSimhei = {"C:\\WINNT\\Fonts\\kaiu.ttf", "c:\\simhei.xml"}; ttfr.main(getSimhei); } catch( Exception e ){ e.printStackTrace(); } } } |
建立org.apache.fop.fonts.apps.TTFReader物件,以TTFReader物件中main()
分別傳入標楷體字型檔所在及檔名, font metrics檔輸出至何處及其檔名, 範例執行成功即在C:\可找到simhei.xml。
3.設置標楷體字型檔
於FOP元件中fop-0.20.5\conf下可找到userconfig.xml, 將被註解化部份的內容如:
<font metrics-file="arial.xml" kerning="yes" embed-file="arial.ttf"> <font-triplet name="Arial" style="normal" weight="normal"/> <font-triplet name="ArialMT" style="normal" weight="normal"/> </font> |
改為如下:
<font metrics-file="D:\programming\FOP_exercise\fo\simhei.xml" kerning="yes" embed-file="C:\WINNT\Fonts\kaiu.ttf"> <font-triplet name="mysimsun" style="normal" weight="normal"/> <!--<font-triplet name="ArialMT" style="normal" weight="normal"/>--> </font> |
並將修改完成的userconfig.xml與simhei.xml複製到D:\programming\FOP_exercise\fo\(自定)。
4.修改fo檔
simple.fo檔內容:
<?xml version="1.0" encoding="Big5"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-body margin-top="3cm"/> <fo:region-before extent="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simple"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="18pt" font-family="mysimsun" line-height="24pt" space-after.optimum="15pt" background-color="blue" color="white" text-align="center" padding-top="3pt"> FOP 0.20.2 繁體測試~~ </fo:block> <!-- Normal Text --> <fo:block font-size="12pt" font-family="mysimsun" line-height="15pt" space-after.optimum="3pt" text-align="justify"> FOP is the world's first print formatter driven by XSL formatting objects. It is a Java application that reads a formatting object tree and then turns it into a PDF document. 這是中文測試喔~~~~~ </fo:block> </fo:flow> </fo:page-sequence> </fo:root> |
其中font-family的來源由為原來的sans-serif改被指定為userconfig.xml的mysimsun,
而xml的encoding則改為Big5。
5.修改foptest.class, Class檔內容如下:
foptest.class
import java.io.*; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.apache.fop.apps.*; public class foptest { public static void main(String[] args) { try { Driver driver = new Driver(); //設定要作為輸出參考的fo檔所在及檔名 driver.setInputSource(new InputSource ("D:\\programming\\FOP_exercise\\fo\\simple.fo")); //設定要作為輸出檔案(在此為PDF檔)至何處及其檔名 driver.setOutputStream(new FileOutputStream("D:\\programming\\FOP_exercise\\fo\\simple.pdf")); //設置轉換類型 //還可以為RENDER_PCL,RENDER_PS,RENDER_TXT,RENDER_MIF driver.setRenderer(Driver.RENDER_PDF); //開始轉換 Options options = new Options(new File("D:\\programming\\FOP_exercise\\fo\\userconfig.xml")); //將字體輸出的參考指向userconfig.xml driver.run(); } catch( Exception e ){ e.printStackTrace(); } } } |
主要是多增加了將字體輸出的參考指向userconfig.xml的設定。
6.執行後產生的simple.pdf檔效果如下
資料參考來源:
如何在程式中嵌入FOP
Apache FOP