无法使用Apache FOP生成PDF

时间:2013-07-09 12:52:20

标签: xslt xsl-fo apache-fop

我正在尝试使用Apache FOP使用XML数据和XSL样式表创建PDF但我不断收到以下错误

  

org.apache.fop.apps.FOPException:   org.apache.fop.fo.ValidationException:   “{http://www.w3.org/1999/XSL/Format}阻止”不是有效的孩子   “FO:根”! (没有上下文信息)   javax.xml.transform.TransformerException中:   org.apache.fop.fo.ValidationException:   “{http://www.w3.org/1999/XSL/Format}阻止”不是有效的孩子   “FO:根”! (没有上下文信息)

奇怪的是,当我在我的本地Windows机器上测试它时它工作正常但是当我在Debian服务器上尝试做同样的事情时我得到了这个错误。

我在命令行上执行以下操作

fop -c fop_config.xml -xml /tmp/fop4IwJKZ -xsl stylesheet.xsl -pdf pdf.pdf

以下是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<report>
    <heading>Heading</heading>
    <mailersent>18/06/2013 14:23</mailersent>
    <reportgenerated>18/06/2013 17:26</reportgenerated>
    <portrait>
        <title>Summary</title>
        <graph>graphs/graph1.png</graph>
        <stats>
            <stat>
                <value>16.67%</value>
                <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor</text>
            </stat>
            <stat>
                <value>2.25%</value>
                <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor</text>
            </stat>
            <stat>
                <value>13.49%</value>
                <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor through</text>
            </stat>
            <stat>
                <value>8.99%</value>
                <text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor</text>
            </stat>
        </stats>
        </portrait>
</report>

样式表:

<?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:output method="xml" indent="yes"/>

<xsl:template match="node()|@*">
    <xsl:apply-templates select="node()|@*"/>
</xsl:template>

<xsl:template match="/report">
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="portraitLayout" page-height="29.7cm" page-width="21.0cm" margin="1cm">
                <fo:region-body margin-bottom="1cm"/>
                <fo:region-after extent="1cm"/>
            </fo:simple-page-master>
            <fo:simple-page-master master-name="landscapeLayout" page-height="21cm" page-width="30cm" margin="1cm">
                <fo:region-body margin-bottom="1cm"/>
                <fo:region-after extent="1cm"/>
            </fo:simple-page-master>
        </fo:layout-master-set>

        <fo:page-sequence master-reference="portraitLayout">
            <fo:flow flow-name="xsl-region-body">
                <xsl:call-template name="heading"/>
            </fo:flow>
        </fo:page-sequence>

        <fo:page-sequence master-reference="portraitLayout">
            <fo:flow flow-name="xsl-region-body">
                <xsl:call-template name="genTOC"/>
            </fo:flow>
        </fo:page-sequence>
        <xsl:apply-templates/>
    </fo:root>
</xsl:template>

<xsl:template name="genTOC">
    <fo:block break-before='page'>
        <fo:block margin-bottom="1cm" font-size="16pt" font-weight="bold" text-align="center">Contents</fo:block>
        <xsl:for-each select="//portrait|//landscape">
            <fo:block text-align-last="justify">
                <fo:basic-link internal-destination="{generate-id(.)}">
                    <xsl:value-of select="title" />
                    <fo:leader leader-pattern="dots" />
                    <fo:page-number-citation ref-id="{generate-id(.)}" />
                </fo:basic-link>
            </fo:block>
        </xsl:for-each>
    </fo:block>
</xsl:template>

<xsl:template match="portrait">
    <fo:page-sequence master-reference="portraitLayout" id="{generate-id(.)}">
        <fo:static-content flow-name="xsl-region-after">
            <fo:block font-size="8pt" text-align="center" border-top-color="#000000" border-top-style="solid" border-top-width=".3mm" padding-top="0.2cm">
                Page <fo:page-number/> of 9
            </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
            <xsl:apply-templates/>
        </fo:flow>
    </fo:page-sequence>
</xsl:template>

<xsl:template match="landscape">
    <fo:page-sequence master-reference="landscapeLayout" id="{generate-id(.)}">
        <fo:static-content flow-name="xsl-region-after">
            <fo:block font-size="8pt" text-align="center" border-top-color="#000000" border-top-style="solid" border-top-width=".3mm" padding-top="0.2cm">
                Page <fo:page-number/> of 9
            </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
            <xsl:apply-templates/>
        </fo:flow>
    </fo:page-sequence>
</xsl:template>

<xsl:template name="heading">
    <fo:block margin-top="10cm" font-size="21pt" font-weight="bold" text-align="center">
        <xsl:value-of select="heading"/>
    </fo:block>
    <fo:block margin-top="0.3cm" font-size="16pt" text-align="center">
        <xsl:text>Mailer sent: </xsl:text>
        <xsl:value-of select="mailersent"/>
    </fo:block>
    <fo:block margin-top="0.3cm" font-size="16pt" text-align="center">
        <xsl:text>Report generated: </xsl:text>
        <xsl:value-of select="reportgenerated"/>
    </fo:block>
</xsl:template>

<xsl:template match="title">
    <fo:block margin-bottom="0.5cm" border-bottom-color="#000000" border-bottom-style="solid" border-bottom-width=".5mm" font-size="20pt">
        <xsl:value-of select="."/>
    </fo:block>
</xsl:template>

<xsl:template match="graph">
    <fo:block margin-bottom="0.5cm" border-color="#d8d8d8" border-style="solid" border-width=".3mm" font-size="10pt">
        <xsl:variable name="graphimage">
            <xsl:value-of select="."/>
        </xsl:variable>
        <fo:external-graphic src="{$graphimage}" content-height="scale-to-fit" content-width="16cm" scaling="non-uniform"/>
    </fo:block>
</xsl:template>

<xsl:template match="svggraph">
    <fo:block margin-bottom="0.5cm" border-color="#d8d8d8" border-style="solid" border-width=".3mm" font-size="10pt">
        <fo:instream-foreign-object xmlns:svg="http://www.w3.org/2000/svg" content-width="19cm" content-height="5cm">
        <svg:svg width="25" height="25">
        <svg:g style="fill:red; stroke:#000000">
        <svg:rect x="0" y="0" width="15" height="15"/>
        <svg:rect x="5" y="5" width="15" height="15"/>
        </svg:g>
        </svg:svg>
        </fo:instream-foreign-object>
    </fo:block>
</xsl:template>

<xsl:template match="stats">
    <xsl:for-each select="stat">
        <fo:block margin-bottom="0.5cm" font-size="10pt">
            <fo:inline font-weight="bold" font-size="12pt">
                <xsl:value-of select="value"/>
            </fo:inline>
            <xsl:text> </xsl:text>
            <xsl:value-of select="text"/>
        </fo:block>
    </xsl:for-each>
</xsl:template>

<xsl:template match="table">
    <fo:block>
        <fo:table margin-bottom="0.5cm" border-collapse="separate" border-color="#d8d8d8" border-style="solid" border-width=".3mm" table-layout="fixed" width="100%" font-size="10pt">

            <xsl:for-each select="thead/tr/th">
                <xsl:choose>
                    <xsl:when test="width">
                        <xsl:variable name="columnwidth">
                            <xsl:value-of select="width" />
                        </xsl:variable>
                        <fo:table-column column-width="{$columnwidth}"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <fo:table-column/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>

            <fo:table-header background-color="#ededed">                    
                <xsl:for-each select="thead/tr/th">
                    <fo:table-cell padding-top="5pt" padding-bottom="5pt" padding-left="3pt" padding-right="3pt">
                        <fo:block font-weight="bold">
                            <xsl:value-of select="heading"/>
                        </fo:block>
                    </fo:table-cell>
                </xsl:for-each>                 
            </fo:table-header>
            <fo:table-body>                 
                <xsl:for-each select="tbody/tr">                        
                    <xsl:variable name="row-background">
                        <xsl:choose>
                            <xsl:when test="position() mod 2 = 0">#f8f8f8</xsl:when>
                            <xsl:otherwise>#ffffff</xsl:otherwise>
                        </xsl:choose>
                    </xsl:variable>                     
                    <fo:table-row background-color="{$row-background}">                         
                        <xsl:for-each select="td">
                            <fo:table-cell padding-top="5pt" padding-bottom="5pt" padding-left="3pt" padding-right="3pt">
                                <fo:block>
                                    <xsl:value-of select="."/>
                                </fo:block>
                            </fo:table-cell>
                        </xsl:for-each>                         
                    </fo:table-row>                     
                </xsl:for-each>
            </fo:table-body>
        </fo:table>
    </fo:block>
</xsl:template>

<xsl:template match="text">
    <fo:block font-size="10pt">
        <xsl:value-of select="."/>
    </fo:block>
</xsl:template>

</xsl:stylesheet>

更新:我已在两台机器上检查了XSLT进程,它们是相同的。我还将两台机器上的XML + XSL转换为XLS:FO,两个文件都是相同的。

1 个答案:

答案 0 :(得分:1)

我使用你的XML和XSL文件

C:\Users\User>cd Documents
C:\Users\User\Documents>cd fop-1.0
C:\Users\User\Documents\fop-1.0>fop -xsl stovfl17548663.xsl -xml stovfl17548663.xml -pdf stovfl17548663.pdf

我收到以下消息

C:\Users\User\Documents\fop-1.0>fop -xsl stovfl17548663.xsl -xml stovfl17548663.xml -pdf stovfl17548663.pdf
Sep 26, 2013 12:09:47 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
Sep 26, 2013 12:09:47 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with    "ZapfDingbats,normal,400".
Sep 26, 2013 12:09:48 PM org.apache.fop.events.LoggingEventListener processEvent
SEVERE: Image not found. URI: graphs/graph1.png. (No context info available)
Sep 26, 2013 12:09:48 PM org.apache.fop.events.LoggingEventListener processEvent
SEVERE: Image not found. URI: graphs/graph1.png. (No context info available)
Sep 26, 2013 12:09:48 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: 1 link target could not be fully resolved and now point to the top of the page or is dysfunctional.

The resulting PDF