mPDF 日语字体配置

mPDF Japanese font configuration

我需要为日本客户使用 mPDF 和 YuGothM.ttc 字体生成 PDF。我需要以 PDF 格式导出的页面包含一些在浏览器版本 (HTML) 中呈现良好的文本,但在 PDF 中,相同的文本有一些使用字体的字符和其他字符一些默认字体未被批准由客户。我知道我必须在 .ttc 文件的每个字体定义中定义 TTCfontID 参数,但我只是不明白这些定义的含义以及如何正确配置字体。

该脚本用于使用 Polylang 插件进行翻译的 WordPress 网站。

$defaultConfig = (new \Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new \Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$pdf = new \Mpdf\Mpdf([
    //'mode' => '+aCJK',
    'mode' => 'utf-8',
    'format' => 'A3-L',
    'margin_left' => 5,
    'margin_right' => 5,
    'margin_top' => 5,
    'margin_bottom' => 5,

    'fontDir' => array_merge($fontDirs, [
        ASSETS_DIR ."fonts/Yugothib/",
    ]),

    'fontdata' => $fontData + [
        'yugoth' => [
            'R' => 'YuGothR.ttc',
            'TTCfontID' => [
                'R' => 1,
            ]
        ],
    ],
    'default_font' => 'yugoth',
    'languageToFont' => new CustomLanguageToFontImplementation()
]);

$pdf->autoScriptToLang  = true;
$pdf->autoLangToFont  = true;
$pdf->showImageErrors = true;
$pdf->allow_charset_conversion = true;
        
$pdf->SetHTMLHeader();
$pdf->SetHTMLFooter();
$pdf->WriteHTML($this->getHtmlTemplate($stock, $userId, $loadedDate));

$pdf->Output($filepath, 'F');

并且 HTML 也取自同一 class

中的私有函数
function getHtmlTemplate($records, $userId, $loadedDate){
    $user = $this->getUserInfo($userId);

    $companyName = $user->client;

    $html = "
<html lang=\"ja\">
<head>
<style>
.table {
    font-family: 'yugoth', sans-serif; 
    font-size: 16px;
    font-weight: normal;
}
.column-header {
    text-align: center;
    vertical-align: middle;
    background-color: #777777;
    color: #FFFFFF;
}
.cell{
    border-left: 1px solid #efefef;
    border-bottom: 1px solid #efefef;
}
.cell-last {
    border-right: 1px solid #efefef;
}
.a-center {
    text-align: center;
}
.w-80 {
    width: 80px;
}
.w-quality {
    width: 380px;
}
.w-150 {
    width: 150px;
}
</style>
</head>
<body lang=\"ja\">

<table border=\"0\" style=\"width: 100%\">
<tr>
    <td style=\"width:26%; text-align: left; font-size: 18px\">
        <div>" . $companyName ."</div><br />
        <div lang='ja'>" . pll__('Stock Info') ."</div>
    </td>
    <td class=\"a-center\" style=\"width:26%\">
        <div style=\"font-size: 50px; color: #004729; font-weight: bold;\" lang='ja'>" . pll__('Stock Report'). "</div>
    </td>
    <td style=\"width:26%; text-align: right\">
        <div>". $date ."</div><br />
        <img src=\"" . ASSETS_URL . "images/logo.png\" width=\"200px\" />
    </td>
</tr>
</table>

<table class=\"table\" border=\"0\" cellpadding=\"7\">
    <thead>
        <tr>
            <th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 1") ."</th>
            <th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 2") ."</th>
            <th class=\"column-header w-150\">" . pll__("Col 3") ."</th>
            <th class=\"column-header w-quality\">" . pll__("Col 4") . "</th>
            <th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 5") . "</th>
            <th class=\"column-header w-80\">" . pll__("Col 6") . "</th>
            <th class=\"column-header\" style=\"width: 130px\">" . pll__("Col 7") . "</th>
            <th class=\"column-header\">" . pll__("Col 8") . "</th>
            <th class=\"column-header\">" . pll__("Col 9") . "</th>
            <th class=\"column-header\" style=\"width: 100px\">" . pll__("Col 10") . "</th>
            <th class=\"column-header\">" . pll__("Col 11") . "</th>
            <th class=\"column-header\" style=\"width: 120px\">" . pll__("Coll 12") . "</th>
            <th class=\"column-header w-150\">" . pll__("Col 13") . "</th>
            <th class=\"column-header w-150\">" . pll__("Col 14") . "</th>
        </tr>
    </thead>
    <tbody>
";

    /** @var StockItem $row */
    foreach ($records as $row){
        $html .= "
    <tr>
        <td class=\"cell a-center\">" . $row->col1 ."</td>
        <td class=\"cell a-center\">".$row->col2."</td>
        <td class=\"cell a-center w-150\">". $row->col3 ."</td>
        <td class=\"cell w-quality\">".$row->col4."</td>
        <td class=\"cell w-150\">".$row->col5."</td>
        <td class=\"cell a-center w-80\">".$row->col6."</td>
        <td class=\"cell a-center w-80\">".$row->col7."</td>
        <td class=\"cell a-center\" lang='ja'>".$row->col8."</td>
        <td class=\"cell a-center\">".$row->col9."</td>
        <td class=\"cell a-center\">".$row->col10."</td>
        <td class=\"cell a-center\">".$row->col11."</td>
        <td class=\"cell a-center w-80\">".$row->col12."</td>
        <td class=\"cell a-center w-150\">".$row->col13."</td>
        <td class=\"cell cell-last a-center w-150\">".$row->col14."</td>
    </tr>";
    }

    $html .= "
</tbody>
</table>
</body>
</html>
";

        return $html;
    }
}

有人可以帮我正确配置 mPDF 或建议一些其他已经可用的日文字体的其他配置吗?

提前致谢!

最终我更改了脚本以使用 TCPDF,这种方式实际上非常有效。我知道这个答案没有回答我提出的问题,但它解决了我的问题。