简体   繁体   中英

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. 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 :

在此处输入图片说明

The PDF output by Dompdf is completely blank.

If I instead use $statement = "hello, world!" , Dompdf correctly outputs hello, world! in the PDF.

What do I need to do to output this SVG image as PDF with Dompdf?

I got it to work by deleting the first line <?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.

Furthermore, I had to put the svg into an img tag, and add this base64_encode part as follows :

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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