0

I am trying to generate a pdf using apache-fop from the following command.

C:\fop-1.0-bin\fop-1.0>fop -xml sample1.xml -xsl xsl-fo.xsl -pdf sample2.pdf

My xml looks like this

<?xml version="1.0" encoding="iso-8859-1"?>
<service>
    <serviceId>11111</serviceId>
    <openDate>2012-02-16</openDate>
    <closeDate>2012-02-16</closeDate>
    <description>INSPECT MACHINE</description>
    <repairCost>$266.76</repairCost>
</service>

My xsl-fo xslt file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    >
    <xsl:output encoding="iso-8859-1" />
    <xsl:template match ="Service">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="Service">
                    <fo:region-body></fo:region-body>
                </fo:simple-page-master>
            </fo:layout-master-set>

            <fo:page-sequence master-reference="Service">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block>
                        <fo:table>
                            <fo:table-body>
                                <fo:table-row>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Service Id</fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Open Date</fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Close Date</fo:block>
                                    </fo:table-cell>                                    
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Order Status</fo:block>
                                    </fo:table-cell>                                    
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Description</fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Repair Cost</fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <xsl:for-each select="./servicetype">
                                    <fo:table-row>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="serviceId" /></fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="openDate" /></fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="closeDate" /></fo:block>
                                        </fo:table-cell>                                

                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="description" /></fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="repairCost" /></fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </xsl:for-each>
                            </fo:table-body>
                        </fo:table>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

I am getting error. Can someone point out my error in the code. I am following this example http://www.codeproject.com/Articles/37663/PDF-Generation-using-XSLFO-and-FOP

SEVERE: org.apache.fop.fo.ValidationException: Document is empty (something migh
t be wrong with your XSLT stylesheet).
Dec 3, 2012 4:27:27 PM org.apache.fop.cli.Main startFOP
SEVERE: Exception
javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException:
 Document is empty (something might be wrong with your XSLT stylesheet).
        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: javax.xml.transform.TransformerException: org.apache.fop.fo.Validatio
nException: Document is empty (something might be wrong with your XSLT styleshee
t).
        at org.apache.xalan.transformer.TransformerImpl.transformNode(Transforme
rImpl.java:1430)
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp
l.java:709)
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp
l.java:1284)
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp
l.java:1262)
        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)
        ... 3 more
Caused by: org.apache.fop.fo.ValidationException: Document is empty (something m
ight be wrong with your XSLT stylesheet).
        at org.apache.fop.events.ValidationExceptionFactory.createException(Vali
dationExceptionFactory.java:38)
        at org.apache.fop.events.EventExceptionManager.throwException(EventExcep
tionManager.java:54)
        at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBr
oadcaster.java:175)
        at $Proxy0.emptyDocument(Unknown Source)
        at org.apache.fop.fo.FOTreeBuilder.endDocument(FOTreeBuilder.java:158)
        at org.apache.xml.serializer.ToXMLSAXHandler.endDocument(ToXMLSAXHandler
.java:181)
        at org.apache.xalan.transformer.TransformerImpl.transformNode(Transforme
rImpl.java:1378)
        ... 7 more
user525146
  • 3,918
  • 14
  • 60
  • 103

3 Answers3

3

try changing the xpath in the foreach

<xsl:for-each select="./service">

xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<services>
    <service>
        <serviceId>11111</serviceId>
        <openDate>2012-02-16</openDate>
        <closeDate>2012-02-16</closeDate>
        <orderStatus>Status2</orderStatus>
        <description>INSPECT MACHINE</description>
        <repairCost>$266.76</repairCost>
    </service>
    <service>
        <serviceId>11111</serviceId>
        <openDate>2012-02-16</openDate>
        <closeDate>2012-02-16</closeDate>
        <orderStatus>Status1</orderStatus>
        <description>INSPECT MACHINE</description>
        <repairCost>$266.76</repairCost>
    </service>
</services>

xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output encoding="iso-8859-1" />
<xsl:template match ="services">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="service">
                <fo:region-body></fo:region-body>
            </fo:simple-page-master>
        </fo:layout-master-set>

        <fo:page-sequence master-reference="service">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    <fo:table>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Service Id</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Open Date</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Close Date</fo:block>
                                </fo:table-cell>                                    
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Order Status</fo:block>
                                </fo:table-cell>                                    
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Description</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Repair Cost</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <xsl:for-each select="./service">
                                <fo:table-row>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="serviceId" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="openDate" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="closeDate" /></fo:block>
                                    </fo:table-cell>                                
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="orderStatus" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="description" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="repairCost" /></fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:for-each>
                        </fo:table-body>
                    </fo:table>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
</xsl:stylesheet>
  • It's important to get the template match as and the for each as . also the xml file needs to have a root called services. I have tried this myself and it generates a pdf –  Dec 03 '12 at 23:42
  • I have copied the code you have put in the ans, checked the tags and tried, it failed – user525146 Dec 04 '12 at 01:53
  • Did you use the same xml? matches the root of the xml. loops over each service element. I've tried it a few times now and it generates a pdf each time, though I have noticed that the column names and the column values don't quite match up. You need to add a value for the order status columm –  Dec 04 '12 at 02:48
  • Thank you very much, I deleted the old files and created new ones then it worked but gave some warnings `Dec 3, 2012 8:43:40 PM org.apache.fop.events.LoggingEventListener processEvent WARNING: The following feature isn't implemented by Apache FOP, yet: table-layou t="auto" (on fo:table) (No context info available) Dec 3, 2012 8:43:41 PM org.apache.fop.events.LoggingEventListener processEvent WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,40 0".` – user525146 Dec 04 '12 at 05:21
2

Could it be as simple as a problem with case? Your sample XML uses <service> as the root tag but you have <xsl:template match="Service"> (capital S).

dbrobins
  • 499
  • 1
  • 3
  • 9
0

FOP doesn't support the automatic table layout and instead uses the fixed table layout

The auto layout is not supported by the FOP , as the page has a static width [A4, A3...] the auto mode is supported only in html rendring as the size of the page could change (depending on the resolution, resizing the window...) see :

http://mail-archives.apache.org/mod_mbox/xmlgraphics-fop-dev/200510.mbox/%3C20051005124250.8A19A21E@ajax.apache.org%3E

http://wiki.apache.org/xmlgraphics-fop/Troubleshooting/CommonLogMessage

Note : To avoid the warning you should specifie table-layout="fixed" , and use width="100%" and the "*" when defining columns widht

yayaman
  • 53
  • 5