【问题标题】:Wrong utf8 character output savexml and tcpdf错误的 utf8 字符输出 savexml 和 tcpdf
【发布时间】:2013-02-07 02:52:50
【问题描述】:

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

【问题讨论】:

  • 很难确定可能是什么问题。 PDF 中显示的内容以及您期望的内容是什么?
  • 嗨,我期待 : "äëïòû" 在 pdf 中,我得到 "äëïòû"。

标签: php utf-8 svg tcpdf


【解决方案1】:
$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,它应该可以按照您的预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多