【问题标题】:Background-image not displays in xsl fo with Relative Path URL背景图像不显示在具有相对路径 URL 的 xsl fo 中
【发布时间】:2015-01-30 11:40:15
【问题描述】:

我正在尝试使用 xsl-fo 创建一个使用背景图像的 pdf。我设置了相对路径如下:

background-image = "url('../themes/images/logo.gif')"

当我给出像 background-image="C://Images/logo.gif" 这样的绝对路径时,它起作用了。 但是当我使用url在服务器中相对取它时,它就不起作用了。

下面是我的场景。

XML:

<?xml version="1.0" encoding="iso-8859-1"?>
 <form-data>
       <field>
          <name>txtFirstName</name>
          <value>ABC</value>
       </field>
       <field>
          <name>txtLastName</name>
          <value>XYZ</value>
       </field>
</form-data>    

XSL-FO(获取 Pdf)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
<xsl:template match="/">
  <fo:root>
     <fo:layout-master-set>
        <fo:simple-page-master master-name="SPM_Name" page-height="29.7cm" page-width="21cm" margin-top="1.2cm" margin-bottom="1.2cm" margin-left="1.75cm" margin-right="1.75cm">
           <fo:region-body margin-top="0.5cm" />
           <fo:region-before extent="0.5cm" />
           <fo:region-after extent="1cm" />
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="PSM_Name">
           <fo:single-page-master-reference master-reference="SPM_Name" />
        </fo:page-sequence-master>
     </fo:layout-master-set>
     <fo:page-sequence master-reference="PSM_Name" initial-page-number="1">
        <fo:static-content flow-name="xsl-region-before">
           <fo:block text-align="end" font-size="10pt" font-family="serif" line-height="14pt">
              Page
              <fo:page-number />
           </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
           <fo:block background-image="url('http://localhost:9081/resources/themes/images/Logo1.gif')" background-position="right" background-color="transparent" >
           </fo:block>
           <fo:block font-size="20pt" text-align="center" font-family="sans-serif" line-height="20pt" space-after.optimum="15pt" padding-top="18pt">Income Tax Form</fo:block>
           <fo:table table-layout="fixed" width="100%" border-collapse="separate">
              <fo:table-column column-width="50%" />
              <fo:table-column column-width="50%" />
              <fo:table-header text-align="center">
                 <fo:table-row>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block font-weight="solid">Questions</fo:block>
                    </fo:table-cell>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block font-weight="solid">Form Inputs</fo:block>
                    </fo:table-cell>
                 </fo:table-row>
              </fo:table-header>
              <fo:table-body text-align="center">
                 <fo:table-row>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>Your First Name</fo:block>
                    </fo:table-cell>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>
                          <xsl:value-of select="//field/value[../name/text() = 'txtFirstName']" />
                       </fo:block>
                    </fo:table-cell>
                 </fo:table-row>
                 <fo:table-row>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>Your Last Name</fo:block>
                    </fo:table-cell>
                    <fo:table-cell padding="6pt" border="0.5pt solid black" wrap-option="wrap" keep-together.within-column="always">
                       <fo:block>
                          <xsl:value-of select="//field/value[../name/text() = 'txtLastName']" />
                       </fo:block>
                    </fo:table-cell>
                 </fo:table-row>
              </fo:table-body>
           </fo:table>
        </fo:flow>
     </fo:page-sequence>
   </fo:root>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • "但是当我使用url在服务器中相对取它时,它不起作用。"相对于什么?
  • 在浏览器中打开开发者工具并转到网络选项卡,使用实际路径的“logo.gif”文件的请求应该失败。这应该会给你一个关于请求是在哪里提出的提示(以及你打算去哪里)
  • 图像未显示在输出 pdf 中

标签: image pdf xslt relative-path xsl-fo


【解决方案1】:

正如您在 cmets 中所说,您没有收到任何“找不到图像”警告,我认为图像 URL 没有任何问题。

您的样式表将图像作为的背景fo:block:

<fo:block background-image="url('http://localhost:9081/resources/themes/images/Logo1.gif')" 
    background-position="right" background-color="transparent" >
</fo:block>

一个空的 fo:block 会产生一个高度为 0pt 的单个块区域,因此它的背景是不可见的。

如果您的图片必须是整个页面的背景,请改用绝对定位的fo:block-container

<fo:block-container position="absolute" height="25cm" 
    background-image="url('http://localhost:9081/resources/themes/images/Logo1.gif')" 
    background-position="right" background-color="transparent" >
    <fo:block/>
</fo:block-container>
  • height 属性设置生成区域的高度,即具有所需背景的区域(使用您想要的高度)
  • 您还可以使用属性background-repeatbackground-position-horizontal 来控制图像的重复和位置
  • 如果您的 FO 处理器对验证要求严格(Apache FOP 是这样,并且如果 block-container 为空,则会出现验证异常),则需要容器内的空 fo:block

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 2020-12-15
    • 2018-08-06
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多