FOP 是 James Tauber 發起的開放原始碼專案,最初目標是用 XSL-FO 把 XML 轉成 PDF。現階段它已能輸出 PDF、MIF、PCL、TXT 等多種格式,可直接送印,並支援以 SVG 描述圖形。XML 是最合適的內容儲存格式,PDF 是最普及的內容載體格式,FOP 正好站在這兩者的交會點上。雖然 XSL-FO 規範本身推進得相當緩慢,但能讓自己的程式直接吐出 PDF,這件事本身就夠誘人。本文示範如何在 Java 程式中嵌入 FOP,並完整處理中文顯示這個實務難題。
FOP 提供命令列、程式嵌入、XT 嵌入三種使用方式,本文聚焦在程式嵌入。先釐清一個觀念:把 XML 轉成 PDF 其實是兩步——第一步用 XSLT 把 XML 轉成 XSL-FO,第二步把 XSL-FO 轉成 PDF。XSLT 與 XSL-FO 的語法已有大量文件可查,這裡只談第二步的程式撰寫。
先看一個最簡單的 FO 文件,它會輸出兩段文字:
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="https://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-name="simple">
<fo:flow flow-name="xsl-region-body">
<!-- Title -->
<fo:block font-size="18pt"
font-family="sans-serif"
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="sans-serif"
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>
具體的 FO 語法請自行參考規格文件,這裡的重點是接下來的程式碼。
FOP 對外提供的介面都在 org.apache.fop.apps 下:AWTStarter 是以 AWT 寫的結果預覽程式,CommandLineStarter 提供命令列模式,PrintStarter 負責列印,XTDriver 是 XT 嵌入介面,而 Driver 就是我們要在程式中使用的類別。下面的 foptest.java 示範最常見的用法:
import java.io.*;
import org.xml.sax.InputSource;
import org.apache.fop.apps.*;
public class foptest {
public static void main(String[] args) {
try {
Driver driver = new Driver();
// 設定要轉換的 fo 檔
driver.setInputSource(new InputSource(args[0]));
// 設定輸出檔
driver.setOutputStream(new FileOutputStream(args[1]));
// 設定轉換類型,亦可為
// RENDER_PCL, RENDER_PS, RENDER_TXT, RENDER_MIF
driver.setRenderer(Driver.RENDER_PDF);
// 開始轉換
driver.run();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
程式接受兩個命令列參數:第一個是輸入的 FO 檔名,第二個是輸出的檔名。執行前請把下列 jar 加入 CLASSPATH:
{FOP 安裝目錄}\build\fop.jar
{FOP 安裝目錄}\lib\batik.jar
{FOP 安裝目錄}\lib\xalan-2.0.0.jar
{FOP 安裝目錄}\lib\xerces-1.2.3.jar
{FOP 安裝目錄}\lib\avalon-framework-4.0.jar
{FOP 安裝目錄}\lib\logkit-1.0b4.jar
{FOP 安裝目錄}\lib\jimi-1.0.jar
接著執行:
java foptest simple.fo simple.pdf
產生的 simple.pdf 如下:

上面這段程式足以應付絕大多數的轉換需求,但對中文使用者來說,一定會遇到中文變成亂碼的問題。把 simple.fo 的標題從「FOP 0.20.2」改成「支援中文的 FOP 0.20.2」,在不改程式的情況下執行:

可以看到漢字全被顯示成 # 符號,原因就是缺少對應的字型設定。
東方字元在 FOP 早期版本中並不受支援。最早嘗試打補丁的解法出自日本社群,從 0.16 版起他們在 sourceforge 上建立了 jpfop 專案來解決日文顯示問題,同一套方法也適用於中文。好消息是現階段的 FOP 已能妥善處理中文,不必再自行修改原始碼。設定步驟如下。
對於 .ttf 的 TrueType 字型,執行:
java org.apache.fop.fonts.apps.TTFReader C:\WINNT\Fonts\simkai.ttf simkai.xml
這裡的 simkai.xml 就是為楷體產生的 font metrics 檔。Windows 的宋體則是 .ttc(TrueType Collection,一個檔案含多個 TrueType),這時得先查出整包裡有哪些字型:
java org.apache.fop.fonts.apps.TTFReader C:\WINNT\Fonts\simsun.ttc simsun.xml
輸出如下:
This is a TrueType collection file with 2 fonts
Containing the following fonts:
SimSun
NSimSun
java.io.IOException: Failed to read font
at org.apache.fop.fonts.TTFFile.readFont(TTFFile.java:388)
at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:181)
at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:143)
後面的例外是因為參數不對(.ttc 並非 .ttf)而丟出的,開發團隊知道這個問題,但大概覺得不痛不癢就沒修。不管怎麼說,我們要的資訊已經拿到了:這包裡有 SimSun 與 NSimSun。接著用 -ttcname 指定要取出哪一個字型:
java org.apache.fop.fonts.apps.TTFReader -ttcname "SimSun" C:\WINNT\Fonts\simsun.ttc simsun.xml
FOP 主目錄的 conf 下有一個 userconfig.xml。為方便起見,把它和上一步產生的 simsun.xml、simkai.xml 都複製到與程式相同的目錄,然後在檔案末尾的 <fonts></fonts> 區塊中加入:
<font metrics-file="simsun.xml" kerning="yes"
embed-file="c:\WINNT\fonts\simsun.ttc">
<font-triplet name="mysimsun" style="normal" weight="normal"/>
</font>
<font metrics-file="simkai.xml" kerning="yes"
embed-file="c:\WINNT\fonts\simkai.ttf">
<font-triplet name="mysimkai" style="normal" weight="normal"/>
</font>
metrics-file 可用相對路徑或絕對路徑(這裡同目錄,寫檔名即可);font-triplet 的 name 可以自行命名,不要求與字型原名相同。一旦設定了這個名字,FO 檔中就只能用這個名字來引用該字型。
示範用的 simplecn.fo 如下,注意 font-family 填的是 userconfig.xml 裡設定的名稱:
<?xml version="1.0" encoding="gb2312"?>
<fo:root xmlns:fo="https://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-name="simple">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="18pt"
font-family="mysimsun"
line-height="24pt"
text-align="center"
padding-top="3pt">
這是宋體
</fo:block>
<fo:block font-size="18pt"
font-family="mysimkai"
line-height="24pt"
text-align="center"
padding-top="3pt">
這是楷體
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
最後,因為需要讀入 userconfig.xml 取得字型資訊,程式主體要在 run() 之前多一行:
Driver driver = new Driver();
driver.setInputSource(new InputSource(args[0]));
driver.setOutputStream(new FileOutputStream(args[1]));
driver.setRenderer(Driver.RENDER_PDF);
// 讀入設定(在 Options 的建構子中完成)
Options options = new Options(new File("userconfig.xml"));
driver.run();
產生的 Options 實例後續並不會再用到,它的作用只是在 driver.run() 之前把字型設定載入。執行結果如下:

就技術而言 FOP 無疑是非常優秀的產品,但對開發者的支援文件明顯不足。FOP 團隊也意識到這一點,承諾會建立專屬網站並充實文件。若你現在就需要更省心的方案,也可以評估 RenderX、X2P 等商用產品。
另外提醒版本選擇:今年 8 月推出的 0.20.1 品質不佳,不但部分範例跑不起來,附帶的原始碼也有若干錯誤;9 月 29 日釋出的 0.20.2 RC 改進幅度很大,建議直接採用這個版本。