我们正在使用基于模板的方法来生成PDF文件,为此我们使用velocity作为模板引擎,使用Apache FOP作为PDF引擎。
我们需要生成可包含0.2M表行的PDF。 以下是我们编写的代码。 MyBean是java bean类,它有0.2行
velocityContext.put("MyBean", myBean);
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
fileWriter writer1 = new FileWriter("C:\\test\\output.fo");
template.merge(velocityContext, writer1);
writer1.flush();
writer1.close();
我的速度模板就像这样
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="mybean"
page-height="29.7cm"
page-width="37cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="1cm"
margin-right="1cm">
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
#foreach($grid in $MyBean.grids)
#set($Griddata=$grid)
<fo:page-sequence master-reference="dashboard" initial-page-number="$count">
force-page-count="no-force" keep-with-previous.within-page="always"
keep-with-next.within-page="always">
<fo:flow flow-name="xsl-region-body"
keep-with-previous.within-page="always"
keep-with-next.within-page="always">
<fo:block keep-with-next.within-page="always"
keep-with-previous.within-page="always">
#parse('GridBody.fo')
</fo:block>
</fo:flow>
</fo:page-sequence>
现在在GridBody.fo中我正在创建表格,行和单元格。
所以速度没有问题,因为我正在从速度中正确地获得输出。
我的休息代码是这样的
final ByteArrayOutputStream outStream1 = new ByteArrayOutputStream()
FopFactory fopFactory;
FOUserAgent foUserAgent;
fopFactory=PDFExportContextInitializer.getFopFactoryInstance();
foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.setConserveMemoryPolicy(true);
final Fop fop = fopFactory.newFop(org.apache.xmlgraphics.util.MimeConstants.MIME_PDF, foUserAgent,outStream1);
final Source src = new StreamSource(new ByteArrayInputStream(outStream.toByteArray()));
src.setSystemId("123");
final Result res = new SAXResult(fop.getDefaultHandler());
TransformerFactory transFact = TransformerFactory.newInstance();
final Transformer transformer = transFact.newTransformer();
System.out.println("created transformer ");
transformer.setParameter("versionParam", "2.0");
transformer.transform(src, res);
StreamingOutput stream = new StreamingOutput() {
public void write(OutputStream output) throws IOException,
WebApplicationException {
try {
ObjectOutputStream oos = new ObjectOutputStream(output);
oos.writeObject(outStream1.toByteArray());
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new WebApplicationException(e);
} finally {
if(outStream1!=null) outStream1.close();
}
}
};
现在主要的问题是使用transformer.transform,因为处理20k记录需要将近70秒。 我的JVM设置是
-Xms4096m -Xmx4096m -Xmn256m
所以我不确定如何在下载20k记录的时间超过1分钟时达到0.2M的里程碑。
我们是一个WEB应用程序,用户可以下载0.2M记录。即使我拿走了velcoity输出的文件,发现大小约为150MB。可能是因为变压器需要花费很多时间。
我们是不是可以切换pasring和验证,只是直接转到FOP生成pdf。