使用带有FOP1.1 jar的JAVA在XML + XSLFO到PDF生成期间获取java堆空间错误

时间:2014-09-30 09:00:33

标签: java apache-fop

使用带有FOP1.1的JAVA将XML + XSLFO转换为PDF

此进程的已分配JVM空间为-Xmx2048M。

如果XML(CALLS标记)中的行数> 27000并将数据拆分为2列(左侧和右侧),则由于Java堆空间内存错误而无法处理PDF转换。


** XML结构是:**

    <MSISDN MDN="55555522">
    <CALLSUM call_type="Telephony" Calls="30000" Duration="25455" Charges="88882.00" >
    <CALLS OPN="1105505501" ZONE="AAA" DU="00:02:53(Sec)" BILLDUR="00:03:00(Sec)" AMT="6.00" />
    <CALLS OPN="1105505502" ZONE="AAA" DU="00:03:06(Sec)" BILLDUR="00:04:00(Sec)" AMT="8.00" />
    <CALLS OPN="1105505503" ZONE="AAA" DU="00:00:58(Sec)" BILLDUR="00:01:00(Sec)" AMT="2.00" />
    <CALLS OPN="1105505504" ZONE="AAA" DU="00:02:37(Sec)" BILLDUR="00:03:00(Sec)" AMT="6.00" />
    <CALLS OPN="1105505505" ZONE="AAA" DU="00:00:45(Sec)" BILLDUR="00:01:00(Sec)" AMT="2.00" />
    ............
    ............
    ............
Total lines of rows is 30000 in this block.    
    </CALLSUM>
    <CALLSUM call_type="SMS" Calls="35000" Duration="45555" Charges="78942.00" >
    <CALLS OPN="1105505401" ZONE="AAA" DU="00:02:53(Sec)" BILLDUR="00:03:00(Sec)" AMT="6.00" />
    <CALLS OPN="1105505402" ZONE="AAA" DU="00:03:06(Sec)" BILLDUR="00:04:00(Sec)" AMT="8.00" />
    <CALLS OPN="1105505403" ZONE="AAA" DU="00:00:58(Sec)" BILLDUR="00:01:00(Sec)" AMT="2.00" />
    <CALLS OPN="1105505404" ZONE="AAA" DU="00:02:37(Sec)" BILLDUR="00:03:00(Sec)" AMT="6.00" />
    <CALLS OPN="1105505405" ZONE="AAA" DU="00:00:45(Sec)" BILLDUR="00:01:00(Sec)" AMT="2.00" />
    ............
    ............
    ............
Total lines of rows is 35000 in this block.
    </CALLSUM>
    </MSISDN>

Java流程代码:

Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);  
TransformerFactory factory = TransformerFactory.newInstance();
Source xsl = new StreamSource(xslDir);
Templates template = factory.newTemplates(xsl);
Transformer transformer = template.newTransformer();
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);

XSL FO代码:

    <fo:table-body>
<fo:table-row>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt" padding-top="0.0pt" padding="0.0pt"
display-align="before" width="264.33pt" reference-orientation="0">
<fo:block-container reference-orientation="0">
<fo:block keep-together="auto">
<!-- GENERATE TABLE START-->
<xsl:if test="CALLS[position()mod 2 !=0]">
<fo:table table-layout="fixed" width="100%">
<fo:table-column column-width="56.05pt"/>
<fo:table-column column-width="18.0pt"/>
<fo:table-column column-width="71.0pt"/>
<fo:table-column column-width="45.52pt"/>
<fo:table-column column-width="48.37pt"/>
<fo:table-body>
<xsl:for-each select="CALLS[position()mod 2 !=0]">
<fo:table-row>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt" padding-top="0.0pt" padding="0.0pt" display-align="before" width="56.05pt" reference-orientation="0">

<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed" padding-bottom="0.0pt" start-indent="1.42pt" end-indent="0.0pt" padding-top="0.0pt" line-height="7.199999999999999pt" line-stacking-strategy="max-height" white-space-collapse="false" hyphenate="true" language="en" text-align="start"
keep-together="auto" color="#000000" font-family="Arial" font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@OPN"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="18.0pt" reference-orientation="0">

<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@ZONE"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="71.0pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@DT"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="48.37pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@BILLDUR"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell border-right-style="solid" border-right-width="0.57pt"
border-right-color="#000000" padding-bottom="0.0pt"
start-indent="0.0pt" end-indent="0.0pt" padding-top="0.0pt"
padding="0.0pt" display-align="before" width="25.47pt"
reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@AMT"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</xsl:if>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt" padding-top="0.0pt" padding="0.0pt"
display-align="before" width="257.21pt" reference-orientation="0">
<fo:block-container reference-orientation="0">
<fo:block keep-together="auto">
<!-- GENERATE TABLE START-->
<xsl:if test="CALLS[position()mod 2 =0]">
<fo:table table-layout="fixed" width="100%">
<fo:table-column column-width="54.06pt"/>
<fo:table-column column-width="18.0pt"/>
<fo:table-column column-width="69.0pt"/>
<fo:table-column column-width="42.68pt"/>
<fo:table-column column-width="45.0pt"/>
<fo:table-column column-width="25.0pt"/>
<fo:table-body>
<xsl:for-each select="CALLS[position()mod 2 =0]">
<fo:table-row>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="54.06pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="4.27pt" end-indent="0.0pt"
padding-top="0.0pt" line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="start"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@OPN"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="18.0pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@ZONE"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="42.68pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@DU"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="45.0pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@BILLDUR"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
<fo:table-cell padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt" display-align="before"
width="25.0pt" reference-orientation="0">
<fo:block-container>
<fo:block linefeed-treatment="preserve"
white-space-treatment="ignore-if-surrounding-linefeed"
padding-bottom="0.0pt" start-indent="0.0pt" end-indent="0.0pt"
padding-top="0.0pt" padding="0.0pt"
line-height="7.199999999999999pt"
line-stacking-strategy="max-height" white-space-collapse="false"
hyphenate="true" language="en" text-align="center"
keep-together="auto" color="#000000" font-family="Arial"
font-size="6.0pt" letter-spacing="normal" word-spacing="normal">
<fo:inline color="#000000" font-family="Arial" font-size="6.0pt">
<xsl:value-of disable-output-escaping="no" select="@AMT"/>
</fo:inline>
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</xsl:if>
</fo:block>
</fo:block-container>
</fo:table-cell>
</fo:table-row>
</fo:table-body>

1 个答案:

答案 0 :(得分:0)

自从我与FOP合作几年后,我记得堆空间问题。

我不知道应该生成多少页面。 我知道图像的使用会使内存使用量增加。

我在6.000页面上做了什么pdf解析器是自己渲染每个页面,最后组合所有页面。在遗留代码中,它需要保留16gb的堆空间(!)。 当我逐个渲染所有页面时,整个过程都没有通过512mb的堆空间。

我知道这不是一个完整的完整答案,但我希望它能为你指明方向。 如果需要,我可以搜索如何编写1-by-1渲染。