1. 用docbook生成html文件(如果要导出pdf可越过此部分)
从sourceforge上下载docbook-xsl.zip,假设解压到e:/docbook-xsl/目录下
制作docbook xml: sample.xml文件如下:
<?xml version='1.0' encoding="utf-8"?>
<article xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="zh-CN"
xmlns:xlink='http://www.w3.org/1999/xlink'>
<articleinfo>
<title>我的第一篇Docbook 5.0文档</title>
<author>
<firstname>Easwy</firstname>
<surname>Yang</surname>
</author>
</articleinfo>
<section>
<title>文档介绍</title>
<para>
这是我的第一篇Docbook 5.0文档,欢迎你来到<link xlink:href=http://philj.iteye.com'>philJ的博客</link>。
</para>
</section>
</article>
然后制作xsl文件:xsl-sample.xsl如下:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>
<xsl:include href="e:/docbook-xsl/html/docbook.xsl"/>
<xsl:output method="html"
encoding="UTF-8"
indent="no"/>
</xsl:stylesheet>
假设这两个文件都放在E:/sample目录下。
从apache官网上下载xalan.zip,解压后把jar包(好像总共四个)放到项目lib目录,导入到项目中。
创建一个main class,在main方法中加入如下代码:
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Source xmlSource = new StreamSource(new FileInputStream("E:/sample/sample.xml"));
Source xslSource = new StreamSource(new FileInputStream("E:/sample/xsl_sample.xsl"));
OutputStream out = new FileOutputStream(new File("E:/1.html"));
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult(out));
} catch (Exception e) {
e.printStackTrace();
}
运行main方法,这样就会在e盘根目录下生成1.html文件。
2. 用docbook生成pdf文件
假设你的项目路径为D:\workspace\dbk-test
在项目下建立docbook-xsl, fo-res, out, sample四个文件夹
docbook-xsl目录: 放从sourceforge上下载的docbook-xsl.zip解压的文件
fo-res目录: 放一些apache fop需要配置的文件(下面会说到)
out目录: 放导出结果
sample目录: 放xml模板
从apache fop项目下载fop,目前是fop0.95, 只要下载binary版就行了。
解压后将build目录下的fop.jar加到你项目里,然后将conf下的fop.xconf配置文件放到fo-res目录下,记住这个文件,等会用它。
从apache xalan项目下载xalan_j, 目前版本是xalan-j_2_7,解压后将serializer.jar,xalan.jar,xercesImpl.jar,xml-apis.jar四个jar包加到项目中。
一般用fop导出pdf会遇到中文乱码问题,网上说了很多解决的方法,但是都不完整,我现在整理一下(其实fop还有一个中文断行的问题,不过fop0.95已经解决得差不多了),下一步创建一个main函数,加入下列代码
public static void main(String[] args) {
String[] parameters = {
"-ttcname",
"SimSun",
"c:\\WINDOWS\\Fonts\\simsun.ttc", "d:\\workspace\\dbk-test\fo-res\\simsun.xml", };
TTFReader.main(parameters);
}
执行main,会在fo-res目录下生成一个simsun.xml
修改fo-res目录下上面提到的fop.xconf文件,在fonts节点下加入或替换下列内容
<font metrics-url="./fo-res/simsun.xml" kerning="yes" embed-url="file:///c:/WINDOWS/Fonts/simsun.ttc">
<font-triplet name="Simsun" style="normal" weight="normal"/>
<font-triplet name="Simsun" style="normal" weight="bold"/>
<font-triplet name="Simsun" style="italic" weight="normal"/>
<font-triplet name="Simsun" style="italic" weight="bold"/>
</font>
注意metrics-url参数值我用的是相对路径的形式,你可以用绝对路径,如:file:///d:/workspace/dbk-test/fo-res/simsun.xml
OK,next step,在项目docbook-xsl目录下,找到fo文件夹,应该你能在文件夹下面找到docbook.xsl文件,路径相当于docbook-xsl/fo/docbook.xsl;
好,我又在fo-res目录下新建一个docbook_zh.xsl文件,加入下列内容:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'>
<xsl:include href="../docbook-xsl/fo/docbook.xsl"/>
<xsl:output method="fo"
encoding="UTF-8"
indent="no"/>
<xsl:param name="body.font.family">Simsun</xsl:param>
<xsl:param name="body.font.size">12</xsl:param>
<xsl:param name="monospace.font.family">Simsun</xsl:param>
<xsl:param name="title.font.family">Simsun</xsl:param>
<xsl:param name="page.margin.inner">2cm</xsl:param>
<xsl:param name="page.margin.outer">2cm</xsl:param>
<xsl:param name="paper.type" select="'A4'"/>
<xsl:param name="hyphenate">false</xsl:param>
<xsl:param name="l10n.gentext.default.language" select="'zh_cn'"/>
</xsl:stylesheet>
注意,xsl-include节点我用的也是相对路径,同样以可以改为d:/workspace/dbk-test/docbook-xsl/fo/docbook.xsl
然后我们写个例子,写个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<article>
<articleinfo>
<title>About DocBook</title>
<pubdate></pubdate>
</articleinfo>
<section>
<title>用docbook生成pdf文件</title>
<para>一般用fop导出pdf会遇到中文乱码问题,网上说了很多解决的方法,但是都不完整,我现在整理一下(其实fop还有一个中文断行的问题,不过fop0.95已经解决得差不多了),下一步创建一个main函数,加入下列代码</para>
<para><firstterm>DocBook has been created by
<trademark>O'Reilly</trademark>, the famous publisher.</firstterm></para>
</section>
<section>
<title>用docbook生成pdf文件</title>
<itemizedlist>
<listitem>
<para id="comments">XHTML is good for web pages.</para>
</listitem>
<listitem>
<para>DITA is promising.</para>
</listitem>
<listitem>
<para>DocBook is ready for production use.</para>
<figure>
<title>DocBook logo中文</title>
<mediaobject>
<imageobject>
<imagedata fileref="e:/2.jpg" width="300px" />
</imageobject>
</mediaobject>
</figure>
<table>
<title>Document types</title>
<tgroup cols="3">
<thead>
<row>
<entry align="center">Document type</entry>
<entry align="center">Number of elements</entry>
<entry align="center">Comments</entry>
</row>
</thead>
<tbody>
<row>
<entry>XHTML</entry>
<entry>77</entry>
<entry>See <link linkend="comments">above</link>.</entry>
</row>
<row>
<entry>DITA</entry>
<entry>141</entry>
<entry></entry>
</row>
<row>
<entry>DocBook</entry>
<entry></entry>
<entry></entry>
</row>
</tbody>
</tgroup>
</table>
</listitem>
</itemizedlist>
</section>
<section>
<title>Conclusion</title>
<blockquote>
<attribution>Arthur Bloch</attribution>
<para>Don't force it... Get it bigger hammer.</para>
</blockquote>
<para>DocBook is a pretty big hammer.</para>
</section>
</article>
例子中有中文,有图片,将文件命名为docbook_sample.xml放到项目sample目录下
配置文件都准备好了,写代码,新建一个main
public static void main(String[] args) {
try {
System.out.println("FOP ExampleXML2PDF\n");
System.out.println("Preparing...");
File baseDir = new File(".");
File outDir = new File(baseDir, "out");
outDir.mkdirs();
// Setup input and output files
File xmlfile = new File(baseDir, "/sample/docbook_sample.xml");
File xsltfile = new File(baseDir, "/fo-res/docbook_zh.xsl");
File pdffile = new File(outDir, "/sample.pdf");
File conffile = new File(baseDir, "/fo-res/fop_zh.xconf");
System.out.println("Input: XML (" + xmlfile + ")");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...");
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
//加载配有中文配置的文件
fopFactory.setUserConfig(conffile);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
System.out.println("Success!");
} catch (Exception e) {
e.printStackTrace();
}
}
运行main,会在out目录下生成sample的pdf文件。
OK,打完收工,牛飞出品!
本文详细介绍如何使用DocBook结合XSL、FOP等工具生成带有中文内容及图片的PDF文件,包括所需配置文件的制作与代码实现。

893

被折叠的 条评论
为什么被折叠?



