如何在文本或段落中包含图像

How can I include images inside a text or paragraph

不确定正确的术语,因为我对 xsl:fo 很陌生。我正在创建一些样式表以将 html 页面转换为 pdf,除 'in paragraph' 图像外,一切都很好。我有几段包括图标,我想将它们转换为 xsl:fo。

html 看起来如下:

<p>This is a sentence with some icons, like <img src="icon_up"/>, <img src="icon_down"/>, and <img src="icon_right"/></p>

看起来像

This is a sentence with some icons, like [u], [d], and [r]

我正在使用这些模板转换为 pdf(使用 Altova XML / FOP)

<xsl:template match="p">
    <fo:block font-size="12pt" line-height="15pt" space-after="12pt">
        <xsl:apply-templates select="*|text()"/>
    </fo:block>
</xsl:template>
<xsl:template match="img[parent::p]">
   <fo:external-graphic src="{@src}" width="100%" content-width="scale-to-fit" scaling="uniform" content-height="100%"/>
</xsl:template>

但我的结果如下:

This is a sentence with some icons, like 
[u]
, 
[d]
, and 
[r]

那么,如何在一行中获取它而不是层叠下来?

没关系,我同时找到了解决方案,就像这样可行:

<xsl:template match="img[parent::p]">
<fo:inline> <fo:external-graphic src="{@src}" content-width="80%" scaling="uniform"/></fo:inline>
</xsl:template>

格式化对象fo:external-graphic有两对维度:

  • widthheight 为图像定义 分配区域 (非正式地,它们定义了 "window" 或 "hole" 通过它可以看到图像)
    • 百分比是相对于线条的宽度和高度
    • 如果不设置为固定值或百分比,则默认为其他几个参数的对应值
  • content-widthcontent-height定义图像的大小
    • 百分比是相对于图像的固有大小(像素宽度/分辨率、像素高度/分辨率)

如果widthheight定义的矩形小于content-widthcontent-height定义的矩形,则只有一部分图像可见;在相反的情况下,分配的区域将仅被图像占用一部分(您可以在 XSL-FO specifications, § 6.6.5 fo:external-graphic 中找到完整的正式定义)。

您的原始模板有:

<fo:external-graphic src="{@src}" width="100%" .../>

因此每个图像的分配区域的宽度等于线宽的 100%,将以下内容推送到下一行。

为了让较小的图像与周围的文本在同一行中,您可以使用 content-widthcontent-height,将它们设置为固定值(例如 "1cm")或相对于其固有大小的百分比(正如您在自己的回答中所做的那样),并将 widthheight 保留为其默认值。