我试图创建一个PDF使用DOMPDF在Pimcore的功能工作绝对良好,但我有问题,而try 包括非ANSI字符在PDF,如泰语,Koread,中文字符.要么它们被转换成问号,要么它返回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>

如果有人对此有什么建议,请告诉我.

推荐答案

确保您使用的字体支持您的语言(例如Noto Sans Thai字体支持泰文字符)

因此,假设使用以下语句:

<link href="https://fonts.googleapis.com/css2?family=Black+Han+Sans&family=Noto+Sans+Thai:wght@100..900&display=swap" rel="stylesheet">

<style>

.noto-sans-thai {
  font-family: "Noto Sans Thai", sans-serif;
  font-optical-sizing: auto;
  font-weight: <weight>;
  font-style: normal;
  font-variation-settings:
    "wdth" 100;
}


</style>

然后使用类似于

 <p class="noto-sans-thai">
         สวัสดี Stackoverflow Ken Lee สวัสดี
    </p>

完全正常工作的代码:

<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;


$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://fonts.googleapis.com/css2?family=Black+Han+Sans&family=Noto+Sans+Thai:wght@100..900&display=swap" rel="stylesheet">

<style>

.noto-sans-thai {
  font-family: "Noto Sans Thai", sans-serif;
  font-optical-sizing: auto;
  font-weight: <weight>;
  font-style: normal;
  font-variation-settings:
    "wdth" 100;
}


</style>
</head>
<body>
    <p class="noto-sans-thai">
        สวัสดี Stackoverflow Ken Lee สวัสดี
    </p>
</body>
</html>
HTML
;

//$_dompdf_show_warnings = true;
//$_dompdf_debug = true;

$tmp = sys_get_temp_dir();

$dompdf = new Dompdf([
    'logOutputFile' => '',
    // authorize DomPdf to download fonts and other Internet assets
    'isRemoteEnabled' => true,
    // all directories must exist and not end with /
    'fontDir' => $tmp,
    'fontCache' => $tmp,
    'tempDir' => $tmp,
    'chroot' => $tmp,
]);

$dompdf->loadHtml($html);

$dompdf->render();

$dompdf->stream('hello.pdf', [
    'compress' => true,
    'Attachment' => false,
]);

结果见下文:

enter image description here

Php相关问答推荐

条纹 checkout :订阅试用版和额外付款(add_invoice_items)错误

Symfony Api平台:按自定义实体方式排序结果

Laravel:让所有用户在一年中的特定月份休息几天

Nginx重写扩展名为.php的动态URL

WooCommerce/WordPress:简单的产品属性显示

如何在函数中定位WooCommerce产品循环

SendGrid响应PHP

免费送货柜台,考虑到折扣码

从目录文件夹中获取文件并追加到 Select 下拉列表(选项)-wordpress/wc

在WP工具中页面加载时可短暂看到的JavaScript生成的字段

如果WooCommerce购物车在 checkout 时被清空,请重定向至store 页面

在WooCommerce管理中为订单项目添加自定义字段价格值

如何使用PHP从TXM(XML)标记中读取特定属性

根据页面以及是否在促销,向 WooCommerce 中显示的价格添加前缀

Filament PHP v3:在禁用的表单字段上,ID 不会写入数据库

Google Drive API 服务->文件->get('root') 失败并显示找不到文件. (范围问题?)

转发和非转发呼叫 - 后期静态绑定

如何将子查询结果添加到学说 2 中主查询的末尾?

使用 GROUP_CONCAT 规范化/转置来自查询的数据

Symfony 在将它传递给 Twig 时是否从数据库中获取整个对象?