我正在尝试使用Apache FOP生成PDF文件。在此过程中,我从数据库中以对象列表的形式获取响应。我的方法的问题是,我的XSL文件可以在小型DataSet上正常工作,但无法生成PDF使用大型数据集时,我超出了OutOfMemoryError GC重载限制。关于这一点,我从512-4Gb增加了堆大小,但仍未解决问题。 有关更多信息,请在下面找到我的代码段
FOUserAgent userAgent = fopFactory.newFOUserAgent();
Fop fop = this.fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslFile));
Source src = new StreamSource(xmlFile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
XSL文件
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="ticketVo">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="page"
page-height="11in" page-width="8.5in" margin-left="0.45in"
margin-right="0.6in" margin-top="0.25in" margin-bottom="0.25in">
<fo:region-body region-name="xsl-region-body" />
<fo:region-before region-name="xsl-region-before" />
<fo:region-after region-name="xsl-region-after" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page"
initial-page-number="1" id="myseq">
<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="end" font-size="10pt" font-family="serif"
line-height="14pt">
Page
<fo:page-number />
of
<fo:page-number-citation-last ref-id="myseq" />
</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:block font-size="8pt">
<fo:table table-layout="fixed" width="100%"
border-collapse="collapse">
<fo:table-header border-bottom="1pt solid black">
<fo:table-row>
<fo:table-cell padding-left="3pt"
background-color="lightgrey" height="0.25in" display-align="center">
<fo:block background-color="lightgrey" margin-bottom="1mm"
font-weight="bold">T1</fo:block>
</fo:table-cell>
<fo:table-cell background-color="lightgrey"
height="0.25in" display-align="center">
<fo:block background-color="lightgrey" margin-bottom="1mm"
font-weight="bold">T2</fo:block>
</fo:table-cell>
<fo:table-cell background-color="lightgrey"
height="0.25in" display-align="center">
<fo:block background-color="lightgrey" margin-bottom="1mm"
font-weight="bold">T3</fo:block>
</fo:table-cell>
.
.
.
.
.
.
.
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:call-template name="cols"></xsl:call-template>
</fo:table-body>
</fo:table>
</fo:block>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="cols">
<xsl:for-each select="obj">
<fo:table-row>
<fo:table-cell display-align="center" padding="0pt">
<fo:block>
<xsl:value-of select="t1" />
</fo:block>
</fo:table-cell>
<fo:table-cell display-align="center" padding="0pt">
<fo:block text-align="right">
<xsl:value-of select="t2" />
</fo:block>
</fo:table-cell>
.
.
.
.
.
</fo:table-row>
</xsl:for-each>
</xsl:template>
</xsl: stylesheet>
和TicketVo用于xml映射
@XmlRootElement (name = "ticketVo")
public class TicketVo
{
@XmlElement
private List<Ticket> obj;
//setters and getters
}
机票模型
Class Ticket{
private String t1;
private String t2;
.
.
.
.
private String t100;
//setters & getters
}
注意::我从数据库中获得了数千条记录,并且我的表包含80多个列 apache-fop version = 2.3