问题
我正在使用XSL-T(v1.0)和XSL-FO(Apache FOP 1.0)来生成PDF(A4)报告,该报告使用的是来自第三方软件(输出XML)中用户填写的表单中的数据。
表单包含用户可以根据需要填写的文本区域(无法控制内容大小)。然后,文本区域内容以XML node/text()
(<textArea> ** content: user input ** </textArea>
)的形式输出。为了保留用户输入的格式,我在块级使用linefeed-treatment = "preserve"
属性。
问题是,每当文本区域中填充有足够的数据以引起分页符时,在换行符发生在换行符上时,我就会使用nullPointerException
和Apache FOP 1.0。除了使用keep-together.within-page = "always"
作为解决方法外,我找不到任何解决方案(不是长期解决方案,因为当text()
超过一页时,我会丢失数据。
工具
要开发XSL样式表,我使用:
对于使用XSL样式表(卡在FOP 1.0和XSLT 1.0中)生成PDF的软件,我别无选择。
上下文
为了根据用户在表单中的输入来生成报告,该软件允许将XSL样式表链接到每种表单。然后,它使用Apache FOP 1.0和未知的XSLT引擎将XSL样式表应用于XML输出。我负责为每种表单构建那些XSL样式表,但我一直坚持那个问题。
下面是FOP输出的错误,并且XSL-FO(已修剪并完整)发出错误。
导致问题的XSL-FO的一部分
<fo:block space-before="5mm" text-align="justify">
<fo:block linefeed-treatment="preserve" keep-together.within-page="auto">
<fo:block>
<fo:inline/>
<fo:inline> TEST
END OF TEST </fo:inline>
</fo:block>
</fo:block>
</fo:block>
详细错误
Standard Error
Jun 29, 2018 9:02:59 AM org.apache.fop.cli.Main startFOP
SEVERE: Exception
org.apache.fop.apps.FOPException
java.lang.NullPointerException
at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:302)
at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130)
at org.apache.fop.cli.Main.startFOP(Main.java:174)
at org.apache.fop.cli.Main.main(Main.java:205)
Caused by: java.lang.NullPointerException
at org.apache.fop.layoutmgr.AbstractBreaker.doLayout(AbstractBreaker.java:434)
at org.apache.fop.layoutmgr.PageBreaker.doLayout(PageBreaker.java:85)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.activateLayout(PageSequenceLayoutManager.java:107)
at org.apache.fop.area.AreaTreeHandler.endPageSequence(AreaTreeHandler.java:238)
at org.apache.fop.fo.pagination.PageSequence.endOfNode(PageSequence.java:120)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:349)
at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:177)
at org.apache.xalan.transformer.TransformerIdentityImpl.endElement(TransformerIdentityImpl.java:1101)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484)
at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)
... 3 more
---------
java.lang.NullPointerException
at org.apache.fop.layoutmgr.AbstractBreaker.doLayout(AbstractBreaker.java:434)
at org.apache.fop.layoutmgr.PageBreaker.doLayout(PageBreaker.java:85)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.activateLayout(PageSequenceLayoutManager.java:107)
at org.apache.fop.area.AreaTreeHandler.endPageSequence(AreaTreeHandler.java:238)
at org.apache.fop.fo.pagination.PageSequence.endOfNode(PageSequence.java:120)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:349)
at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:177)
at org.apache.xalan.transformer.TransformerIdentityImpl.endElement(TransformerIdentityImpl.java:1101)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.xinclude.XIncludeHandler.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:484)
at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)
at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130)
at org.apache.fop.cli.Main.startFOP(Main.java:174)
at org.apache.fop.cli.Main.main(Main.java:205)
在完成FOP之前完成XSL-FO
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="coverPage" margin="5mm" page-height="297mm" page-width="210mm" reference-orientation="0" writing-mode="lr-tb">
<fo:region-body margin-top="25mm" margin-bottom="25mm" margin-left="45mm" margin-right="5mm"/>
<fo:region-after extent="20mm" precedence="true" display-align="before"/>
<fo:region-start extent="40mm"/>
</fo:simple-page-master>
<fo:simple-page-master master-name="repeatablePage" margin="5mm" page-height="297mm" page-width="210mm" reference-orientation="0" writing-mode="lr-tb">
<fo:region-body margin-top="30mm" margin-bottom="25mm" margin-left="20mm" margin-right="20mm"/>
<fo:region-after extent="20mm" precedence="true" display-align="before"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="pageSequence">
<fo:single-page-master-reference master-reference="coverPage"/>
<fo:repeatable-page-master-reference master-reference="repeatablePage" maximum-repeats="no-limit"/>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="pageSequence" font-family="sans-serif" font-weight="normal">
<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="right" font-size="10" color="#083E70">
Page <fo:page-number/> de <fo:page-number-citation ref-id="endDoc"/></fo:block>
<fo:block text-align="center" font-size="8" color="#083E70" space-before="8mm">
<fo:block>ADRESSE LINE 1</fo:block>
<fo:block>ADRESSE LINE 2</fo:block>
</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-start">
<fo:block>
<fo:external-graphic src="Logo.jpg" content-width="40mm" scaling="uniform"/>
</fo:block>
<fo:block-container color="#083E70" border-end-color="#083E70" border-end-width="1pt" border-end-style="solid" block-progression-dimension="235mm">
<fo:block/>
<fo:block space-before="10mm" space-after="10mm" font-weight="bold" text-decoration="underline"/>
</fo:block-container>
</fo:static-content>
<fo:flow flow-name="xsl-region-body" font-size="10pt">
<fo:block text-align="right" space-after="5mm">
<fo:inline>Bruxelles, le </fo:inline>
<fo:inline>19/06/2018</fo:inline>.
</fo:block>
<fo:block space-after="10mm" border="0pt solid black" padding="2mm 0">
<fo:table start-indent="3mm" space-after="10mm" table-layout="fixed" width="147mm">
<fo:table-column column-number="1" column-width="35mm"/>
<fo:table-column column-number="2" column-width="112mm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block>
<fo:inline font-weight="bold">
<fo:inline>Destinataire:</fo:inline>
</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell display-align="center">
<fo:block start-indent="40mm">Mara Kristen<fo:block/>5911 Maple Blvd<fo:block/>1400 NIVELLES<fo:block/>B<fo:block/></fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
<fo:table start-indent="3mm" table-layout="fixed" width="147mm">
<fo:table-column column-number="1" column-width="35mm"/>
<fo:table-column column-number="2" column-width="112mm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block>
<fo:inline font-weight="bold">
<fo:inline>Copie:</fo:inline>
</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:inline>Gold Denis</fo:inline>
<fo:inline font-style="italic"> (Merci Denis !, 3001 HEVERLEE, B)</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block/>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:inline>Gold Denis</fo:inline>
<fo:inline font-style="italic"> (Merci Denis !, 3001 HEVERLEE, B)</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block/>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:inline>Gold Denis</fo:inline>
<fo:inline font-style="italic"> (Merci Denis !, 3001 HEVERLEE, B)</fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:block>
<fo:block text-align="left" space-after="10mm">
Ref.: 002956347</fo:block>
<fo:block space-after="10mm" text-align="center" text-decoration="underline" font-weight="bold">
<fo:inline>Lettre de consultation - </fo:inline>Psychiatrie</fo:block>
<fo:block>
<fo:inline>Concerne: </fo:inline>
</fo:block>
<fo:block space-after="10mm" border="1pt solid black" padding="2mm 0">
<fo:table table-layout="fixed" start-indent="3mm" width="147mm">
<fo:table-column column-number="1" column-width="35mm"/>
<fo:table-column column-number="2" column-width="112mm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold">
<fo:inline>Nom, Prénom:</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Quigley, Jules</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold">
<fo:inline>Adresse:</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>9816 Main Street,
1402 LOOL,
B</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold">
<fo:inline>Nr. national:</fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>31035622101</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:block>
<fo:block text-align="left">
<fo:inline font-weight="bold" text-decoration="underline">
<fo:inline>Sujet:</fo:inline>
</fo:inline> <fo:inline>
<fo:inline>Exemple</fo:inline>
</fo:inline>
</fo:block>
<fo:block space-before="5mm" text-align="justify">
<fo:block linefeed-treatment="preserve" keep-together.within-page="auto">
<fo:block>
<fo:inline/>
<fo:inline>TEST
TEST</fo:inline>
</fo:block>
</fo:block>
</fo:block>
<fo:block keep-together.within-page="always">
<fo:block space-before="10mm" text-align="right">
<fo:inline text-decoration="underline">
<fo:inline>Signataire</fo:inline>
</fo:inline>
<fo:block/>
<fo:block>
<fo:inline>John SMITH</fo:inline>
</fo:block>
<fo:block>
<fo:inline>
<fo:inline>Nr. INAMI: </fo:inline>
</fo:inline>
<fo:inline>45632163140</fo:inline>
</fo:block>
</fo:block>
</fo:block>
<fo:block id="endDoc"/>
</fo:flow>
</fo:page-sequence>
</fo:root>
编辑
我已经找到一种方法,用@lfurini的建议用 <fo:block/>
替换所有换行符,但是它仍然会产生相同的NPE错误。
使用的代码如下所示:
<xsl:template name="gReplaceLineBreak">
<xsl:param name="pString"/>
<xsl:choose>
<xsl:when test="substring-before($pString,'
')">
<xsl:value-of select="substring-before($pString,'
')"/> <fo:block/>
<xsl:call-template name="gReplaceLineBreak">
<xsl:with-param name="pString" select="substring-after($pString,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
EDIT-2
好的,我终于找到了为什么上面的代码不起作用。有一个<fo:inline>
节点将所有<fo:block/>
包围起来。现在,我删除了它,它就像@lfurini所说的那样工作。非常感谢。
答案 0 :(得分:0)
这是一个bug,会影响每个 FOP版本,包括最新版本。
这是一个(非常丑陋的)解决方法:
linefeed-treatment="preserve" keep-together.within-page="auto"
,因为前者会产生错误,而后者会使内容超出页面限制 <fo:block/>
替换该块中的换行符(不间断空格,空块,仅用于可读性的换行符)例如,您示例中的相关块将变为:
<fo:block space-before="5mm" text-align="justify">
<fo:block> <fo:block/>
<fo:block> <fo:block/>
<fo:inline/> <fo:block/>
TEST <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
 <fo:block/>
END OF TEST <fo:block/>
</fo:block> <fo:block/>
</fo:block>
</fo:block>
这适用于FOP从1.1(无法从1.0进行测试,因为它甚至无法从下载页面获得)到最新的2.3。
如何实现::在您的XSLT样式表中,您可能应该使用特殊的mode
处理来自文本区域的用户内容,对于长度为0的文本节点,则产生{ {1}}。