Personal tools
You are here: Home ブログ nagai Forrest の PDF 出力を日本語へ対応させる
Document Actions

Forrest の PDF 出力を日本語へ対応させる

今回使用したバージョンは以下のとおりです。
Forrest (Apache Forrest) 0.8


日本語フォント用と FOP 拡張設定用に、それぞれフォルダを作成します。

場所は任意ですが、FOP 拡張設定ファイルの参照元である PDF 出力プラグインの仕様 (後述) に合わせて、以下のようにしました。
%FORREST_HOME%\main\webapp\ext\fop
%FORREST_HOME%\fonts


日本語フォントの準備を行います。

FOP の TTFReader を利用して日本語フォントのフォントメトリクスファイルを作成します。
TTCファイルの場合は引数に -ttcname をつけます。
java -cp %FORREST_HOME%\lib\core\fop-0.20.5.jar org.apache.fop.fonts.apps.TTFReader msgothic.ttc -ttcname "MS Gothic" msgothic.xml
作成後、フォントファイルと作成したフォントメトリクスファイルをフォント用フォルダへ格納します。


FOP 拡張設定用ファイルを作成します。

参照元ファイルの共通設定は %FORREST_HOME%\main\webapp\skins\common\xslt\fo\document-to-fo.xsl で行われています。
これはその名の通り、中間生成物である FO への変換時に適用される XSL です。
設定は FOP の初期値が継承されるので、fontBaseDir と stream-fileter-list のみ上書きしています。fontBaseDir は URL で指定します。
また、fonts は document-to-fo.xsl で利用しているのが論理フォントであったことから、font-triplet に論理フォントを全て指定しています。
※設定方法は http://xmlgraphics.apache.org/fop/ や http://www.leverkruid.eu/FOP/index.html が参考になります。

%FORREST_HOME%\main\webapp\ext\fop\userconfig.xml
-------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <entry>
    <key>fontBaseDir</key>
    <value>file:///C:/Users/Public/ApacheForrest/fonts/</value>
  </entry>
  <entry role="pdf">
    <key>stream-filter-list</key>
    <list>
      <value>flate</value>
      <value>ascii-85</value>
    </list>
  </entry> 
  <fonts>
    <font embed-file="msgothic.ttc" kerning="yes" metrics-file="msgothic.xml">
      <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-triplet name="serif" style="italic" weight="normal"/>
      <font-triplet name="serif" style="oblique" weight="normal"/>
      <font-triplet name="serif" style="oblique" 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-triplet name="sans-serif" style="oblique" weight="normal"/>
      <font-triplet name="sans-serif" style="oblique" weight="bold"/>
      <font-triplet name="monospace" style="normal" weight="normal"/>
      <font-triplet name="monospace" style="normal" weight="bold"/>
      <font-triplet name="monospace" style="italic" weight="normal"/>
      <font-triplet name="monospace" style="italic" weight="bold"/>
      <font-triplet name="monospace" style="oblique" weight="normal"/>
      <font-triplet name="monospace" style="oblique" weight="bold"/>
      <font-triplet name="any" style="normal" weight="normal"/>
      <font-triplet name="any" style="normal" weight="bold"/>
      <font-triplet name="any" style="italic" weight="normal"/>
      <font-triplet name="any" style="italic" weight="bold"/>
      <font-triplet name="any" style="oblique" weight="normal"/>
      <font-triplet name="any" style="oblique" weight="bold"/>
    </font>
  </fonts>
</configuration>
-------------------------------------------------------------------------------


PDF 出力プラグインからユーザ設定ファイルが参照されるようにします。

user-config の src 属性に指定したパスが抽象パスの場合、%FORREST_HOME%\main\webapp がルートになります。絶対パスを指定することも可能です。
%FORREST_HOME%\plugins\org.apache.forrest.plugin.output.pdf\output.xmap
-------------------------------------------------------------------------------
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
      :
      <map:serializer name="fo2pdf" src="org.apache.cocoon.serialization.FOPSerializer" mime-type="application/pdf">
        <user-config src="ext/fop/userconfig.xml"/>
      </map:serializer>



PDF 変換時に正常に折り返されるようにfopに日本語であることを知らせます

これを指定しないと英語と同じアルゴリズムで処理されてしまい、単語間に空白がない日本語では文章が折り返されず、ページからはみ出してしまいます。
%FORREST_HOME%\main\webapp\skins\common\xslt\fo\document-to-fo.xsl
-------------------------------------------------------------------------------
  :
  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" language="ja">


これで日本語でのPDF表示が可能となります。

Category(s)
Forrest
FOP
The URL to Trackback this entry is:
http://dev.ariel-networks.com/Members/nagai/forrest-306e-pdf-51fa529b309265e5672c8a9e30785bfe5fdc3055305b308b/tbping
Add comment

You can add a comment by filling out the form below. Plain text formatting.

(Required)
(Required)
(Required)
(Required)
(Required)
This helps us prevent automated spamming.
Captcha Image


Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.