嗨,我正在使用 TCPDF 加载 svg 文件,对其进行操作并将其作为 pdf 发送。
svg 文件包含 utf8 字符,保存后 utf8 字符无法正确显示。
这是svg文件
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<svg xmlns="http://www.w3.org/2000/svg" height="742" id="svg_area" shape-rendering="crispEdges" title="test" viewport-fill="" viewport-fill-opacity="0" width="1042" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs id="4D"/>
<g Cbarre="2" id="Image_DBB524BDFDFB4E4180B8BB48EF3F68CC" transform="rotate(0,176.5,522)">
<text font-family="arial" font-size="14" height="20" id="txtImage_DBB524BDFDFB4E4180B8BB48EF3F68CC" visibility="visible" x="404" y="435" ztspan="1">
<tspan>äëïòû</tspan>
</text>
</g>
</svg>
和PHP代码:
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(0, 0, 5, true);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 0);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
//set some language-dependent strings
$pdf->setLanguageArray($lg);
//$source : path to svg file
$dom = new DOMDocument();
$dom->load($source);
$xpath = new DOMXpath($dom);
// set orientation
$page = $dom->getElementsByTagName('svg')->item(0);
$width_page = $page->getAttribute('width');
$height_page = $page->getAttribute('height');
$orientation = 'L';
if($width_page < $height_page){
$orientation = 'P';
}
// add a page
$pdf->AddPage($orientation, '', false, false);
//modify dom svg for variable
foreach ($_GET as $key => $value) {
$gNodes=$xpath->query('//*[@id=\'' . $key . '\']');
foreach ($gNodes as $gNode) {
// doing stuff with $gNode
$gNode->nodeValue = $value;
}
}
$txt = '@'.$dom->saveXml();
$pdf->ImageSVG($txt, $x=0, $y=0, $w='', $h='', $link='', $align='', $palign='', $border=1, $fitonpage=true);
//Close and output PDF document
$pdf->Output('utf8.pdf', 'I');
处理后的 pdf 文件显示错误的 utf-8 字符:我期待 pdf 中有“äëïòû”,我得到“äëïòû”。感谢您的回答。