FOP (PDF生成) メモ

目的

* FOP で XML+XSL から PDF 作成。日本語も使えるように。

インストールしたもの

* fop-0.92beta-bin-jdk1.4.zip Apche FOP 公式HP

設定

* fop-0.92beta-bin-jdk1.4.zipを解凍して、c:/win32app/fop/ フォルダに置く。
* windows環境 Path に c:/win32app/fop/ を追加(regtool使用)。
* JAVA(ver.1.4)をfopは要求してるけど、1.5でも動いた。
* 日本語フォント用XMLを C:/win32app/fop/conf/ フォルダに作成。
* 環境設定ファイル C:/win32app/fop/conf/fop.xconf のフォント情報を修正。

Javaの位置・バージョン確認

$ which java
/cygdrive/c/WINNT/system32/java

$ java -version
java version “1.5.0_06”
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

Windows環境変数 パス(Path) 設定(reg_path.sh)

#! /usr/bin/sh

regtool -s set ‘\machine\System\CurrentControlSet\Control\Session Manager\Environment\Path’ \
“\
c:\\cygwin\\usr\\local\\bin;\
c:\\cygwin\\usr\\local\\sbin;\
(中略)
c:\\win32app\\fop;\
(中略)
c:\\win32app\\Maxima-5.9.1\\bin;”

日本語フォント準備(mkjfont.bat)

日本語のフォントメトリックファイルなるものを作成。

* c:/win32app/fop/conf/ フォルダでバッチを実行。
* windows標準の MS明朝、MSゴシックフォント から 2ファイル 「msmincho.xml」、「msgothic.xml」を作成。

set LOCAL_FOP_HOME=c:\win32app\fop
set LIBDIR=%LOCAL_FOP_HOME%\lib

set LOCALCLASSPATH=%LOCAL_FOP_HOME%\build\fop.jar

set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\avalon-framework-4.2.0.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\commons-logging-1.0.4.jar
set LOCALCLASSPATH=%LOCALCLASSPATH%;%LIBDIR%\commons-io-1.1.jar

SET EXEC=org.apache.fop.fonts.apps.TTFReader

set FONT=-ttcname “MS Mincho” c:/winnt/fonts/msmincho.ttc msmincho.xml
java -cp “%LOCALCLASSPATH%” %EXEC% %FONT%

set FONT=-ttcname “MS Gothic” c:/winnt/fonts/msgothic.ttc msgothic.xml
java -cp “%LOCALCLASSPATH%” %EXEC% %FONT%

c:/win32app/fop/conf/fop.xconf の修正

fontsタグ内に 下記 font 情報追加。

<font metrics-url=”file:///c:/win32app/fop/conf/msmincho.xml” kerning=”yes”
embed-url=”file:///c:/winnt/fonts/msmincho.ttc”>
<font-triplet name=”MSMincho” style=”normal” weight=”normal”/>
<font-triplet name=”MSMincho” style=”normal” weight=”bold”/>
</font>
<font metrics-url=”file:///c:/win32app/fop/conf/msgothic.xml” kerning=”yes”
embed-url=”file:///c:/winnt/fonts/msgothic.ttc”>
<font-triplet name=”MSGothic” style=”normal” weight=”normal”/>
<font-triplet name=”MSGothic” style=”normal” weight=”bold”/>
</font>

テスト用XMLファイル(hello.xml)

<?xml version=”1.0″ encoding=”Shift_JIS”?>
<text>
Hello, 漢字 FOP!
</text>

テスト用XSLファイル(hello.xsl)

<?xml version=”1.0″ encoding=”Shift_JIS”?>
<xsl:stylesheet
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”
version=”1.0″>
<xsl:template match=”text”>
<fo:root xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<fo:layout-master-set>
<fo:simple-page-master master-name=”simple”>
<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” text-align=”center”
font-family=”MSMincho”>
<xsl:value-of select=”.”/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>

変換

XML を 2段階で PDF に変換。最初に XML+XSL を XSL-FO に変換。次に生成された FO を PDF に変換。

* XML ⇒ XSL-FO

fop.bat -c C:\win32app\fop\conf\fop.xconf -xml hello.xml -xsl hello.xsl -foout hello.fo

* XSL-FO ⇒ PDF

fop.bat -c C:\win32app\fop\conf\fop.xconf hello.fo -pdf hello.pdf

覚書き

* fop/docs/0.92/fonts.html にフォントの設定方法が書かれてる。
* 「font-triplet」タグは fop.bat のエラーを頼りに必要なものを追加しながら作成。

エラーの場合・・・ 警告: Font ‘MSMincho,normal,400’ not found. Substituting with default font.
成功の場合 ・・・ 情報: MS Mincho <– selected

* ‘MSMincho,normal,400’

<font-triplet name=”MSMincho” style=”normal” weight=”normal”/>

* fop.xconf内のコメント

possible weights: normal | bold | 100 | 200 | 300 | 400
| 500 | 600 | 700 | 800 | 900
(normal = 400, bold = 700)

コメントする