【问题标题】:TCPDF strange behaviour with SVG imageSVG 图像的 TCPDF 奇怪行为
【发布时间】:2021-06-06 09:33:54
【问题描述】:

当我尝试从 PHP 代码生成 PDF 时,我遇到了 TCPDF 呈现的奇怪行为。

这是我的 SVG 文件:

<?xml version="1.0" encoding="utf-8"?>
<svg x="0pt" y="0pt" width="1020pt" height="1020pt" viewBox="0 0 1020 1020" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <g id="1">
    <title>LAC ET FORET</title>
  </g>
  <g id="2">
    <title>VILLE</title>
    <g id="3">
      <title>Velo</title>
      <defs>
        <title>Path</title>
        <g id="4">
          <defs>
            <path id="5" d="M7.5,259.922 C213.226,259.922,380,427.885,380,635.077 C380,842.27,213.226,1010.23,7.5,1010.23 C-198.226,1010.23,-365,842.27,-365,635.077 C-365,427.885,-198.226,259.922,7.5,259.922 z"/>
          </defs>
          <use xlink:href="#5" style="fill:none;opacity:1;stroke:#ff0000;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-opacity:1;stroke-width:20;"/>
        </g>
      </defs>
      <use xlink:href="#4"/>
      <defs>
        <title>Path Copy</title>
        <g id="6">
          <defs>
            <path id="7" d="M1012.5,259.922 C1218.23,259.922,1385,427.825,1385,634.944 C1385,842.063,1218.23,1009.97,1012.5,1009.97 C806.774,1009.97,640,842.063,640,634.944 C640,427.825,806.774,259.922,1012.5,259.922 z"/>
          </defs>
          <use xlink:href="#7" style="fill:none;opacity:1;stroke:#ff0000;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-opacity:1;stroke-width:20;"/>
        </g>
      </defs>
      <use xlink:href="#6"/>
      <defs>
        <title>Path</title>
        <g id="8">
          <defs>
            <path id="9" d="M1010.1,635.507 C1010.1,635.507,760.013,134.872,760.013,134.872 C760.013,134.872,9.90756,134.892,9.90756,134.892 C9.90756,134.892,10.0611,635.784,10.0611,635.784 C10.0611,635.784,759.988,134.636,759.988,134.636 C759.988,134.636,760.03,10.1281,760.03,10.1281 C760.03,10.1281,766.014,9.93965,885.136,9.93963 C965.555,9.93962,970.933,135.054,885.062,135.054"/>
          </defs>
          <use xlink:href="#9" style="fill:none;opacity:1;stroke:#ff0000;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-opacity:1;stroke-width:20;"/>
        </g>
      </defs>
      <use xlink:href="#8"/>
      <defs>
        <title>Path</title>
        <g id="10">
          <defs>
            <path id="11" d="M1060.52,636.085 C1060.52,636.085,1009.94,636.085,1009.94,636.085"/>
          </defs>
          <use xlink:href="#11" style="fill:none;opacity:1;stroke:#ff0000;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:10;stroke-opacity:1;stroke-width:20;"/>
        </g>
      </defs>
      <use xlink:href="#10"/>
      <defs>
        <title>Path</title>
        <g id="12">
          <defs>
            <path id="13" d="M-30.5769,135.081 C-30.5769,135.081,10.1142,135.081,10.1142,135.081"/>
          </defs>
          <use xlink:href="#13" style="fill:none;opacity:1;stroke:#ff0000;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:10;stroke-opacity:1;stroke-width:20;"/>
        </g>
      </defs>
      <use xlink:href="#12"/>
    </g>
  </g>
</svg>

这是我用于生成 PDF 的 PHP 代码:它包含在一个将图像并排放置的循环中。

$pdf->ImageSVG($file = '@' . $img, $line*($iconsize*$ratioOf), $column*($iconsize*$ratioOf), $iconsize, $iconsize, $link='', $align='', $palign='', $border=0, $fitonpage=false);   

我不知道为什么,但渲染看起来像这样:我的意思是图像显示不正确。第一个没问题。然后第二个是缩小尺寸。

如果我使用相同的 SVG 文件,但仅使用 &lt;PATH&gt;(不再使用),包括样式。它工作正常。 My SVG 中的某些东西会使 TCPDF 异常运行。我想不通。

【问题讨论】:

    标签: svg tcpdf


    【解决方案1】:

    我知道这有点晚了,但也许有一天有人需要答案。 tcpdf 无法处理使用元素,因此您需要将它们替换为它们指向的内容,如下所示:

    $imagePath = "folder/imagename.svg";
    $svgContent = file_get_contents($imagePath);
    $xml = simplexml_load_string($svgContent);
    if($xml->xpath("//*[local-name()='use']")) {
        $defsContent = $xml->xpath("//*[local-name()='defs']");
        $defs = []; 
        foreach ($defsContent as $key => $elements) {
            foreach ($elements as $key => $element) {
                $defsArray = [];
                $defsArray['id'] = $element['id'];
                $defsArray['content'] = $element;
                array_push($defs, $defsArray);
            }
        }
        
        $usesParent = $xml->xpath("//*[local-name()='use']/..");
        foreach ($usesParent as $key => $parent) {
            $href = ltrim((string)$parent->use->attributes('http://www.w3.org/1999/xlink')->href, "#");
            $style = (string)$parent->use->attributes()->style;
            unset($parent->use); 
    
            foreach ($defs as $key => $def) {
                if($def['id'] == $href) {
                    $newElement = $parent->addChild('g');
                    $newElement->addAttribute('style', $style);
                    $this->simplexml_import_simplexml($newElement, $def['content']);
                }
            }
        }
        // overrides the old svg
        $xml->asXML($imagePath);
    }
    
    function simplexml_import_xml($parent, $xml) {
        $xml = (string)$xml;
        if ($nodata = !strlen($xml) or $parent[0] == NULL) {
            return $nodata;
        }
        $node     = dom_import_simplexml($parent);
        $fragment = $node->ownerDocument->createDocumentFragment();
        $fragment->appendXML($xml);
    
        return (bool)$node->appendChild($fragment);
    }
    
    function simplexml_import_simplexml($parent, $child) {
        // check if there is something to add
        if ($child[0] == NULL) {
            return true;
        }
        // if it is a list of SimpleXMLElements default to the first one
        $child = $child[0];
        // insert attribute
        if ($child->xpath('.') != array($child)) {
            $parent[$child->getName()] = (string)$child;
            return true;
        }
        $xml = $child->asXML();
        // remove the XML declaration on document elements
        if ($child->xpath('/*') == array($child)) {
            $pos = strpos($xml, "\n");
            $xml = substr($xml, $pos + 1);
        }
        return $this->simplexml_import_xml($parent, $xml);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 2015-06-21
      • 2023-03-11
      • 2019-10-09
      • 2014-02-18
      • 2016-08-12
      • 2016-06-26
      • 1970-01-01
      相关资源
      最近更新 更多