xml + xslt => html => pdf

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    继上一篇:使用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;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值