错误的utf8字符输出savexml和tcpdf

时间:2013-02-21 17:02:06

标签: php utf-8 svg tcpdf

您好我正在使用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中的“äëïòû”,我得到“äëÃÃÃÔ。 谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'UTF-8', false);

应该是:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

如果您要使用unicode编码,则应将unicode参数设置为true。假设您的SVG文件已正确保存为UTF-8,它应该按照您的预期进行此更改。