将内联svg的html转换为pdf

时间:2018-08-28 08:54:13

标签: php tcpdf

我通过foreach循环生成html代码。有时可能会有内嵌的svg代码。这是我的代码的一部分,我认为我们需要找到解决方案。我使用TCPDF库。请告诉我如何解决此问题,或者您可以建议另一个比TCPDF更易于使用的库。谢谢。

foreach($submissions as $key => $value){
        $field_value = htmlspecialchars_decode(preg_replace("/\r\n|\r|\n/",'<br/>',$value['value']));

        if($fields[$fieldId]['type'] == 'signature'){
            $pdf->ImageSVG('@' . $field_value, $x=15, $y=30, $w='', $h='', $link='http://www.tcpdf.org', $align='', $palign='', $border=1, $fitonpage=false);
            $pdf->Write(0, $txt='', '', 0, 'L', true, 0, false, false, 0);
        }   

        if($fields[$fieldId]['type'] != 'signature')
            $html = '<div><div class="value">'.$field_value.'</div></div>';
            $pdf->writeHTML($html, true, false, true, false, '');
    }
    $pdf->Output($submissions[0]['date'].".pdf", 'I');

2 个答案:

答案 0 :(得分:0)

我强烈认为您有用于签名的有效数据...

所以也许首先尝试直接注入它。然后检查它是否起作用

$svgtest = '<svg width="10cm" height="10cm">    
    <rect x="50" y="100" width="50" height="50"
          style="stroke: #000000; fill: none;"/>
    <rect x="100" y="100" width="50mm" height="50mm"
          style="stroke: #000000; fill: none;" />
</svg>';
$pdf->ImageSVG('@' . $svgtest, $x=15, $y=30, $w='', $h='', $link='http://www.tcpdf.org', $align='', $palign='', $border=1, $fitonpage=false);

答案 1 :(得分:0)

感谢您的回复。这是我的svg数据$field_value = '<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" width=\"12cm\" height=\"4cm\"><polyline style=\"stroke:#000000;stroke-width:2;width:100%;fill:#ffffff;\" points=\"116,54 \" ><\/polyline><polyline style=\"stroke:#000000;stroke-width:2;width:100%;fill:#ffffff;\" points=\"145,36 \" ><\/polyline><polyline style=\"stroke:#000000;stroke-width:2;width:100%;fill:#ffffff;\" points=\"183,69 200,81 232,100 258,111 272,117 276,118 277,118 278,118 282,118 292,115 304,111 \" ><\/polyline><polyline style=\"stroke:#000000;stroke-width:2;width:100%;fill:#ffffff;\" points=\"344,88 341,87 329,87 304,87 264,87 223,87 198,88 180,93 169,96 168,96 \" ><\/polyline><polyline style=\"stroke:#000000;stroke-width:2;width:100%;fill:#ffffff;\" points=\"10,57 11,56 24,54 43,54 69,54 102,54 170,66 212,74 242,78 261,80 271,80 273,80 285,74 299,65 313,56 318,53 \" ><\/polyline><polyline style=\"stroke:#000000;stroke-width:2;width:100%;fill:#ffffff;\" points=\"321,50 315,51 303,58 293,66 287,73 282,80 272,85 263,87 253,89 245,92 233,97 224,100 214,104 206,106 196,108 187,110 178,110\" ><\/polyline><\/svg>';

相关问题