Java Apache FOP transformation.transform需要花费大量时间
我们使用基于模板的方法生成PDF文件,为此,我们使用velocity作为模板引擎,使用ApacheFop作为PDF引擎 我们需要生成可以包含0.2M表行的PDF。 下面是我们编写的代码。MyBean是JavaBean类,它将有0.2行Java Apache FOP transformation.transform需要花费大量时间,java,pdf,Java,Pdf,我们使用基于模板的方法生成PDF文件,为此,我们使用velocity作为模板引擎,使用ApacheFop作为PDF引擎 我们需要生成可以包含0.2M表行的PDF。 下面是我们编写的代码。MyBean是JavaBean类,它将有0.2行 velocityContext.put("MyBean", myBean); final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); fileWriter writer1 = new
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>
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
因此,我不知道如何实现0.2M的里程碑,因为下载20k条记录需要1分钟以上的时间
顺便说一句,我们是一个网络应用程序,用户可以下载0.2百万条记录
甚至我在一个文件中转储velcoity输出,发现大小大约为150MB。可能是因为哪个变压器占用了这么多时间
有没有一种方法,我们可以切换pasring和validation,直接转到FOP生成pdf