【问题标题】:Transform SVG with sub svg images via TCPDF通过 TCPDF 使用子 svg 图像转换 SVG
【发布时间】:2016-10-05 05:53:03
【问题描述】:

我正在尝试将带有 tcpdf 的 svg 转换为 pdf。我正在使用

$pdf->ImageSVG(
    $file=$my_source,...

在 $my_source 中插入其他 svg 图像之前,一切都可以正常工作,例如:

<image xlink:href='subSvg.svg'/>

但我总是收到 tcpdf 的错误消息。有没有人在 svg 在 tcpdf 中插入子 svg 图像的经验?

更新:

我又试了一次:这只是一个警告。生成 PDF。 Tcdpf 只能释放子 svg 的资源:警告:xml_parser_free(): 19 is not a valid XML Parser resource in tcpdf.php on line 22913

这是一个示例 subSvg.svg

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
width="300" height="300" 
viewBox="0 0 300 300"
preserveAspectRatio="xMinYMin meet">
<desc>123456</desc>
<g id="elements" fill="black" stroke="none">
<rect x="0" y="0" width="30" height="30" />
</g>
</svg> 

是否可以引用 svg inline (data:image/svg+xml;utf8,)?

<image xlink:href='@<svg ...'/>

【问题讨论】:

  • 什么错误信息?
  • 我又试了一次:这只是一个警告。生成 PDF。 Tcdpf 只能释放子 svg 的资源:警告:xml_parser_free(): 19 is not a valid XML Parser resource in tcpdf.php on line 22913
  • 那么 subSvg.svg 中有什么
  • viewport 不是有效的 SVG 属性您是说 viewBox 吗?
  • 你是对的!但这对错误/警告没有影响。

标签: image svg tcpdf


【解决方案1】:

您可以考虑预处理 SVG 图像,内联引用的 SVG。以下是使用 XSLT 2.0 的可能方法的草稿:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  version="2.0">

  <xsl:template match="node()|@*">
    <xsl:param name="attributes"/>

    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select="$attributes"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="svg:image[matches(@xlink:href, '\.svg$')]">
    <xsl:variable name="imageUrl">
      <xsl:choose>
        <xsl:when test="contains(@xlink:href, '//')">
          <xsl:value-of select="@xlink:href"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(replace(base-uri(), '/[^/]+$', '/'), @xlink:href)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="imageDocument" select="document($imageUrl)"/>

    <xsl:if test="not($imageDocument)">
      <xsl:message terminate="yes">
        <xsl:value-of select="concat('Could not find image ', $imageUrl)"/>
      </xsl:message>
    </xsl:if>

    <xsl:apply-templates select="$imageDocument">
      <xsl:with-param name="attributes" select="(@x, @y, @width, @height, @transform)"/>
    </xsl:apply-templates>
  </xsl:template>
</xsl:stylesheet>

确保

  • 您正在使用 XSLT 2.0 处理器(对于 PHP 上下文中的 XSLT 2.0,请参阅 this post),
  • XSLT 处理器可以通过 xlink:href 属性中的 URL 访问引用的 SVG,
  • SVG 不会递归地相互引用,
  • 外部 SVG 不使用 CSS 和样式属性,这会导致内部 SVG 发生不希望的重新样式化,
  • 内联不会导致 ID 冲突,例如打破 &lt;use&gt; 元素或过滤器。

根据 SVG 文件的外观,XSLT 可能需要改进,例如重写 ID 和 ID 引用以解决最后一点。

【讨论】:

  • 有趣的想法!我想,我不需要自动转换。我可以将 sub,svg 的数据直接插入到文件中。但将它用作图像对我来说会很舒服。
猜你喜欢
  • 2014-08-22
  • 2022-07-05
  • 2013-04-15
  • 2018-11-19
  • 2014-03-22
  • 2021-06-06
  • 1970-01-01
  • 2017-07-09
  • 2022-11-10
相关资源
最近更新 更多