为什么我的图像不以 XSL-FO 显示?



在我的一个项目中,我必须使用 xslt 和 xml 在 pdf 中添加图像。但是,我的图像根本没有显示。

这是我的 xslt 代码片段

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

(我已经尝试过使用和不使用 url 部分,但由于它已经存在,我决定在我的问题中显示它)

变量 var 在包含的另一个文件中声明。

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

我遇到的问题是,即使我在调试时可以看到图像的正确路径,图像也不会显示在最终结果中。

我认为这不是图像位置的问题,因为 xsl 和图像都处于同一级别(所以图像.svg是访问图像的正确路径)

因此,我的问题的答案是我的文件名中有重音,这使我的FOP无法找到我的文件。有时最简单的问题最难找到。

SVG Embedded Graphics via XSL-FO

Apache FOP 嵌入式 SVG 示例

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

渲染X 嵌入式 SVG 示例

SVG 直接嵌入在 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>

根据 XEP 用户指南,fo:instream-foreign-object可以托管 SVG 图形。

最新更新