无法使用Apache FOP生成PDF [英] Can't generate PDF with Apache FOP

查看:527
本文介绍了无法使用Apache FOP生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 }块不是有效的孩子 "fo:root"! (没有可用的上下文信息) javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: "{ http://www.w3.org/1999/XSL/Format }块不是有效的孩子 "fo:root"! (没有可用的上下文信息)

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)

奇怪的是,当我在本地Windows机器上对其进行测试时,它工作正常,但是当我尝试在Debian服务器上进行同样的操作时,却出现此错误.

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

下面是我的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,并且两个文件都相同.

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.

推荐答案

我将您的XML和XSL文件与

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

我收到以下消息

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.

这篇关于无法使用Apache FOP生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆