HEX
Server: Apache/2.4.37 (CentOS Stream) OpenSSL/1.1.1k
System: Linux ysnet.com.tw 4.18.0-553.5.1.el8.x86_64 #1 SMP Tue May 21 05:46:01 UTC 2024 x86_64
User: test (521)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //proc/self/cwd/test.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

// 檢查字型是否存在
$file_path = __DIR__ . '/fonts/NotoSansCJKtc-Regular.ttf';
if (!file_exists($file_path)) {
    die("字型檔案不存在:$file_path");
} 

// Dompdf Options
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
$options->set('isFontSubsettingEnabled', true);
// 將 Dompdf 的讀取根目錄設為此目錄 (確保能讀到 fonts/)
$options->set('chroot', __DIR__);

// 可以選擇預設字型名稱,但請與 @font-face 的 font-family 一致
$options->set('defaultFont', 'NotoSansCJKTC');

$dompdf = new Dompdf($options);

// HTML (只有簡單中文)
$html = <<<HTML
<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <style>
    @font-face {
        font-family: 'NotoSansCJKTC';
        src: url('fonts/NotoSansCJKtc-Regular.ttf') format('truetype');
        font-weight: normal;
        font-style: normal;
    }
    body {
        font-family: 'NotoSansCJKTC', sans-serif;
    }
    </style>
</head>
<body>
    <h1>中文測試</h1>
    <p>這是一段測試內容,用來確定是否正確嵌入字型。</p>
</body>
</html>
HTML;

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

// 輸出 PDF 供下載
$dompdf->stream("test.pdf", ["Attachment" => true]);
exit();