简体   繁体   English

使用php和dompdf输出svg的问题

[英]problems using php and dompdf to output svg

I'm trying to display some SVG as a PDF with Dompdf, but the PDF output is blank.我正在尝试使用 Dompdf 将一些 SVG 显示为 PDF,但 PDF 输出为空白。 Can someone tell me what I need to change?有人可以告诉我我需要改变什么吗?

Here is some example code which produces the error.这是一些产生错误的示例代码。

<?php
use Dompdf\Dompdf;
$dompdf = new Dompdf();

 $statement = '
        <?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
        "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
        <!-- Generated by graphviz version 2.40.1 (20161225.0304)
        -->
        <!-- Title: boxes_and_circles Pages: 1 -->
        <svg width="422pt" height="448pt"
        viewBox="0.00 0.00 421.50 448.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 444)">
        <title>boxes_and_circles</title>
        <polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-444 417.5,-444 417.5,4 -4,4"/>
        <!-- A -->
        <g id="node1" class="node">
        <title>A</title>
        <polygon fill="none" stroke="#000000" points="136,-353.5 82,-353.5 82,-317.5 136,-317.5 136,-353.5"/>
        <text text-anchor="middle" x="109" y="-331.3" font-family="Helvetica,sans-Serif" font-size="14.00" fill="#000000">A</text>
        </g>
        <!-- 1 -->
        <g id="node7" class="node">
        <title>1</title>
        <ellipse fill="none" stroke="#000000" cx="102" cy="-234.5" rx="32.5" ry="32.5"/>
        <text text-anchor="middle" x="102" y="-230.3" font-family="Helvetica,sans-Serif" font-size="14.00" fill="#000000">1</text>
        </g>
        <!-- A&#45;&gt;1 -->
        <g id="edge1" class="edge">
        <title>A&#45;&gt;1</title>
        <path fill="none" stroke="#000000" d="M107.7485,-317.4432C106.9701,-306.2112 105.9343,-291.266 104.9565,-277.1578"/>
        <polygon fill="#000000" stroke="#000000" points="108.4424,-276.8319 104.2593,-267.0979 101.4591,-277.316 108.4424,-276.8319"/>
        </g>
        </svg>
    ';

    $dompdf->loadHtml($statement);

    // Render the HTML as PDF
    $dompdf->render();

    //echo($statement);
    // Output the generated PDF to Browser
    $dompdf->stream();

?>

If I run echo($statement) , the browser outputs this :如果我运行echo($statement) ,浏览器会输出这个:

在此处输入图片说明

The PDF output by Dompdf is completely blank. Dompdf 输出的 PDF 是完全空白的。

If I instead use $statement = "hello, world!"如果我改为使用$statement = "hello, world!" , Dompdf correctly outputs hello, world! , Dompdf 正确输出 hello, world! in the PDF.在 PDF 中。

What do I need to do to output this SVG image as PDF with Dompdf?我需要做什么才能使用 Dompdf 将此 SVG 图像输出为 PDF?

I got it to work by deleting the first line <?xml version="1.0" encoding="UTF-8" standalone="no"?>我通过删除第一行<?xml version="1.0" encoding="UTF-8" standalone="no"?>

Also, in the real code, I had to delete all links that were embedded in the svg.此外,在实际代码中,我不得不删除嵌入在 svg 中的所有链接。

Furthermore, I had to put the svg into an img tag, and add this base64_encode part as follows :此外,我不得不将 svg 放入一个 img 标签,并添加这个base64_encode部分,如下所示:

$html = '<img src="data:image/svg+xml base64,'.base64_encode($statement).'"  width="100" height="100" />';
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream();

This was from trial and error, and through reading another post Dompdf not generating pdf properly from SVG这是来自反复试验,并通过阅读另一篇文章Dompdf not generate pdf 从 SVG 正确生成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM