<fo:external-graphic src=… > throws error

倾然丶 夕夏残阳落幕 提交于 2019-12-11 05:14:56

问题


I use fop 2.1 and I need to load an external graphic with an absolute path. I tried this:

    <fo:root xsl:use-attribute-sets="text-attributes">
        <fo:layout-master-set>
         ...

        <fo:page-sequence master-reference="master">
            <xsl:call-template name="pageHeader"/>
            <xsl:call-template name="pageFooter"/>

            <fo:flow flow-name="xsl-region-body">
                ...
            </fo:flow>
        </fo:page-sequence>

   ...

<xsl:template name="pageHeader">
    <fo:static-content flow-name="region-before-first">
        <fo:block>
           <fo:external-graphic src="url({concat('file:/',$logo-image)})" height="15mm" content-height="15mm"/>  
        </fo:block>
    </fo:static-content>
</xsl:template>

$logo-image contains the absolute path like C:/users/.../application/logo.gif

I get the following error:

PM org.apache.fop.events.LoggingEventListener processEvent
SEVERE: Invalid property value encountered in src="url(file:/C:/users/.../application/logo.gif)"  .... Invalid URI specified

Any idea?

I tried also to put ' around like "url('file:/C:/users/.../application/logo.gif')" and else at divers place. No success... :-(


As in some other stylesheets I do have external graphic that functions I tried also take the header into the normal flow ( => not static ). Not really the way to do for a header, but a try... :

    <fo:root xsl:use-attribute-sets="text-attributes">
        <fo:layout-master-set>
         ...

        <fo:page-sequence master-reference="master">
            <!--<xsl:call-template name="pageHeader"/>-->
            <xsl:call-template name="pageFooter"/>

            <fo:flow flow-name="xsl-region-body">
                <xsl:call-template name="pageHeader"/>
                ...
            </fo:flow>
        </fo:page-sequence>

   ...

<xsl:template name="pageHeader">
    <fo:block-container position="absolute" top="10mm" left="10mm">
        <fo:block>
           <fo:external-graphic src="url({concat('file:/',$logo-image)})" height="15mm" content-height="15mm"/>  
        </fo:block>
    </fo:block-container>
</xsl:template>

But: Independantly if I put

<fo:external-graphic src="url({concat('file:/',$logo-image)})" height="15mm" content-height="15mm"/> 

or

<fo:external-graphic src="url({concat('file:///',$logo-image)})" height="15mm" content-height="15mm"/> 

I get same stupid error:

.... Invalid URI specified

回答1:


First, simplify matters by getting it to work as a static FO file, independent of XSLT. You can double-back to the XSLT once you have a static target FO file working.

Realize that as @rene commented, you have to use full URI syntax, which may actually require yet another / for local access. (Think protocol://host/path where protocol is 'file' and host is empty here, yielding three slashes in a row):

file:///c:/users

If you still have trouble with this, do a browser walk up the directory tree to make sure that the files and its ancestor directories all exist.

Finally, note that ... isn't valid, in case that's a typo for .. and not a meta-notation about elided path components.




回答2:


Oups, I got!

It's a silly Windows slash / backslash thing...!

When getting the image path, I just had to convert the slashes:

string logoImg = BackslashToSlash(Path.GetFullPath(imageFile));
xsltArgumentList.Add("logo-image", logoImg);
...

    public static String BackslashToSlash(String inStr)
    {
        try
        {
            return inStr.Replace("\\", "/");
        }
        catch (Exception)
        {
            return "";
        }
    }

In the stylesheet (xsl) you can even the one-slash version after "file:" (!):

<fo:external-graphic src="url({concat('file:/',$logo-image)})" />

Nevertheless I think it actually should be three-slash one:

<fo:external-graphic src="url({concat('file:///',$logo-image)})"


来源:https://stackoverflow.com/questions/44972748/foexternal-graphic-src-throws-error

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