2009年4月21日火曜日

FOP

http://ja.wikipedia.org/wiki/XSL_Formatting_Objects
http://ja.wikipedia.org/wiki/Apache_FOP
http://nanasi.jp/articles/others/fop.html
http://www.sm.u-tokai.ac.jp/~sugita/xml/japanese.html
http://xmlgraphics.apache.org/fop/

---------------------------------------------

そのままでは日本語が表示できないのでフォント設定を行う

http://www.02.246.ne.jp/~torutk/fop/fop4sdoc.html
http://www.nslabs.jp/apache_fop.rhtml
http://d.hatena.ne.jp/ditahack/20090331/1238513240
http://d.hatena.ne.jp/mitsu48/
http://liosk.blog103.fc2.com/blog-entry-71.html

手順(1) MSゴシック/MS明朝のフォント名を調べる

コマンドプロンプトより

java -cp ../build/fop.jar;../lib/commons-io-1.3.1.jar;../lib/commons-logging-1.0.4.jar org.apache.fop.fonts.apps.TTFReader c:/windows/Fonts/msmincho.ttc msmincho.xml
java -cp ../build/fop.jar;../lib/commons-io-1.3.1.jar;../lib/commons-logging-1.0.4.jar org.apache.fop.fonts.apps.TTFReader c:/windows/Fonts/msgothic.ttc msgothic.xml

すると下記のような形でエラーと共にフォント名(ttcname)を表示してくれる。

D:\DocBook\fop-0.95\JFonts>java -cp ../build/fop.jar;../lib/commons-io-1.3.1.jar;../lib/commons-logging-1.0.4.jar org.apache.fop.fonts.apps.TTFReader c:/windows/Fonts/msmincho.ttc msmincho.xml
TTF Reader for Apache FOP 0.95

Parsing font...
Reading c:/windows/Fonts/msmincho.ttc...
This is a TrueType collection file with 2 fonts
Containing the following fonts:
MS Mincho
MS PMincho
Error while building XML font metrics file.
java.lang.IllegalArgumentException: For TrueType collection you must specify which font to select (-ttcname)


手順(2) フォントメトリクスファイルを作成する

上記の手順で得たフォント名をコマンドラインに追加して実行

java -cp ../build/fop.jar;../lib/commons-io-1.3.1.jar;../lib/commons-logging-1.0.4.jar;../lib/xmlgraphics-commons-1.3.1.jar org.apache.fop.fonts.apps.TTFReader -ttcname "MS Mincho" c:/windows/Fonts/msmincho.ttc msmincho.xml
java -cp ../build/fop.jar;../lib/commons-io-1.3.1.jar;../lib/commons-logging-1.0.4.jar;../lib/xmlgraphics-commons-1.3.1.jar org.apache.fop.fonts.apps.TTFReader -ttcname "MS Gothic" c:/windows/Fonts/msgothic.ttc msgothic.xml

※必要なクラスライブラリが増えている


手順(3) フォントメトリクスファイルを修正する

TTFReader にはバグがあって、msmincho.xml / msgothic.xml ないのフォントファミリ名の部分が文字化けしているのでそれぞれ MS 明朝 MS ゴシック に修正する。


手順(4) ユーザコンフィグファイルの作成

fop の conf フォルダに fop.xconf というファイルがあるが、これはただのサンプルファイル。
これをベースにユーザコンフィグファイル JFConf.xml を作成する(ファイル名はxmlファイルあるなら何でもかまわない)

<?xml version="1.0" ?>
<fop version="1.0">
<renderers>
<renderer mime="application/pdf">
<fonts>
<font metrics-url="D:\DocBook\fop-0.95\JFonts\msmincho.xml" kerning="yes" embed-url="C:\windows\Fonts\msmincho.ttc">
<font-triplet name="MS-Mincho" style="normal" weight="normal"/>
<font-triplet name="MS-Mincho" style="normal" weight="bold"/>
<font-triplet name="MS-Mincho" style="italic" weight="normal"/>
<font-triplet name="MS-Mincho" style="italic" weight="bold"/>
<font-triplet name="MSMincho" style="normal" weight="normal"/>
<font-triplet name="MSMincho" style="normal" weight="bold"/>
<font-triplet name="MSMincho" style="italic" weight="normal"/>
<font-triplet name="MSMincho" style="italic" weight="bold"/>
<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-triplet name="MS明朝" style="normal" weight="normal"/>
<font-triplet name="MS明朝" style="normal" weight="bold"/>
<font-triplet name="MS明朝" style="italic" weight="normal"/>
<font-triplet name="MS明朝" style="italic" weight="bold"/>
<font-triplet name="serif" style="normal" weight="normal"/>
<font-triplet name="serif" style="normal" weight="bold"/>
<font-triplet name="serif" style="italic" weight="normal"/>
<font-triplet name="serif" style="italic" weight="bold"/>
</font>
<font metrics-url="D:\DocBook\fop-0.95\JFonts\msgothic.xml" kerning="yes" embed-url="C:\windows\Fonts\msgothic.ttc">
<font-triplet name="MS-Gothic" style="normal" weight="normal"/>
<font-triplet name="MS-Gothic" style="normal" weight="bold"/>
<font-triplet name="MS-Gothic" style="italic" weight="normal"/>
<font-triplet name="MS-Gothic" style="italic" weight="bold"/>
<font-triplet name="MSGothic" style="normal" weight="normal"/>
<font-triplet name="MSGothic" style="normal" weight="bold"/>
<font-triplet name="MSGothic" style="italic" weight="normal"/>
<font-triplet name="MSGothic" style="italic" weight="bold"/>
<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-triplet name="MSゴシック" style="normal" weight="normal"/>
<font-triplet name="MSゴシック" style="normal" weight="bold"/>
<font-triplet name="MSゴシック" style="italic" weight="normal"/>
<font-triplet name="MSゴシック" style="italic" weight="bold"/>
<font-triplet name="sans-serif" style="normal" weight="normal"/>
<font-triplet name="sans-serif" style="normal" weight="bold"/>
<font-triplet name="sans-serif" style="italic" weight="normal"/>
<font-triplet name="sans-serif" style="italic" weight="bold"/>
</font>
</fonts>
</renderer>
</renderers>
</fop>


手順(5) サンプル実行

テストファイル JFT2.fo

<fo:root fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master name="PageMaster">
<fo:region-body top="3cm">
</fo:region-body>
</fo:simple-page-master>
<fo:page-sequence reference="PageMaster">
<fo:flow name="xsl-region-body">
<fo:block size="18pt" align="center">
Hello: XSL-FO to PDF 日本語フォントはどうか?
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:layout-master-set>

コマンドラインより

..\fop -c D:\DocBook\fop-0.95\conf\JFConf.xml -fo JFT2.fo -pdf a.pdf

と実行する。

0 件のコメント:

コメントを投稿