java xsl转换pdf_fop生成PDF支持中文(xml & xsl) - 飞猪 - ITeye博客

这篇博客介绍了如何利用Java的FOP库结合XSL模板将XML数据转换成支持中文的PDF文档。通过FopReport类的createReport方法,详细展示了转换过程,包括设置FopFactory、构造输出流、进行XSLT转换和FOP处理等步骤。此外,还提到了字体配置文件fop.xml的重要性和示例代码中创建XML内容的部分。
摘要由CSDN通过智能技术生成

2.FopReport.java

// Step 1: Construct a FopFactory

private static final FopFactory fopFactory = FopFactory.newInstance();

/**

* 根据xsl模板及xml数据文件生成pdf

* @param xsltFile xsl模板

* @param xmlFile xml数据文件

* @return ReportData

* @throws Exception

* @author bin.yin 2012/12/25

*/

public static ReportData createReport(String xsltFile, String xmlFile) throws Exception {

ReportData reportData = new ReportData();

reportData.setContentType("application/pdf");

fopFactory.setUserConfig("conf/fop.xml");

// Step 2: Set up output stream.

ByteArrayOutputStream out = new ByteArrayOutputStream();

try {

// Step 3: Construct fop with desired output format

Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

// Step 4: Setup XSLT using identity transformer

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltFile)));

// Step 5: Setup input and output for XSLT transformation

Source src = new StreamSource(new File(xmlFile));

// Source src = new StreamSource(new StringReader(myString));

// Step 6: Resulting SAX events (the generated FO) must be piped through to FOP

Result res = new SAXResult(fop.getDefaultHandler());

// Step 7: Start XSLT transformation and FOP processing

transformer.transform(src, res);

reportData.setData(out.toByteArray());

} catch(Exception e) {

throw e;

} finally {

out.close();

}

return reportData;

}

/**

* 根据xsl模板及xml字节数组生成pdf

* @param xsltFile xsl模板

* @param bXmlData xml字节数组 eg. StringBuffer buf = new StringBuffer(); buf.getBytes("UTF-8");

* @return ReportData

* @throws Exception

* @author bin.yin 2012/12/25

*/

public static ReportData createReport(String xsltFile, byte[] bXmlData) throws Exception {

ReportData reportData = new ReportData();

try {

// convert xml bytes to a temp file

File xmlFile = File.createTempFile("FOP", ".tmp");

FileOutputStream fos = new FileOutputStream(xmlFile);

fos.write(bXmlData);

fos.close();

reportData = createReport(xsltFile, xmlFile.getAbsolutePath());

// delete temp file

xmlFile.delete();

} catch (Exception e) {

throw e;

}

return reportData;

}

3.拼接xml文件

StringBuffer buf = new StringBuffer();

buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

buf.append("");

buf.append("");

buf.append("

附加条款");

buf.append("上海信息技术有限公司B");

buf.append("上海信息技术有限公司B");

buf.append("");

buf.append("");

buf.append("

buf.append("");

buf.append("附加条款1");

buf.append("2012-12-23 09:03");

buf.append("");

buf.append("");

buf.append("上海信息技术有限公司附加条款1");

buf.append("2012-12-23 09:03");

buf.append("");

buf.append("

");

buf.append("");

buf.append("");

buf.append("2012-12-12");

buf.append("010123456789");

buf.append("");

buf.append("");

4.生成PDF文档

ReportData data = FopReport.createReport("report\\sample\\Sample.xsl", buf.toString().getBytes("UTF-8"));

FileOutputStream fos = new FileOutputStream("D:/sample.pdf");

5.附字体配置fop.xml

6.效果图如下:

大小: 32.4 KB

大小: 59.8 KB

下载次数: 246

下载次数: 386

分享到:

2012-12-27 16:01

浏览 9345

评论

7 楼

qrqhuang

2017-11-01

感谢博主分享, 条理清晰,很赞。

稍加修改, FOP2.1 一样可以跑起来。

6 楼

qrqhuang

2017-11-01

shihengli2010 写道

.xsl文件需要我们自己编写么  好难啊

一般来说这种标记语言, 学习起来很快的。

后续你能用到的也就那么几种排版

5 楼

shihengli2010

2016-09-05

.xsl文件需要我们自己编写么  好难啊

4 楼

Nancy0

2016-01-04

为什么解压报错呢?很是费解。。。

3 楼

qq_28504151

2015-07-18

好东西!!!

2 楼

gg_goodgame

2013-12-11

可以直接跑起来,但是下载解压时有些麻烦

1 楼

asigh

2013-10-24

东西很好,不然在fop官方看英文实在头疼。博主强大。赞赞赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值