跳至主要内容

Hack JBoss JDocbook Maven Plugin

I've used Docbook to organize my technical document for some years. But I prefered using my custom ant-based publish tools than other tools due to the poor Chinese font support. I've just shared my house-use docbook publish tool on google code (http://code.google.com/p/hantsy-labs/) some days ago. Then I created a group on javaeye.com(one of the biggest java community in China) to discuss docbook usage.
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>

[1]I renamed my modified jdocbook version to 2.1.2.pachted
[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>

Replace them to :

<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 .
发表评论

此博客中的热门博文

JPA 2.1: Attribute Converter

JPA 2.1: Attribute Converter If you are using Hibernate, and want a customized type is supported in your Entity class, you could have to write a custom Hibernate Type. JPA 2.1 brings a new feature named attribute converter, which can help you convert your custom class type to JPA supported type. Create an Entity Reuse thePostentity class as example. @Entity @Table(name="POSTS") public class Post implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID") private Long id; @Column(name="TITLE") private String title; @Column(name="BODY") private String body; @Temporal(javax.persistence.TemporalType.DATE) @Column(name="CREATED") private Date created; @Column(name="TAGS") private List<String> tags=new ArrayList<>(); } Create an attribute converter In this example…

Auditing with Hibernate Envers

Auditing with Hibernate Envers The approaches provided in JPA lifecyle hook and Spring Data auditing only track the creation and last modification info of an Entity, but all the modification history are not tracked. Hibernate Envers fills the blank table. Since Hibernate 3.5, Envers is part of Hibernate core project. Configuration Configure Hibernate Envers in your project is very simple, just need to addhibernate-enversas project dependency. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> </dependency> Done. No need extra Event listeners configuration as the early version. Basic Usage Hibernate Envers provides a simple@Auditedannotation, you can place it on an Entity class or property of an Entity. @Audited private String description; If@Auditedannotation is placed on a property, this property can be tracked.

Create a restful application with AngularJS and Zend 2 framework

Create a restful application with AngularJS and Zend 2 framework This example application uses AngularJS/Bootstrap as frontend and Zend2 Framework as REST API producer. The backend code This backend code reuses the database scheme and codes of the official Zend Tutorial, and REST API support is also from the Zend community. Getting Started with Zend Framework 2Getting Started with REST and Zend Framework 2 Zend2 provides aAbstractRestfulControllerfor RESR API producing. class AlbumController extends AbstractRestfulController { public function getList() { $results = $this->getAlbumTable()->fetchAll(); $data = array(); foreach ($results as $result) { $data[] = $result; } return new JsonModel(array( 'data' => $data) ); } public function get($id) { $album = $this->getAlbumTable()->getAlbum($id); return new JsonModel(array("data" => $album)); } …