package com.cisetech.put.utils.fop;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
* FopReport
* @author bin.yin 2012/12/23
*/
public class FopReport {
private static Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG);
// 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;
}
public static void main(String[] args) {
long t0 = System.currentTimeMillis();
try {
StringBuffer buf = new StringBuffer();
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buf.append("<ItemListReport>");
buf.append(" <ReportHeader>");
buf.append(" <Title>附加条款</Title>");
buf.append(" <PartyA>上海信息技术有限公司B</PartyA>");
buf.append(" <PartyB>上海信息技术有限公司B</PartyB>");
buf.append(" </ReportHeader>");
buf.append(" <ReportBody>");
buf.append(" <Table>");
buf.append(" <TableRow>");
buf.append(" <ItemName>附加条款1</ItemName>");
buf.append(" <ItemTime>2012-12-23 09:03</ItemTime>");
buf.append(" </TableRow>");
buf.append(" <TableRow>");
buf.append(" <ItemName>上海信息技术有限公司附加条款1</ItemName>");
buf.append(" <ItemTime>2012-12-23 09:03</ItemTime>");
buf.append(" </TableRow>");
buf.append(" </Table>");
buf.append(" </ReportBody>");
buf.append(" <ReportFooter>");
buf.append(" <PrintDate>2012-12-12</PrintDate>");
buf.append(" <ReportNo>010123456789</ReportNo>");
buf.append(" </ReportFooter>");
buf.append("</ItemListReport>");
long t = System.currentTimeMillis();
ReportData data = FopReport.createReport("report\\sample\\Sample.xsl", buf.toString().getBytes("UTF-8"));
//data = FopReport.createReport("report\\sample\\Sample.xsl", "report\\sample\\Sample.xml");
long t1 = System.currentTimeMillis();
log.debug("time:" + (t1 - t));
FileOutputStream fos = new FileOutputStream("D:/sample.pdf");
fos.write(data.getData());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
log.debug("use time:" + (System.currentTimeMillis() - t0));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
fopdemo.rar (51个子文件)
fopdemo
src
com
cisetech
put
utils
fop
FopReport.java 5KB
ReportData.java 634B
.classpath 637B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 522B
.jsdtscope 496B
org.eclipse.jdt.core.prefs 395B
report
sample
Sample.xsl 6KB
Sample.xml 1KB
web
META-INF
MANIFEST.MF 39B
WEB-INF
classes
fonts
Code39Seven.ttf 7KB
arialb.xml 58KB
SimHei.xml 307KB
Code39Seven.xml 1KB
SimSun.ttc 10.01MB
arial.xml 58KB
arialb.ttf 278KB
SimSun.xml 309KB
arial.ttf 267KB
NSimSun.xml 309KB
SimHei.ttf 9.58MB
com
cisetech
put
utils
fop
FopReport.class 5KB
ReportData.class 923B
fop.xml 5KB
lib
batik-all-1.7.jar 3.16MB
fop.jar 2.94MB
commons-logging-1.0.4.jar 37KB
xalan-2.7.0.jar 2.94MB
xercesImpl-2.7.1.jar 1.15MB
xmlgraphics-commons-1.4.jar 556KB
servlet-api.jar 86KB
xml-apis-1.3.04.jar 190KB
avalon-framework-4.2.0.jar 80KB
serializer-2.7.0.jar 185KB
xml-apis-ext-1.3.04.jar 84KB
commons-io-1.3.1.jar 82KB
.project 1KB
conf
fonts
Code39Seven.ttf 7KB
arialb.xml 58KB
SimHei.xml 307KB
Code39Seven.xml 1KB
SimSun.ttc 10.01MB
arial.xml 58KB
arialb.ttf 278KB
SimSun.xml 309KB
arial.ttf 267KB
NSimSun.xml 309KB
SimHei.ttf 9.58MB
fop.xml 5KB
共 51 条
- 1
资源评论
- 源码怪2020-07-23干啥啥不行,骗人第一名
- tyl062017-11-15我根本就没下载,说是我下载过了,必须评论,................................
- qq_222222172017-12-22楼主写的很详细,而我解决问题了。
oudoud
- 粉丝: 11
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- note test a bc d e f g h
- MERGE.csv.zip
- 15白落梅:你是锦瑟我为流年:三毛的万水千山-3491776.mobi
- 微信小程序源码 实现 城市切换 demo 根据城市首字母排序城市 选择城市
- 12因为懂得 所以慈悲-张爱玲的倾城往事 (白落梅经典散文系列)-3321089.mobi
- 基于 C# 实现的串口温度监控软件
- 基于STM32F103RBT6单片机+SIM868自动收货机主控板硬件(原理图+PCB)工程文件.zip
- 微信小程序源码 车源宝 二手车交易平台 源码下载
- 专注于在线教育领域,打造更好的网课系统,网校系统,知识付费系统
- ADM487EARZ-REEL7 产品规格书pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功