我正在尝试使用 Pimcore 中的 DOMPDF 创建 PDF,其功能绝对正常,但在尝试在 PDF 中包含非 ANSI 字符(如泰语、韩语、中文字符)时遇到问题。它们要么转换为问号,要么返回 Squere 框。
这是我正在使用的代码。
public function downloadPdfTemplate(Request $request): Response
{
header('Content-Type: text/html; charset=utf-8');
$checkStatus = GeneralHelper::getUserAndRoleFromSession();
$peId = $request && $request->get('id') ? $request->get('id') : 0;
$userLanguage = $request && $request->get('language') ? $request->get('language') : $checkStatus[LANGUAGE];
DataObject\PackagingElement::setHideUnpublished(false);
$peData = DataObject\PackagingElement::getById($peId);
if ($peData) {
$pdfData = PackagingElementService::generatePePdf($peData, $userLanguage, $peId, true);
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('isHtml5ParserEnabled', true);
$pdfOptions->set('isPhpEnabled', true);
$pdfOptions->set('isFontSubsettingEnabled', true);
$pdfOptions->set('isRemoteEnabled', true);
$fontPath = PIMCORE_PROJECT_ROOT.'/public/bundles/frontend/css/fonts/NotoSansThai.ttf'; // Update the path accordingly
$pdfOptions->setFontDir(PIMCORE_PROJECT_ROOT . '/public/bundles/frontend/css/fonts/');
$fileContents = file_get_contents($fontPath);
$html = $this->renderView('@Frontend/packingElements/model/download-pdf-template.html.twig', $pdfData);
$dompdf = new Dompdf($pdfOptions);
$canvas = new CPDF([0, 0, 200, 200], "portrait", $dompdf);
$fontMetrics = new FontMetrics($canvas,$pdfOptions);
$fontMetrics->registerFont(['family'=>'Noto Sans Thai', 'style'=>'normal', 'weight'=>'normal'],
$fontPath);
$dompdf->setFontMetrics($fontMetrics);
$dompdf->loadHtml($html,'UTF-8');
$dompdf->render();
$output = $dompdf->output();
$pdfagicode = $pdfData['agiCode'];
$globdesc = $pdfData['globalDescription'];
$gd = GeneralHelper::replaceSpecialCharacters($globdesc);
$pdfFilepath = $folderName . '/PE_' . $pdfagicode . '_' . $gd . '.pdf';
file_put_contents($pdfFilepath, $output);
// Output the generated PDF to Browser (force download)
$dompdf->stream('PE_' . $pdfagicode . '_' . $gd . '.pdf', [
'Attachment' => true,
]);
unlink($pdfFilepath);
} else {
return $this->redirect('/packaging-element');
}
}
这是我正在使用的示例 HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;" charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Noto+Sans+HK:wght@600&family=Noto+Sans+JP:wght@600&family=Noto+Sans+KR:wght@600&family=Noto+Sans+SC:wght@600&family=Noto+Sans+TC:wght@600&display=swap"
rel="stylesheet" crossorigin>
<style>
/** Define now the real margins of every page in the PDF **/
body {
margin-top: 4cm;
margin-left: 0.5cm;
margin-right: 0.5cm;
margin-bottom: 1.8cm;
border-top: 1px solid #a0a0a0 !important;
font-family: 'Noto Sans HK', 'Noto Sans JP', 'Noto Sans KR', 'Noto Sans SC', 'Noto Sans TC', 'Noto Sans Thai','Noto Sans Tai Viet', DejaVu Sans, sans-serif
}
</style>
</head>
<body>
<p>Thai text สวัสดี</p>
</body>
</html>
如果有人对此有任何建议,请告诉我。
确保您使用支持您的语言的字体(例如,一般支持 unicode 字符的 droidsansfallback 字体)
因此请使用以下内容:
.uni1 {
font-family: droidsansfallback;
font-size: 10pt;
}
然后使用
<span class="uni1">สวัสดี</span>
确保您已为 domPDF 包含/安装了此字体