Can't generate PDF with Apache FOP

2019-07-21 23:16发布

问题:

I am trying to use Apache FOP to create a PDF using XML data and a XSL stylesheet but I keep getting the following error

org.apache.fop.apps.FOPException: org.apache.fop.fo.ValidationException: "{http://www.w3.org/1999/XSL/Format}block" is not a valid child of "fo:root"! (No context info available) javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: "{http://www.w3.org/1999/XSL/Format}block" is not a valid child of "fo:root"! (No context info available)

The strange thing is that it works fine when I was testing it on my local Windows machine but when I tried to do the same on Debian server I was getting this error.

I am doing the following on the command line

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

Below is my 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>

Stylesheet:

<?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>

UPDATE: I have checked the XSLT Process on both machines and they are the same. I have also converted the XML + XSL on both machines to XLS:FO and both files are the same.

回答1:

I use your XML and XSL file with

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

And I get the following messages

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.