XSL-FOは、XMLを使った文書整形のための規格。
LaTeXのXML版とでも言えばわかりやすいだろうか。
ある程度、型の決まった文章を記述する場合には、非常に便利なものである。
XMLとXSLTを組み合わせるとデータとデザインを分離させることができ、さらに便利になる。
逆に、一度きりしか使わないような印刷物を記述する場合には、SVGのほうが便利かも。
(SVGの印刷をするならApache Batikがオススメ。
Apache Batikを使うと特に日本語フォントの設定をしなくても日本語の表示ができた。)
SUNのJAVAページからjdk-1_5_0_07-linux-i586.bin(現在の最新版)をダウンロードしてくる。
/usr/local/ で実行する。
できたディレクトリに /usr/local/java という名前でシンボリックリンクを作成しておく。
/usr/local/java/jre/lib/fonts/ に fallback という名前でディレクトリを作成し、
そのディレクトリに中にTrueTypeフォントへのシンボリックリンクを作成する。
.bash_profile などで、
export JAVA_HOME=/usr/local/java
export PATH=$PATH:/usr/local/java/bin
Apache FOPのページからfop-0.92beta-bin-jdk1.4.tar.gz(現在の最新版)をダウンロードしてくる。
/usr/local/ で展開する。
できたディレクトリに /usr/local/fop という名前でシンボリックリンクを作成しておく。
Linuxの場合
/usr/local/java/bin/java -cp \
build/fop.jar:lib/avalon-framework-4.2.0.jar:lib/commons-logging-1.0.4.jar:lib/commons-io-1.1.jar \
org.apache.fop.fonts.apps.TTFReader \
/usr/share/fonts/truetype/sazanami/sazanami-gothic.ttf sazanami-gothic.xml
/usr/local/java/bin/java -cp \
build/fop.jar:lib/avalon-framework-4.2.0.jar:lib/commons-logging-1.0.4.jar:lib/commons-io-1.1.jar \
org.apache.fop.fonts.apps.TTFReader \
/usr/share/fonts/truetype/sazanami/sazanami-mincho.ttf sazanami-mincho.xml
Windowsの場合
java -cp build\fop.jar;lib\avalon-framework-4.2.0.jar;lib\commons-logging-1.0.4.jar;lib\commons-io-1.1.jar
org.apache.fop.fonts.apps.TTFReader -ttcname "MS Gothic"
C:\WINDOWS\Fonts\msgothic.ttc msgothic.xml
java -cp build\fop.jar;lib\avalon-framework-4.2.0.jar;lib\commons-logging-1.0.4.jar;lib\commons-io-1.1.jar
org.apache.fop.fonts.apps.TTFReader -ttcname "MS Mincho"
C:\WINDOWS\Fonts\msmincho.ttc msmincho.xml
conf/fop.xconfを参考にして、↓のようにフォントの設定を追加します。
Linuxの場合
<fonts>
<font metrics-url="file:///usr/local/fop/conf/sazanami-gothic.xml"
kerning="yes" embed-url="file:///usr/share/fonts/truetype/sazanami/sazanami-gothic.ttf">
<font-triplet name="Gothic" style="normal" weight="normal"/>
<font-triplet name="Gothic" style="normal" weight="bold"/>
<font-triplet name="Gothic" style="italic" weight="normal"/>
<font-triplet name="Gothic" style="italic" weight="bold"/>
</font>
<font metrics-url="file:///usr/local/fop/conf/sazanami-mincho.xml"
kerning="yes" embed-url="file:///usr/share/fonts/truetype/sazanami/sazanami-mincho.ttf">
<font-triplet name="Mincho" style="normal" weight="normal"/>
<font-triplet name="Mincho" style="normal" weight="bold"/>
<font-triplet name="Mincho" style="italic" weight="normal"/>
<font-triplet name="Mincho" style="italic" weight="bold"/>
</font>
</fonts>
Windowsの場合
<fonts>
<font metrics-url="file:///C:/app/fop-0.92beta/conf/msgothic.xml"
kerning="yes" embed-url="file:///C:/WINDOWS/Fonts/msgothic.ttc">
<font-triplet name="Gothic" style="normal" weight="normal"/>
<font-triplet name="Gothic" style="normal" weight="bold"/>
<font-triplet name="Gothic" style="italic" weight="normal"/>
<font-triplet name="Gothic" style="italic" weight="bold"/>
</font>
<font metrics-url="file:///C:/app/fop-0.92beta/conf/msmincho.xml"
kerning="yes" embed-url="file:///C:/WINDOWS/Fonts/msmincho.ttc">
<font-triplet name="Mincho" style="normal" weight="normal"/>
<font-triplet name="Mincho" style="normal" weight="bold"/>
<font-triplet name="Mincho" style="italic" weight="normal"/>
<font-triplet name="Mincho" style="italic" weight="bold"/>
</font>
</fonts>
基本的なXSL-FOフォーマットの記述です。これをベースに内容を書き込んでいきましょう!
<?xml version="1.0" encoding="UTF-8" ?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" language="ja">
<fo:layout-master-set>
<fo:simple-page-master
page-height="297mm" page-width="210mm"
margin-top="20mm" margin-bottom="20mm"
margin-left="20mm" margin-right="20mm"
master-name="Pagemaster-1" >
<fo:region-body
margin-top="5mm" margin-bottom="5mm"
margin-left="5mm" margin-right="5mm" />
<fo:region-before extent="10mm" />
<fo:region-after extent="10mm" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="Pagemaster-1" >
<fo:static-content flow-name="xsl-region-before">
<fo:block>- XSL-FO -</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block><fo:page-number /></fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block font-family="Mincho" font-size="18pt">XSL-FOのテスト。</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
fop-0.92beta$ ./fop -c XXXX.conf XXXX.fo XXXX.pdf
XXXX.conf : 設定ファイル
XXXX.fo : 入力FOファイル
XXXX.pdf : 出力PDFファイル
[ RETURN ]