Images in XSL-FO in eXist-db

2019-09-06 03:14发布

问题:

In my former question I have touched a broader problem of paths and their recognizing inside an eXist-db app.

At the moment, I am not able to get images in to PDF files. I have tried 2 installations of eXist (2.2 and 3RC) and many possible scenarios. Of course, I have tested those pictures are reachable through the browser.

In the source file, I have tried:

1. <graphic url="img/tealover.jpg"/>
2. <graphic url="/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg"/>
3. <graphic url="http://46.28.111.241:8081/exist/rest/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg"/>

4. <graphic url="url(img/tealover.jpg)"/>
5. <graphic url="url(/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg)"/>
6. <graphic url="url(http://46.28.111.241:8081/exist/rest/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg)"/>

7. <graphic url="url('img/tealover.jpg')"/>
8. <graphic url="url('/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg')"/>
9. <graphic url="url('http://46.28.111.241:8081/exist/rest/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg')"/>

As expected, samples 3, 6 and 9 work but only if I have them in a form of hardcoded links in the source. If I build up the links in my XSLT stylesheet, they are exactly the same in the FO file but there is nothing in the produced PDF.

Equivalents produced in FO file:

1. <fo:external-graphic src="img/tealover.jpg"/>
2. <fo:external-graphic src="/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg"/>
3. <fo:external-graphic src="http://46.28.111.241:8081/exist/rest/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg"/>

4. <fo:external-graphic src="url(img/tealover.jpg)"/>
5. <fo:external-graphic src="url(/db/apps/karolinum-apps/data/2015080/img/tealover.jpg)"/>
6. <fo:external-graphic src="url(http://46.28.111.241:8081/exist/rest/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg)"/>

7. <fo:external-graphic src="url('img/tealover.jpg')"/>
8. <fo:external-graphic src="url('/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg')"/>
9. <fo:external-graphic src="url('http://46.28.111.241:8081/exist/rest/db/apps/karolinum-apps/data/mono/2015080/img/tealover.jpg')"/>

When I hard-code those links into the source, in the stylesheet works src="{@url}". When I use the short version of url everywhere (url="img/tealover.jpg"), in the attribute stylesheet I use

<xsl:value-of select="concat('http://46.28.111.241:8081/exist/rest', $imgPath, @url)"/>

or directly in the template

src="concat('http://46.28.111.241:8081/exist/rest', $imgPath, @url)"

The $imgPath variable is passed as a param from the application:

let $bookUri := base-uri($resource)
let $imgPath := replace($bookUri, '[^/]*?$', '')

With this, links 1, 4 and 7 should work, the rest is a mess. I can copy these links in eXide and they still work in the browser.

When I am testing, everything looks fine. In the PDF, there is still no picture. I guess there has to be a tiny detail I am missing now.

回答1:

Got the solution. Unexpectedly, the best thing is to let urls as they are and focus on resolving of the root paths. With this the FOP processor is able to resolve relative paths and everything works as expected. In my case, I serve dynamic config file to the xslfo:render() function. The most important part is:

<hyphenation-base>{replace(request:get-url(), '/apps(.*?)$', '/rest')}/db/apps/karolinum-apps/modules/resources/hyph/</hyphenation-base>
<hyphenation-pattern lang="cs" country="CZ">cs</hyphenation-pattern>
<base>{replace(request:get-url(), '/apps(.*?)$', '/rest') || replace(base-uri($doc), '[^/]*?$', '')}</base>
<font-base>{replace(request:get-url(), '/apps(.*?)$', '/rest')}/db/apps/karolinum-apps/modules/resources/fonts/</font-base>

Now I am able to store everything inside the project and use relative paths in my source files. Hope this solution will survive next testing! If you knew more elegant way how to do this, please, let me know (ok, first of all I could put those clumsy path operations into variables).