首页 > 解决方案 > 我在 MPDF 库中遇到 UTF-8 字符集问题

问题描述

我正在制作一个自动生成合同的系统,问题是我无法打印 PDF 中的某些字符。

Sérgio Avilla(例如,我的名字是这样的)-> 它应该是这样的:Sérgio Avilla。

下面是简化的应用程序代码。

<?php
require_once __DIR__ . '/vendor/autoload.php';
include 'config.php';
header("Content-type: text/html; charset=utf-8");

function file_get_contents_utf8($fn) {
    $content = file_get_contents($fn);
    return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

$html = file_get_contents_utf8("contratos/".$contrato);

$mpdf = new \Mpdf\Mpdf();

$mpdf->WriteHTML($html);

$mpdf->Output();

?>

如果有人可以帮助我,我将不胜感激。我已经测试过了,$html,如果直接打印在屏幕上没有问题,字符都对,问题是mpdf下来了。

标签: phppdfutf-8character-encodingmpdf

解决方案


在合同 html 文件中有一个 charset =... 元标记,我只是将其更改为 charset = utf-8 并且它有效。

后:<meta http-equiv=Content-Type content="text/html; charset=utf-8">

前:<meta http-equiv=Content-Type content="text/html; charset=windows-1252">


推荐阅读