[ Topページへ戻る ]

Apache FOPでPDF

【SmartDoc編】

2003.7.30よりアクセス

はじめに

FOP(Formatting Objects Processor)は、ApacheのXMLプロジェクトにおいて開発されているXSLベースのフリーのフォーマットツールです。現時点では、PDF, PCL, PS, SVG, XML, AWT, MIF, TXTの出力形式に対応しています。

XSL(Extensible Stylesheet Language)は、XMLに書式情報を与えて画面やプリンタといった出力機器にフォーマットするための規格です。HTMLにおけるCSS(Cascading Style Sheets)と対比するとXSLのイメージがつかみ易いでしょう。

XSLの仕様の一部として考えられていたXSLTが先に分離独立して世に出たので、XSLTの方が有名になっています。XSLTに対して区別するためか、XSL-FO(Format Objects)と呼ばれることもあります。

Apache FOPのセットアップ

入手

FOPは、Javaで実行可能なツールです。まずJava実行環境(Java Runtime Environment)が必要となります。Java実行環境がなければ下記サイトから無償で入手できます。
※Windows, Solaris, Linux-x86の場合。他のOSも大半はJava実行環境が出ているので、OSのホームページ等から入手できます。

Apache FOPは、以下のサイトから入手します。

インストール

以下のバージョンをインストールします。

2003.7.30現在の最新版
ソフトウェア バージョン 備考
Java実行環境 1.4.2 JDK1.4.2に含まれるものを使用
Apache FOP 0.20.5

Java実行環境のインストールはここでは説明しません。各種サイトをご覧下さい。

Apache FOPは、TAR+GZIP形式またはZIP形式でアーカイブされているので、然るべきディレクトリにて展開します。

コマンド実行環境の設定

Apache FOPをコマンド環境で使用するための設定を行います。

Windows コマンドプロンプト

Apache FOPを展開したディレクトリに、fop.batが含まれています。環境変数PATHにこのディレクトリを追加します。

Windows Cygwin環境

Cygwin環境ではコマンドシェルにbashが利用できるので、aliasにfop.batを指定します。

alias fop='/usr/local/java/fop-0.20.5/fop.bat'

日本語フォント環境の設定

Apache FOPをそのままで使用した場合、生成されたPDFは日本語フォントを扱うことができません。FOPのデフォルト設定では日本語フォントを扱う情報が不足しているからです。そこでFOPに追加フォントを指定します。具体的には追加したいフォントのフォントメトリクス情報を記述したXMLファイルを作成し、このXMLファイルをFOPユーザ設定ファイルに登録します。

Apache FOPには、指定したTrueTypeフォントを読み込んでフォントメトリクス情報を記述したXMLファイルを生成する機能があります。

Windowsでの日本語フォント追加

まずMS明朝とMSゴシックの2種類のフォントを扱えるように設定します。

Windows OSの場合、標準で日本語を使用できるTrueTypeフォントとしてMS明朝とMSゴシックが搭載されています。それぞれmsmincho.ttc、msgothic.ttcというファイルに格納されています。ttcファイルは複数のTrueTypeフォントを含んだファイルのことです。例えばMS明朝には等幅とプロポーショナルの2種類があり、それらが一つのmsmincho.ttcに含まれます。このttcファイルからFOPのフォントメトリクス情報記述XMLファイルを生成するにはフォント名が必要となります。

フォントメトリクス情報の取得

まずフォント名を取得します。FOPに含まれるorg.apache.fop.fonts.apps.TTFReaderクラスを実行します。オプションにttcファイルを指定します。ttcファイルを指定した場合-ttcnameオプションも指定しなければなりませんが、ここではわざとエラーを起こさせてフォント名を取得しています。

msmincho.ttcファイルに含まれるフォント名を取得
work$ java -cp d:/java/fop-0.20.5/build/fop.jar \
 org.apache.fop.fonts.apps.TTFReader c:/WINNT/Fonts/msmincho.ttc msmincho.xml
TTF Reader v1.1.1

Reading c:/WINNT/Fonts/msmincho.ttc...

This is a TrueType collection file with 2 fonts
Containing the following fonts:
MS Mincho
MS PMincho
java.lang.IllegalArgumentException: For TrueType collection you must specify whi
ch font to select (-ttcname)
        at org.apache.fop.fonts.TTFFile.readFont(TTFFile.java:416)
        at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:222)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:184)
work$ 

フォント名は"MS Mincho"と"MS PMincho"の2つとなります。

msgothic.ttcファイルに含まれるフォント名を取得
work$ java -cp d:/java/fop-0.20.5/build/fop.jar \
 org.apache.fop.fonts.apps.TTFReader c:/WINNT/Fonts/msgothic.ttc msgothic.xml
TTF Reader v1.1.1

Reading c:/WINNT/Fonts/msgothic.ttc...

This is a TrueType collection file with 3 fonts
Containing the following fonts:
MS Gothic
MS PGothic
MS UI Gothic
java.lang.IllegalArgumentException: For TrueType collection you must specify whi
ch font to select (-ttcname)
        at org.apache.fop.fonts.TTFFile.readFont(TTFFile.java:416)
        at org.apache.fop.fonts.apps.TTFReader.loadTTF(TTFReader.java:222)
        at org.apache.fop.fonts.apps.TTFReader.main(TTFReader.java:184)
work$ 

フォント名は"MS Gothic"と"MS PGothic"および"MS UI Gothic"の3つとなります。

続いて、MS明朝("MS Mincho")とMSゴシック("MS Gothic")のフォントメトリクス情報XMLファイルを生成します。

msmincho.ttcファイルに含まれるMS明朝フォントのフォントメトリクス情報XMLを生成
work$ java -cp d:/java/fop-0.20.5/build/fop.jar \
 org.apache.fop.fonts.apps.TTFReader -ttcname "MS Mincho" \            
 c:/WINNT/Fonts/msmincho.ttc msmincho.xml
TTF Reader v1.1.1

Reading c:/WINNT/Fonts/msmincho.ttc...

This is a TrueType collection file with 2 fonts
Containing the following fonts:
* MS Mincho
MS PMincho
Number of glyphs in font: 17807
Postscript format 3
Creating xml font file...

Creating CID encoded metrics
Writing xml font file msmincho.xml...

This font contains no embedding license restrictions
work$ 

MS明朝フォントのフォントメトリクス情報を持ったmsmincho.xmlファイルが生成されました。

msgothic.ttcファイルに含まれるMSゴシックフォントのフォントメトリクス情報XMLを生成
work$ java -cp d:/java/fop-0.20.5/build/fop.jar \
 org.apache.fop.fonts.apps.TTFReader -ttcname "MS Gothic" \            
 c:/WINNT/Fonts/msgothic.ttc msgothic.xml
TTF Reader v1.1.1

Reading c:/WINNT/Fonts/msgothic.ttc...

This is a TrueType collection file with 3 fonts
Containing the following fonts:
* MS Gothic
MS PGothic
MS UI Gothic
Number of glyphs in font: 20458
Postscript format 3
Creating xml font file...

Creating CID encoded metrics
Writing xml font file msgothic.xml...

This font contains no embedding license restrictions
work$ 

MSゴシックフォントのフォントメトリクス情報を持ったmsgothic.xmlファイルが生成されました。

フォントの登録

Apache FOPを展開したディレクトリの中にconf/userconf.xmlというファイルがあります。このファイルに日本語フォントの登録を記述することにします。<fonts>要素の中に複数の<font>要素が書けますので、ここに追加します。

MS明朝フォントの登録
 <font metrics-file="d:\java\fop-0.20.5\conf\msmincho.xml" kerning="yes"
 embed-file="C:\winnt\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>
MSゴシックフォントの登録
 <font metrics-file="d:\java\fop-0.20.5\conf\msgothic.xml" kerning="yes"
 embed-file="C:\winnt\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>

動作確認

セットアップの動作確認のため、簡単なXSL-FOファイルを作成し、これをApache FOPを使ってPDF形式に変換して表示確認します。

hello.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="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">
        こんにちは、FOP
      </fo:block>
    </fo:flow>

  <fo:page-sequence>

</fo:root>
hello.foからPDFファイルを生成
work$ fop -c d:/java/fop-0.20.5/conf/userconfig.xml hello.fo hello.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] Parsing of document complete, stopping renderer
work$ 

SmartDocからFO文書への変換

SmartDocからXSL-FO文書へ変換するには、sdoc2fo.xslを使用します。まずsdoc2co.xslを入手します。

sdoc2fo.xslの入手

以下のURLから入手できます。

sdoc2fo.xslの修正

上記で入手したsdoc2fo.xslが対象としているApache FOPのバージョンがやや古いため、そのままではエラーが発生します。以下の修正を加えます。

以下にdiffを取った内容を示します。

sdoc2fo.xslの修正個所
sdoc_fo$ diff sdoc2fo.xsl.orig sdoc2fo.xsl
348c348
<       <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
---
>       <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" language="ja">
391c391
<       <fo:page-sequence master-name="PageMaster-Top">
---
>       <fo:page-sequence master-reference="PageMaster-Top">
438c438
<       <fo:page-sequence master-name="PageMaster-Cover">
---
>       <fo:page-sequence master-reference="PageMaster-Cover">
504c504
<       <fo:page-sequence master-name="PageMaster">
---
>       <fo:page-sequence master-reference="PageMaster">
sdoc_fo$

SmartDoc文書のPDF化

以下の手順でPDF化を実施します。

  1. SmartDoc文書からPureSmartDoc文書の生成
  2. PureSmartDoc文書からPDF文書の生成
SmartDoc文書からPDF文書の生成処理例
sdoc_fo$ ls
sample.sdoc   sdoc2fo.xsl
sdoc_fo$ sdoc -format:pure sample.sdoc
sdoc_fo$ ls
PureSmartDoc.dtd   sample.sdoc   sample.xml   sdoc2fo.xsl
sdoc_fo$ fop -xsl sdoc2fo.xsl -c d:/java/fop-0.20.5/conf/userconfig.xml
 -xml array.xml -pdf array.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
[ERROR] property - "reference-orientation" is not implemented yet.
[ERROR] property - "reference-orientation" is not implemented yet.
[INFO] [1]
[INFO] Using org.apache.xerces.parsers.SAXParser as SAX2 Parser
[INFO] [2]
[INFO] [3]
[INFO] area contents overflows area in line ?
[WARNING] Some static content could not fit in the area.
[WARNING] Some static content could not fit in the area.
[INFO] [4]
[WARNING] Some static content could not fit in the area.
[WARNING] Some static content could not fit in the area.
[INFO] Parsing of document complete, stopping renderer
sdoc_fo$ ls
PureSmartDoc.dtd   sample.pdf   sample.sdoc   sample.xml   sdoc2fo.xsl
sdoc_fo$

現状、いくつか(というかかなり)問題があります。

参考文献