UbuntuでApacheFOP その1
今度はFOPをやってみたいと思います。
正確にはXSLTとXSL-FOですけどね。
まずは環境準備です。
$ sudo apt-get install fop
インストールはこれだけです。
続きまして日本語フォントの設定です。
$ cp /usr/share/doc/fop/fop.xconf ~/.
このコピーしたfop.xconfに使用するフォントを追加します。
今回はサンプルでTakaoGothicを追加してみます。
<fonts> <!-- embedded fonts -->
の下に
<font kerning="yes" embed-url="/usr/share/fonts/truetype/takao/TakaoGothic.ttf"> <font-triplet name="TakaoGothic" style="normal" weight="normal"/> </font>
を追加します。
それでは変換テストです。
sample.xml
<?xml version="1.0" encoding="utf-8"?> <column> <title> 攻略! XSL-FO </title> <abstract> 今度はXMLファイルからXSLTを利用してXSL-FOからPDFを作成します。 </abstract> </column>
sample.xsl
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/column"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="A4-portrait" page-height="29.7cm" page-width="21.0cm" margin="2cm"> <fo:region-body margin-top="1cm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4-portrait"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="14pt" color="red" margin="0.5cm" font-family="TakaoGothic"> <xsl:value-of select="title"/> </fo:block> <fo:block font-size="12pt" margin="0.5cm" font-family="TakaoGothic"> <xsl:value-of select="abstract"/> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> </xsl:stylesheet>
変換 & 参照
$ fop -c ~/fop.xconf -xml sample.xml -xsl sample.xsl sample.pdf $ evince sample.pdf
次回からはフォーマットについてやる予定です。