继上一篇:使用java将xml格式化,本blog主要描述如何通过xslt将xml渲染为html(包含CSS),然后再
将html转为pdf.
如果你想了解更多xslt信息,请点击链接xslt。本程序直接使用javax.xml.transform.Transformer将howto.xsl通过数据源howto.xml渲染并保存为howto.html,然后通过第三方开源软件http://code.google.com/p/flying-saucer/将生成的XHTML转换为pdf.(注意:任何标签都必须闭合,否则flying-Saucer做XML解析会报错)
主要代码如下(程序已经上传):
howto.xml:
<?xml version="1.0"?>
<products>
<product href="http://www.playfield.com/text">
<name>Playfield Text</name>
<price currency="usd">299</price>
<description>Faster than the competition.</description>
<version>1.0</version>
</product>
<product href="http://www.playfield.com/virus">
<name>Playfield Virus</name>
<price currency="eur">199</price>
<description>
Protect yourself against malicious code.
</description>
<version>5.0</version>
</product>
<product href="http://www.playfield.com/calc">
<name>Playfield Calc</name>
<price currency="usd">299</price>
<description>Clear picture on your data.</description>
<version>1.5</version>
</product>
<product href="http://www.playfield.com/db">
<name>Playfield DB</name>
<price currency="cad">599</price>
<description>Organize your data.</description>
</product>
</products>
howto.xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:htm="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/products">
<html ang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cascading Style Sheet</title>
<link rel="stylesheet" type="text/css" href="table.css" title="Style"/>
</head>
<body>
<table>
<tr class="header">
<td>Name</td>
<td>Price</td>
<td>Description</td>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="product[position() mod 2 = 1]">
<tr class="odd">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="description"/></td>
</tr>
</xsl:template>
<xsl:template match="product">
<tr class="even">
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="description"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
table.css:
.header { background-color: #999999; font-weight: bold; }
.odd { background-color: normal; }
.even { background-color: #dfdfdf; }
package sunflowerbbs.com;
import javax.xml.transform.*;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
public class HowToXSLT {
public static void main(String[] args) throws Exception{
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory
.newTransformer(new javax.xml.transform.stream.StreamSource(
"howto.xsl"));
transformer.transform(new javax.xml.transform.stream.StreamSource(
"howto.xml"), new javax.xml.transform.stream.StreamResult(
new FileOutputStream("howto.html")));
} catch (Exception e) {
e.printStackTrace();
}
convertHtmlToPdf("howto.html","product.pdf");
}
public static boolean convertHtmlToPdf(String inputFile, String outputFile)
throws Exception {
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
String url = new File(inputFile).toURI().toURL().toString();
renderer.setDocument(url);
// 解决中文支持问题
// ITextFontResolver fontResolver = renderer.getFontResolver();
// fontResolver.addFont("C:/Windows/Fonts/SIMSUN.TTC",
// BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解决图片的相对路径问题
// renderer.getSharedContext().setBaseURL("file:/D:/");
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
return true;
}
}

本文介绍了一种将XML文件通过XSLT转换成HTML的方法,并进一步利用Flying Saucer工具将其转化为PDF格式。此过程涉及XML样式表的设计及如何正确设置CSS以确保最终PDF文档的质量。

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



