【问题标题】:Displaying SVG using XSLFO使用 XSLFO 显示 SVG
【发布时间】:2011-01-21 09:07:15
【问题描述】:

我的第一个问题;我目前正在修复一个图形服务,它使用 XSLFO 将我们的语法转换为 FO,并最终将其转换为 PDF。

之前我们一直在 PDF 导出中使用来自网络的 PNG 图,但这会产生非常丑陋的结果,因此我们决定使用 SVG 代替 PDF。

但是,SVG 似乎无法正确缩放到 SVG 画布中。

这是运行 XSLFO 之前的语法:

<img src="someimage.svg">

这是我正在使用的 XSLFO:

   <xsl:template match="img">
      <fo:block space-after="12pt">
          <fo:instream-foreign-object width="20cm" height="15cm" content-width="scale-to-fit" content-height="scale-to-fit" scaling="uniform" background-color="#cccccc">
          <svg:svg x="0" y="0" width="100" height="100" viewBox="0 0 100 100">
                <svg:image x="0" y="0" width="100" height="100">
                    <xsl:if test="@src">
                       <xsl:attribute name="xlink:href">
                          <xsl:choose>
                             <xsl:when test="starts-with(@src, 'http://')">
                                <xsl:value-of select="@src"/>
                             </xsl:when>
                             <xsl:when test="starts-with(@src, 'https://')">
                                <xsl:value-of select="@src"/>
                             </xsl:when>
                             <xsl:otherwise>
                                <xsl:value-of select="concat($baseurl, @src)"/>
                             </xsl:otherwise>
                          </xsl:choose>
                       </xsl:attribute>
                    </xsl:if>
                   </svg:image>
            </svg:svg>
         </fo:instream-foreign-object>
      </fo:block>
   </xsl:template>

SVG 确实出现在 PDF 中,并且似乎包含在画布中 - 但无论出于何种原因,我都无法正确缩放它。它真的非常非常大,结果是 SVG 的一个非常裁剪的版本。

我的建议用完了 - 这里有没有人有这方面的经验?

PS:图片是使用最新版本的Batik制作的,宽高设置正确。

【问题讨论】:

    标签: java xslt pdf-generation svg xsl-fo


    【解决方案1】:

    实际上,instream-foreign-object 似乎根本无法缩放 SVG,即使使用正确的画布集也是如此。通过在 SVG 上设置正确的画布,fo:external-graphic 成功了 ;-)

    感谢你们给我的建议 :-) 以下是有效的方法:

        <fo:external-graphic content-width="25cm" content-height="16cm">
            <xsl:if test="@src">
                <xsl:attribute name="src">
                    <xsl:choose>
                        <xsl:when test="starts-with(@src, 'http://')">
                            <xsl:value-of select="concat('url(',@src,')')"/>
                        </xsl:when>
                        <xsl:when test="starts-with(@src, 'https://')">
                            <xsl:value-of select="concat('url(',@src,')')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="concat('url(',$baseurl, @src,')') + ')'"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </xsl:if>
        </fo:external-graphic>
    

    【讨论】:

      【解决方案2】:

      它很大是因为 fo:instream-foreign-object 有很大的宽度和高度;如果您是 XSL-FO 的初学者,您应该尝试Ecrion Designer - 您可以直观地编辑 XSLFO 并使用鼠标调整大小。 干杯!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-25
        • 2018-09-27
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-15
        • 1970-01-01
        相关资源
        最近更新 更多