4-2. ユーザー・コンフィグレーション・ファイル
フォント・メトリクス・ファイルを作成するだけでは、 FOP にフォントを追加したことになりません。
FO 中では単にフォント名が記述されているだけです。このため、FOP のコンフィグレーションに、フォント名と実際のフォント・メトリクス・ファイルの関係を追加しておく必要があります。
Apache FOP を展開したディレクトリの中に conf/userconf.xml というファイルがあります。このファイルにフォント名とフォン・メトリクス・ファイルの関係を記述します。
fonts 要素の中に複数の font 要素が書けますので、ここに追加します。
追加の留意点:
-
フォント名は自由につけてかまいません。
-
ひとつのフォント・メトリクス・ファイルに対して、複数のフォント名を指定することができます。
-
この例のように フォント名に漢字が含まれている 場合は、ファイルは UTF-8 で保存 しなければなりません。 Shift JIS や EUC で保存すると、 FOP 実行時にエラーになります。
-
DocBook から FO を作成する際に、 DocBook のどの要素に、どのフォントを使用するかを XSL で記述する必要があります。この際のフォント名として、このコンフィグレーションで定義したフォント名を使用します。
<fonts
font metrics-file="C:\cygwin\usr\local\java\fop-0.20.5\conf\msmincho.xml"
kerning="yes" embed-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-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="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>
<font metrics-file="C:\cygwin\usr\local\java\fop-0.20.5\conf\mspmincho.xml"
kerning="yes" embed-file="C:\WINDOWS\Fonts\msmincho.ttc">
<font-triplet name="PMincho" style="normal" weight="normal"/>
<font-triplet name="PMincho" style="normal" weight="bold"/>
<font-triplet name="PMincho" style="italic" weight="normal"/>
<font-triplet name="PMincho" style="italic" weight="bold"/>
<font-triplet name="MS-PMincho" style="normal" weight="normal"/>
<font-triplet name="MS-PMincho" style="normal" weight="bold"/>
<font-triplet name="MS-PMincho" style="italic" weight="normal"/>
<font-triplet name="MS-PMincho" style="italic" weight="bold"/>
<font-triplet name="MSPMincho" style="normal" weight="normal"/>
<font-triplet name="MSPMincho" style="normal" weight="bold"/>
<font-triplet name="MSPMincho" style="italic" weight="normal"/>
<font-triplet name="MSPMincho" style="italic" weight="bold"/>
<font-triplet name="MSP明朝" style="normal" weight="normal"/>
<font-triplet name="MSP明朝" style="normal" weight="bold"/>
<font-triplet name="MSP明朝" style="italic" weight="normal"/>
<font-triplet name="MSP明朝" style="italic" weight="bold"/>
</font>
<font metrics-file="C:\cygwin\usr\local\java\fop-0.20.5\conf\msgothic.xml"
kerning="yes" embed-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-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="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>
<font metrics-file="C:\cygwin\usr\local\java\fop-0.20.5\conf\mspgothic.xml"
kerning="yes" embed-file="C:\WINDOWS\Fonts\msgothic.ttc">
<font-triplet name="PGothic" style="normal" weight="normal"/>
<font-triplet name="PGothic" style="normal" weight="bold"/>
<font-triplet name="PGothic" style="italic" weight="normal"/>
<font-triplet name="PGothic" style="italic" weight="bold"/>
<font-triplet name="MS-PGothic" style="normal" weight="normal"/>
<font-triplet name="MS-PGothic" style="normal" weight="bold"/>
<font-triplet name="MS-PGothic" style="italic" weight="normal"/>
<font-triplet name="MS-PGothic" style="italic" weight="bold"/>
<font-triplet name="MSPGothic" style="normal" weight="normal"/>
<font-triplet name="MSPGothic" style="normal" weight="bold"/>
<font-triplet name="MSPGothic" style="italic" weight="normal"/>
<font-triplet name="MSPGothic" style="italic" weight="bold"/>
<font-triplet name="MSPゴシック" style="normal" weight="normal"/>
<font-triplet name="MSPゴシック" style="normal" weight="bold"/>
<font-triplet name="MSPゴシック" style="italic" weight="normal"/>
<font-triplet name="MSPゴシック" style="italic" weight="bold"/>
</font>
<font metrics-file="C:\cygwin\usr\local\java\fop-0.20.5\conf\msuigothic.xml"
kerning="yes" embed-file="C:\WINDOWS\Fonts\msgothic.ttc">
<font-triplet name="UIGothic" style="normal" weight="normal"/>
<font-triplet name="UIGothic" style="normal" weight="bold"/>
<font-triplet name="UIGothic" style="italic" weight="normal"/>
<font-triplet name="UIGothic" style="italic" weight="bold"/>
<font-triplet name="MS-UIGothic" style="normal" weight="normal"/>
<font-triplet name="MS-UIGothic" style="normal" weight="bold"/>
<font-triplet name="MS-UIGothic" style="italic" weight="normal"/>
<font-triplet name="MS-UIGothic" style="italic" weight="bold"/>
<font-triplet name="MSUIGothic" style="normal" weight="normal"/>
<font-triplet name="MSUIGothic" style="normal" weight="bold"/>
<font-triplet name="MSUIGothic" style="italic" weight="normal"/>
<font-triplet name="MSUIGothic" style="italic" weight="bold"/>
<font-triplet name="MSUIゴシック" style="normal" weight="normal"/>
<font-triplet name="MSUIゴシック" style="normal" weight="bold"/>
<font-triplet name="MSUIゴシック" style="italic" weight="normal"/>
<font-triplet name="MSUIゴシック" style="italic" weight="bold"/>
</font>
</fonts>
サンプル: userconfig.xml
これでフォントの追加は終了です。
日本語を含む PDF を作成することができるかどうか確認してみます。
まず、日本語を含む .fo ファイルが必要ですので、 FOP に添付されているサンプル helloworld.fo を日本語を追加して helloworld-jp.fo を作成します。
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4" page-height="29.7cm"
page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm"
margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block>Hello World!</fo:block>
<fo:block font-size="18pt" font-family="Mincho">
Mincho: ハロー ワールド!
</fo:block>
<fo:block font-size="18pt" font-family="PMincho">
PMincho: ハロー ワールド!
</fo:block>
<fo:block font-size="18pt" font-family="Gothic">
Gothic: ハロー ワールド!
</fo:block>
<fo:block font-size="18pt" font-family="PGothic">
PGothic: ハロー ワールド!
</fo:block>
<fo:block font-size="18pt" font-family="UIGothic">
UIGothic: ハロー ワールド!
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
FOPを実行します。
$ pwd /usr/local/java/fop-0.20.5/examples/embedding/xml/fo $ fop helloworld-jp.fo helloworld-jp.pdf [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] FOP 0.20.5 [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] building formatting object tree [INFO] setting up fonts [INFO] [1] [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser [INFO] Parsing of document complete, stopping renderer $ ls -l total 23 -rw-r--r-- 1 merita なし 1124 Jan 19 13:44 helloworld-jp.fo -rwxr-xr-x 1 merita なし 19558 Jan 19 13:57 helloworld-jp.pdf -rw-r--r-- 1 merita なし 541 Jul 15 2003 helloworld.fo
サンプル: helloworld-jp.pdfhelloworld.fo
Acrobat Reader 6.0 で見ると、以下のようになります。
[ファイル]-[文書のプロパティ] の [フォント] で確認すると、日本語のフォントが埋め込まれていることが確認できます。
コマンドラインで起動する注意点
こちらで提供している fop_sh.txt では、デフォルトでユーザーコンフィグレーションをリードするようにしてあります。実際の java のコマンドラインでは、 -c ファイル名 でユーザーコンフィグレーションを指定する必要があります。
java -classpath (クラスパス) org.apache.fop.apps.Fop \ -c c:/usr/local/java/fop-0.20.5/conf/userconfig.xml \ input.fo output.pdf
新着記事
-
#1724. 店名は変われど味は変わらず。横浜中華街・富泰楼麺房のとりそば ― 澄んだスープに鶏油と葱だけのシンプル極上ラーメン:
2026-01-20 横浜中華街・広東路の富泰楼麺房(ふうたいろうめんぼう)にてランチ。もとは揚州麺房だった店だが、2024年7月1日に店名を変更して富泰楼麺房になった。オーナーは変わったが、料理人はそのままで、料理の味も変わっていない。横浜中華街は店の入れ替わ...
-
#1723. 大ぶり海老天が2本。横浜元町「そば処 木の芽」海老天丼 ― 蕎麦屋の仕事が光る:
2026-01-19 横浜・元町のそば処 木の芽にてランチ。海老天丼をオーダー。蕎麦屋の丼ものの「花」と言えば、やはり天丼、なかでも海老天丼。今日は暖簾をくぐる前からそれ以外の選択肢が思い浮かばなかった。運ばれてきた丼には、大ぶりの海老天が堂々と2本。衣は軽く、...
-
#1722. 春菊+柚子胡椒が決め手の本丸亭・横浜元町店の本丸塩らー麺 ― 代官坂で味わう極上塩ラーメンランチ:
2026-01-17 横浜・元町・代官坂のラーメン店本丸亭(ほんまるてい)横浜元町店にてランチ。本丸塩らー麺をオーダー。トッピングは、ガッツリ系のチャーシューに雲呑、そして春菊。標準で春菊が入っているのも特徴だが、さらに春菊を追加してもらう。春菊がラーメンに入る...
-
#1721. 黒ホッピーで独酌!東北人家の閃椒土豆絲&干鍋筋头巴脑 ― 横浜中華街で味わう中国東北料理の辛旨キレ味:
2026-01-16 横浜中華街・広東道の東北人家(とうほくじんか)新館にて、晩餐という名の独酌。東北人家は、中国東北地方の料理(東北菜)の店。中華街では珍しくホッピーが置いてあり、本日も黒ホッピーのセットでスタート。オーダーは閃椒土豆絲(ジャガイモ細切りの唐辛...
-
#1720. 横浜・馬車道の老舗トンカツ「勝烈庵」で昼ビール&ロースかつ定食 ― 休日に背筋が伸びる名店でひとり贅沢ランチ:
2026-01-16 休暇をとって、午前中に所用を済ます。そのまま、横浜の街を少し散歩して、横浜・馬車道の勝烈庵(かつれつあん)馬車道総本店にてランチ。勝烈庵といえば、横浜のトンカツの老舗。やや高級店の落ち着いた雰囲気があり、背筋が伸びる一方で、不思議と居心地が...
merita.jp