我正在尝试使用Dompdf将某些SVG显示为PDF,但是PDF输出为空白。有人可以告诉我我需要更改吗?
这里是一些产生错误的示例代码。
<?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->1 -->
<g id="edge1" class="edge">
<title>A->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();
?>
如果运行echo($statement)
,浏览器将输出以下内容:
Dompdf输出的PDF完全为空白。
[如果我改用$statement = "hello, world!"
,则Dompdf正确输出世界!在PDF中。
我需要怎么做才能通过Dompdf将SVG图像输出为PDF?
我通过删除第一行<?xml version="1.0" encoding="UTF-8" standalone="no"?>
使它正常工作>
此外,在真实代码中,我必须删除svg中嵌入的所有链接。
此外,我不得不将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();
这是通过反复试验,并通过阅读另一篇文章Dompdf not generating pdf properly from SVG