image in PDF from Apache FOP 1.1 with Java

家住魔仙堡 提交于 2019-12-24 14:40:23

问题


i have a strange problem. im creating a PDF document from Java with apache FOP 1.1. If i create this file from eclipse, it works well. If i export my Programm as a jar file, it doesn work.

the XSL-FO File is:

<?xml version="1.0" encoding="ISO-8859-1"?>

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="/">

    <fo:root font-family="Arial">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="A4-portrait"
                page-height="29.7cm" page-width="21.0cm" margin="2cm">
                <fo:region-body region-name="xsl-region-body"
                    margin-bottom=".5in" />
                <fo:region-before region-name="xsl-region-before"
                    extent=".4in" />
                <fo:region-after region-name="xsl-region-after"
                    extent=".4in" />
            </fo:simple-page-master>

        </fo:layout-master-set>
        <fo:page-sequence master-reference="A4-portrait">

            <fo:static-content flow-name="xsl-region-before">
                <fo:block text-align="right">
                    **<fo:external-graphic width="150pt" height="150pt" content-width="150pt" content-height="150pt" src="C:/Users/user/Desktop/logo.jpg"/>**
                </fo:block>

            </fo:static-content>


            <fo:static-content flow-name="xsl-region-after">
                <fo:table table-layout="fixed" width="170mm">

                    <fo:table-column column-width="67mm" />
                    <fo:table-column column-width="67mm" />
                    <fo:table-column column-width="33mm" />
                    <fo:table-body start-indent="0mm" end-indent="0mm">


                        <fo:table-row>


                            <fo:table-cell margin-left="2mm">

                                <fo:block font-size="6pt">
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    <xsl:value-of select="<vaules>" />&#8239;<xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                            </fo:table-cell>

                            <fo:table-cell margin-left="2mm">

                                <fo:block font-size="6pt">
                                    Telefon:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    Fax:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    Internet:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    E-Mail:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                            </fo:table-cell>

                            <fo:table-cell margin-left="2mm">

                                <fo:block font-size="6pt">
                                    IBAN:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    BIC:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    Bank:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                                <fo:block font-size="6pt">
                                    USTNr.:
                                    <xsl:value-of select="<vaules>" />
                                </fo:block>
                            </fo:table-cell>

                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:static-content>
            <fo:flow flow-name="xsl-region-body">


                <fo:block>
                    <fo:leader />
                </fo:block>

                <fo:block>
                    <fo:leader />
                </fo:block>

                <fo:block>
                    <fo:leader />
                </fo:block>

                <fo:block>
                    <fo:leader />
                </fo:block>

                <fo:block>
                    <fo:leader />
                </fo:block>

                <fo:block font-size="8pt">
                    <xsl:value-of select="<vaules>" />
                    ,
                    <xsl:value-of select="<vaules>" />
                    ,
                    <xsl:value-of select="<vaules>" />&#8239;<xsl:value-of select="<vaules>" />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>


                <fo:block font-size="12pt">
                    <xsl:value-of select="<vaules>" />
                </fo:block>
                <fo:block>
                    <xsl:value-of select="<vaules>" />&#8239;<xsl:value-of select="<vaules>" />
                </fo:block>
                <fo:block>
                    <xsl:value-of select="<vaules>" />
                </fo:block>
                <fo:block>
                    <xsl:value-of select="<vaules>" />&#8239;<xsl:value-of select="<vaules>" />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block text-align="right">
                    <xsl:value-of select="<vaules>" />,<xsl:value-of select=<vaules>" />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block text-align="right">
                    **<fo:external-graphic src="PDF/barcode.png"/>**
                </fo:block>
                <fo:block text-align="left" font-size="14pt">
                    Rechnung #<xsl:value-of select="<vaules>" />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <fo:block>
                    <fo:leader />
                </fo:block>
                <xsl:choose>
                    <xsl:when test="<vaules>">
                        <fo:block font-size="12pt">
                            Sehr geehrter
                            <xsl:value-of select="<vaules>" />&#8239;<xsl:value-of select="<vaules>" />,
                        </fo:block>
                    </xsl:when>
                    <xsl:when test="<vaules>">
                        <fo:block font-size="12pt">
                            Sehr geehrte
                            <xsl:value-of select="<vaules>" />&#8239;<xsl:value-of select="<vaules>" />,
                        </fo:block>
                    </xsl:when>
                    <xsl:otherwise>
                        <fo:block font-size="12pt">Nichts</fo:block>
                    </xsl:otherwise>
                </xsl:choose>
                <fo:block>

... some more text.

in the terminal it brings me the following error:

    Sep 20, 2014 10:39:08 AM org.apache.fop.events.LoggingEventListener processEvent

**SCHWERWIEGEND: Image not available. URI: C:/Users/user/Desktop/logo.jpg. Reason:
 org.apache.xmlgraphics.image.loader.ImageException: The file format is not supp
orted. No ImagePreloader found for C:/Users/user/Desktop/logo.jpg (See position
13:134)**
org.apache.xmlgraphics.image.loader.ImageException: The file format is not suppo
rted. No ImagePreloader found for C:/Users/niko/Desktop/logo.jpg
        at org.apache.xmlgraphics.image.loader.ImageManager.preloadImage(ImageMa
nager.java:180)
        at org.apache.xmlgraphics.image.loader.cache.ImageCache.needImageInfo(Im
ageCache.java:128)
        at org.apache.xmlgraphics.image.loader.ImageManager.getImageInfo(ImageMa
nager.java:122)
        at org.apache.fop.fo.flow.ExternalGraphic.bind(ExternalGraphic.java:81)
        at org.apache.fop.fo.FObj.processNode(FObj.java:124)
        at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuil
der.java:280)
        at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:175)
        at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(Tra
nsformerIdentityImpl.java:1072)
        at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Sour
ce)
        at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unkn
own Source)
        at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unkn
own Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContent
Dispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Un
known 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.xalan.transformer.TransformerIdentityImpl.transform(Transf
ormerIdentityImpl.java:484)
        at pdfcreator_paket.CreatePDF.convertXML2FO(CreatePDF.java:104)
        at side_pakets.XMLParser.printRechnung(XMLParser.java:447)
        at auftrag_paket.Auftragsverwaltung.btnDruckenClick(Auftragsverwaltung.j
ava:691)
        at auftrag_paket.Auftragsverwaltung.access$3(Auftragsverwaltung.java:571
)
        at auftrag_paket.Auftragsverwaltung$8.actionPerformed(Auftragsverwaltung
.java:1197)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$400(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

Sep 20, 2014 10:39:08 AM org.apache.fop.events.LoggingEventListener processEvent

**SCHWERWIEGEND: Image not available. URI: PDF/barcode.png. Reason: org.apache.xml
graphics.image.loader.ImageException: The file format is not supported. No Image
Preloader found for PDF/barcode.png (See position 109:45)**
**org.apache.xmlgraphics.image.loader.ImageException: The file format is not suppo
rted. No ImagePreloader found for PDF/barcode.png**
        at org.apache.xmlgraphics.image.loader.ImageManager.preloadImage(ImageMa
nager.java:180)
        at org.apache.xmlgraphics.image.loader.cache.ImageCache.needImageInfo(Im
ageCache.java:128)
        at org.apache.xmlgraphics.image.loader.ImageManager.getImageInfo(ImageMa
nager.java:122)
        at org.apache.fop.fo.flow.ExternalGraphic.bind(ExternalGraphic.java:81)
        at org.apache.fop.fo.FObj.processNode(FObj.java:124)
        at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuil
der.java:280)
        at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:175)
        at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(Tra
nsformerIdentityImpl.java:1072)
        at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Sour
ce)
        at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unkn
own Source)
        at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unkn
own Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContent
Dispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Un
known 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.xalan.transformer.TransformerIdentityImpl.transform(Transf
ormerIdentityImpl.java:484)
        at pdfcreator_paket.CreatePDF.convertXML2FO(CreatePDF.java:104)
        at side_pakets.XMLParser.printRechnung(XMLParser.java:447)
        at auftrag_paket.Auftragsverwaltung.btnDruckenClick(Auftragsverwaltung.j
ava:691)
        at auftrag_paket.Auftragsverwaltung.access$3(Auftragsverwaltung.java:571
)
        at auftrag_paket.Auftragsverwaltung$8.actionPerformed(Auftragsverwaltung
.java:1197)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$400(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

Sep 20, 2014 10:39:08 AM org.apache.fop.events.LoggingEventListener processEvent

WARNUNG: Content overflows the viewport of the fo:region-before on page 1 in blo
ck-progression direction by 126864 millipoints. (See position 11:50)
Sep 20, 2014 10:39:08 AM org.apache.fop.events.LoggingEventListener processEvent

can anyone help me?

Sincerely


回答1:


It looks like the program is struggling to resolve the image file paths when run as a jar. It's worth remembering the src attribute points to a URI rather than a file path and, depending on its structure, will be resolved differently when run directly rather than from an executable jar. The first thing I would try is to change the file path to something like this:

src="file:///C:/Users/user/Desktop/logo.jpg"

or this:

src='url("file:///C:/Users/user/Desktop/logo.jpg")'

Are you able to arrange your files so you can use relative paths instead? Absolute paths can be quite brittle to maintain. You may also want to consider including all the XSL and image files in the jar file itself. There's a nice explanation of the ins and outs of this approach here.




回答2:


I faced the same problem. What fixed it for me was to set the base path for your FopUserAgent object so it knows the path to your image. Then you can specify your image relative to this path.

// images are located in or in subfolders of baseDir
String baseDir = /some/path/to/includes/
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
foUserAgent.setBaseURL(baseDir);

Furthermore, in order to make it work with a jar I had to add the following imports:

import org.apache.xmlgraphics.image.loader.ImageManager;
import org.apache.xmlgraphics.image.loader.impl.DefaultImageContext;
/* Added this line to my code to get these imports. Not using it at all */
ImageManager imageManager = new ImageManager(new DefaultImageContext());

The reason was that apparently the fop.jar uses the same names for its settings as does the xmlgraphics-commons jar (which handles the images). So in order to not have fop.jar override the settings xmlgraphics I had to use said imports.


Unfortunately, this only seems to work on my machine. Testing the jar on other PCs causes the same error again. Maybe you have more luck. If so let me know. (Running Debian 8, OpenJDK7, FOP 1.1)



来源:https://stackoverflow.com/questions/25946952/image-in-pdf-from-apache-fop-1-1-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!