当前位置: 首页 > 知识库问答 >
问题:

来自Apache FOP 1.1的PDF格式图像,带有Java

邵正雅
2023-03-14

我有一个奇怪的问题。我用apache FOP 1.1从Java创建了一个PDF文档。如果我从eclipse创建这个文件,它工作得很好。如果我把我程序导出为jar文件,它不能工作。

XSL-FO文件是:

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

…还有一些文字。

在终端中,它会给我带来以下错误:

    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

有人能帮我吗?

真诚地

共有2个答案

华泽语
2023-03-14

我也面临同样的问题。对我来说,修复它的方法是为您的FopUserAgent对象设置基本路径,这样它就知道您的图像的路径。然后,您可以指定相对于此路径的图像。

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

此外,为了使它与罐子一起工作,我不得不添加以下导入:

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());

原因很明显是fop。jar使用与xmlgraphics commons jar(处理图像)相同的设置名称。所以为了没有fop。jar覆盖了我必须使用的设置xmlgraphics。

不幸的是,这只适用于我的机器。在其他PC上测试jar会再次导致相同的错误。也许你运气更好。如果是这样,请告诉我。(运行Debian 8,OpenJDK7,FOP 1.1)

耿学义
2023-03-14

看起来程序在作为jar运行时很难解析图像文件路径。值得记住的是,src属性指向URI而不是文件路径,并且,根据其结构,直接运行而不是从可执行jar运行时解析方式不同。我要尝试的第一件事是将文件路径更改为如下内容:

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

或者这个:

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

你能安排你的文件,这样你就可以使用相对路径代替了吗?绝对路径可能很难维护。您可能还想考虑在jar文件本身中包含所有XSL和图像文件。这里有一个关于这种方法来龙去脉的很好的解释。

 类似资料:
  • 我试图生成一个. docx从超文本标记语言字符串与docx4j在Java,其中包含Base64图像里面。目前,我能够生成Word并下载它(在一个Vaadin项目内),但图像没有正确插入。但是,如果我把超文本标记语言代码放入index.html页面,它们确实显示正确! 我使用的代码是这样的: 有没有什么特别的工作应该做,而我没有做? 编辑: 我现在可以插入图片了!用以下代码替换行下方和行上方的代码:

  • 我有一个pdf文件(附件)。我的目标是使用pdfbox将pdf转换为图像(与在windows中使用剪切工具相同)。pdf有各种形状和文本。 我使用的代码如下: 我如何让pdfbox采取像直接快照图像的东西? 另外,我注意到png的图像质量不太好,有没有办法提高生成图像的分辨率? 编辑:这是pdf(见第68页)https://drive.google.com/file/d/0B0ZiP71EQHz2

  • 我正在尝试建立一个带有外部文件链接的PDF文件。 我使用的规范是https://www.adobe.com/content/dam/adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf 在第348页上有一个具有远程加载的替代图像的图像示例。当我使用文档中的示例创建文档时,阅读器(使用acrobat reader XI)不会获取图像。没有错误消息,但没

  • 问题内容: 我发现调整图像大小以使其具有响应能力很棘手。 我正在开发一个php应用程序,以自动将网站转换为响应版本。我有点卡在图像上。 我已经成功将包装类添加到网站上的每个图像,并且可以很好地调整图像的大小。 我的问题在于图像自然小于窗口,例如徽标和图标。我不想调整这些大小。 我的代码当前会转换: 变成: 我在哪里使用以下CSS: 这将调整所有图像的大小,但是我只希望它调整超出页面宽度的图像的大小

  • 问题内容: 当我需要定制我的UIPageControl解决方案。 为新版本的swift稍微修改一下即可: 我的问题是,当您第一次启动应用程序时,我无法更改图片。仅在页面更改时更改。 在中,我添加了和,但没有结果。我可能做错了什么? 问题答案: Swift 3.0 …您知道是否可以接受声明的风险:“修改现有控件的子视图很脆弱”。 您将必须在viewDidAppear()中调用“ updateDots

  • 问题内容: 我想在Python / Django中编写包含以下部分的HTML邮件: HTML链接到logo.png logo.png应该在邮件用户代理中内联显示(不作为附件显示) info.pdf,应显示为附件 如果邮件用户代理无法显示HTML,则应显示的文本。 我关注了这篇博客文章。 结果: HTML和内嵌图片有效 但info.pdf文件的处理方式类似于内联logo.png,并且某些邮件用户代理