3

In one of my project, I have to add image in a pdf with xslt and xml. However, my images isn't displaying at all.

Here's my xslt snippet

<fo:block text-align="left">
<fo:external-graphic src="url({$var})" content-height="6pt" content-width="6pt"/>
</fo:block>

(I've tried with and without the url part, but since it was already there I decided to show it in my question)

The variable var is declared in another files that is included.

<xsl:variable name="imgPath"></xsl:variable>
<xsl:variable name="var">
<xsl:value-of select="$imgPath"/>image.svg</xsl:variable>

The problem, I have is that even though I can see the right path of the image when I debug, the image is not displayed in the final result.

I don't think it is a problem with the location of the image since both the xsl and the images are at the same level (So image.svg is the correct path to access the image)

J. Lev
  • 307
  • 3
  • 19
  • I am pretty sure that image src attributes do not use `url()`, that's from CSS. Try `src="{$var}"`. – Tomalak Oct 25 '17 at 17:40
  • @Tomalak Even if I remove the url() part my images are not displayed. – J. Lev Oct 25 '17 at 17:48
  • Have you tried hardcoding the path just to see if it works? Maybe try prefixing it with "./" (like `src="./image.png"`). – Daniel Haley Oct 25 '17 at 17:51
  • @DanielHaley Even if I hardcode(with "./image.png" or with "image.png"), my images are not displayed... (In debug I can see that the src of my external-graphic was image.png) – J. Lev Oct 25 '17 at 17:58
  • To eliminate the possibility of a location issue, try hardcoding the absolute path (like `src="file:/C:/somepath/image.png"`). – Daniel Haley Oct 25 '17 at 18:07
  • 1
    @DanielHaley So by changing my code to`` my images still doesn't display, which at least tells me there might be a problem with the format of my image... Now I know where to look thanks! – J. Lev Oct 25 '17 at 18:13
  • See if [**this answer**](https://stackoverflow.com/a/44973885/290085) helps. – kjhughes Oct 25 '17 at 18:16
  • @kjhughes No success :(. I've tried to add one /, two /, changes the / to \, but none of it worked. Thanks anyway, as I said to danierHaley, It might be a format issue or maybe my fopProcessor doesn't support svg files... – J. Lev Oct 25 '17 at 18:31
  • Note: Your question talks about PNG but your comment mentions SVG. Which is it? Are you getting any errors? What is your FOP processor? – kjhughes Oct 25 '17 at 18:35
  • 1
    @kjhughes I use RenderX. I don't get any error, just an image with a red X ( that implies that there is an external-graphic block, but no image Inside) in my pdf – J. Lev Oct 25 '17 at 18:38
  • I use SVG not png. – J. Lev Oct 25 '17 at 18:41
  • Can you open image.svg in another program? That would rule out corruption in the image itself. – Hobbes Oct 26 '17 at 08:56
  • @Hobbes I can open my images with internet explorer, so it rules out the corruption of the images – J. Lev Oct 26 '17 at 12:08
  • 1
    If you use RenderX it will report any issues in the log. – Kevin Brown Oct 26 '17 at 17:35
  • Thanks @KevinBrown, With the log I found my problem :) – J. Lev Oct 26 '17 at 18:24

2 Answers2

3

So, the answer to my problem is that there were an accent in the name of my file which made my FOP unable to locate my files. Some time the most simple problems are the hardest to find.

J. Lev
  • 307
  • 3
  • 19
0

SVG Embedded Graphics via XSL-FO

Apache FOP Embedded SVG Example

<?xml version="1.0" encoding="utf-8"?>
<!-- external-graphic-SVG-Diagram.fo
 - Copyright (c) 2016, HerongYang.com, All Rights Reserved.
-->
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <fo:layout-master-set>
  <fo:simple-page-master master-name="page" 
   margin="0.1in" page-height="4in" page-width="3in">
   <fo:region-body region-name="body" background-color="#eeffff"/>
  </fo:simple-page-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="page">
  <fo:flow flow-name="body">
   <fo:block border-width="1px" border-style="solid">
    <fo:block-container width="1in" border-width="1px"
     border-style="solid">
     <fo:block>1 inch</fo:block>
    </fo:block-container>
    <fo:block-container width="72px" border-width="1px" 
     border-style="solid">
     <fo:block>72 px</fo:block>
    </fo:block-container>
    <fo:block-container width="120px" border-width="1px" 
     border-style="solid">
     <fo:block>120 px</fo:block>
    </fo:block-container>
    <fo:block border-width="1px" border-style="solid">
     SVG diagram of 288x288 px at a fixed resolution 144dpi:
    <fo:external-graphic src="sample.svg" width="2in" height="2in"
     content-width="scale-to-fit" content-height="scale-to-fit"/>
    </fo:block>
   </fo:block>
  </fo:flow>
 </fo:page-sequence>
</fo:root>

RenderX Embedded SVG Example

SVG directly embedded in XSL-FO:

<fo:block>
  Here is the image of a typical roadsign:
  <fo:instream-foreign-object content-height="1em">1 
    <svg:svg xmlns:svg="http://www.w3.org/2000/svg"2
             height="100" width="100" viewBox="-50 -50 100 100">
      <svg:circle r="50" style="fill:red; stroke:none"/>
      <svg:rect x="-40" y="-10" width="80" height="20"
                style="fill:white; stroke:none"/>             
    </svg:svg> 
  </fo:instream-foreign-object>
</fo:block>

Per the XEP User Guide, fo:instream-foreign-object can host SVG graphics.

kjhughes
  • 106,133
  • 27
  • 181
  • 240