Jeff Yu noticed my post on javaeye forum and suggested me to use JBoss JDocbook maven plugin(http://www.jboss.org/maven-jdocbook-plugin), and complained it is lack of Chinese support at the same time. Given my docbook experience , if you want to use Apache FOP to render PDF and dream beautiful Chinese font support , you must customsize the FOP config file.
When I checked out the JDocbook source code and read the core code about pdf render, I found it have no way to use customized FOP config file.
I decided to resovle the problem , add serval lines of code , I made a patch soon. This patch has been submitted to JBoss , please see https://jira.jboss.org/jira/browse/MPJDOCBOOK-19(beside a patch, you can also get the modified pdf.xsl file and my customsized fop config file) .
This patch do the following thing in code.
1. Expose a option of the Apache FOP config file path in the configuration class - Options.
2. In the PDFRender class, use FopFactory to load this config file.
After apply the patch to jdocbook 2.1.2 , you can use a custom fop config file now.
As a example to test the patch, I use the translated JBoss Cache Core User Guide docbook source as a sample. You can get it from the kava community(http://www.kava.org.cn) which translate the JBoss Cache Core User Guide into Chinese.
Modify the pom.xml, add a userConfig element in options tag. The following is the User Guide profile in details.
<profile>
<!-- This profile generates Javadocs and the UserGuide, FAQs and Tutorial in the "package" phase. -->
<id>User Guide</id>
<activation>
</activation>
<properties>
<!-- override to generate javadocs in the "package" phase -->
<javadocPhase>package</javadocPhase>
</properties>
<build>
<plugins>
<!-- the docbook generation plugin for the user guide -->
<plugin>
<groupId>org.jboss.maven.plugins</groupId>
<artifactId>maven-jdocbook-plugin</artifactId>
<version>2.1.2.patched</version>[1]
<extensions>true</extensions>
<dependencies>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jbossorg-docbook-xslt</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jbossorg-jdocbook-style</artifactId>
<version>1.1.0</version>
<type>jdocbook-style</type>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>0.95</version>[2]
</dependency>
<dependency>[3]
<groupId>avalon-framework</groupId> <artifactId>avalon-framework</artifactId> <version>4.1.5</version> </dependency> <dependency> <groupId>avalon-framework</groupId> <artifactId>avalon-framework-impl</artifactId> <version>4.1.5</version> </dependency>
</dependencies>
<executions>
<execution>
<id>userguide_zh_CN</id>
<phase>package</phase>
<goals>
<goal>resources</goal>
<goal>generate</goal>
</goals>
<configuration>
<sourceDocumentName>master.xml</sourceDocumentName>
<sourceDirectory>${basedir}/src/main/docbook/userguide/zh_CN</sourceDirectory>
<imageResource>
<directory>${basedir}/src/main/docbook/images</directory>
</imageResource>
<cssResource>
<directory>${basedir}/src/main/docbook/css</directory>
</cssResource>
<targetDirectory>${basedir}/target/docbook/userguide_zh_CN</targetDirectory>
<formats>
<format>[4] <formatName>pdf</formatName> <stylesheetResource>classpath:/xslt/org/jboss/pdf.xsl</stylesheetResource> <finalName>userguide_zh_CN.pdf</finalName> </format>
<format>
<formatName>html</formatName>
<stylesheetResource>classpath:/xslt/org/jboss/xhtml.xsl</stylesheetResource>
<finalName>index.html</finalName>
</format>
<format>
<formatName>html_single</formatName>
<stylesheetResource>classpath:/xslt/org/jboss/xhtml-single.xsl</stylesheetResource>
<finalName>index.html</finalName>
</format>
</formats>
<options>
<xincludeSupported>false</xincludeSupported>
<userConfig>src/main/resources/fop.xconf</userConfig>[5]
</options>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
[2]I used the latest fop.
[3]Add avalon dependency due to a exception in compile progress.
[4]Use my modified pdf.xsl to render pdf.
[5]Specified the customsized fop config file location which include my favorite Chinese font config.
Let's look at the modified pdf.xsl ( which is provide in the jbossorg-docbook-xslt). This purpose is to use my favorite Chinese font in font family .
Find the following section.
<xsl:param name="title.font.family">
<xsl:call-template name="pickfont-sans"/>
</xsl:param>
<xsl:param name="body.font.family">
<xsl:call-template name="pickfont-sans"/>
</xsl:param>
<xsl:param name="monospace.font.family">
<xsl:call-template name="pickfont-mono"/>
</xsl:param>
<xsl:param name="sans.font.family">
<xsl:call-template name="pickfont-sans"/>
</xsl:param>
<xsl:param name="body.font.family">'SimSun'</xsl:param>
<xsl:param name="title.font.family">'SimHei'</xsl:param>
<xsl:param name="monospace.font.family">'SimKai'</xsl:param>
<xsl:param name="sans.font.family">'SimSun'</xsl:param>
The best way is redefine the pikfont-sans, pikfont-mono template definition, it is friendly to multilanguage support. As a lazy guy , I use Chinese font directly.
You might notice that I use a single Chinese font definition in font family instead of a series of font name , because FOP can not correctly process the font family if you use other none Chinese font together. This problem may be fixed in the snapshot version.
Now you can generate PDF .
评论