多語系支援指南
字型管理
註冊 CJK 字型
use NextPDF\Core\Support\FontRegistry;
$fontRegistry = FontRegistry::create()
->registerFont(
path: '/fonts/NotoSansCJKtc-Regular.otf',
alias: 'NotoSansCJKtc',
)
->registerFont(
path: '/fonts/NotoSansCJKtc-Bold.otf',
alias: 'NotoSansCJKtc-Bold',
)
->registerFont(
path: '/fonts/NotoSansArabic-Regular.ttf',
alias: 'NotoSansArabic',
);
推薦字型資源
| 語系 | 推薦字型 | 授權 |
| 繁體中文 | Noto Sans CJK TC | SIL OFL |
| 簡體中文 | Noto Sans CJK SC | SIL OFL |
| 日文 | Noto Sans CJK JP | SIL OFL |
| 韓文 | Noto Sans CJK KR | SIL OFL |
| 阿拉伯文 | Noto Sans Arabic | SIL OFL |
| 希伯來文 | Noto Sans Hebrew | SIL OFL |
CJK 文字渲染
$renderer = $document->text();
// 繁體中文
$renderer->write(
text: '這是一份使用 NextPDF 生成的 PDF 文件。',
position: Position::at(x: 25.0, y: 50.0),
fontSize: 12.0,
fontName: 'NotoSansCJKtc',
language: 'zh-TW',
);
// 日文
$renderer->write(
text: 'NextPDF で生成された PDF ドキュメント',
position: Position::at(x: 25.0, y: 70.0),
fontSize: 12.0,
fontName: 'NotoSansCJKtc',
language: 'ja-JP',
);
雙向文字(BiDi)
use NextPDF\Core\Typography\BiDiResolver;
// BiDi 解析自動處理,依 language 參數啟用
$renderer->write(
text: 'مرحبا بك في NextPDF', // 阿拉伯文:從右到左
position: Position::at(x: 25.0, y: 100.0),
fontSize: 14.0,
fontName: 'NotoSansArabic',
language: 'ar-SA',
direction: 'rtl',
);
混合方向文字
// 混合方向自動處理(BiDi 演算法)
$renderer->write(
text: 'مرحبا NextPDF مرحبا', // Arabic with English embedded
position: Position::at(x: 25.0, y: 130.0),
fontSize: 12.0,
fontName: 'NotoSansArabic',
language: 'ar-SA',
);
阿拉伯文塑形
use NextPDF\Core\Typography\ArabicShaper;
// ArabicShaper 由 FontRegistry 自動啟用
// 可透過 RenderingContext 取得塑形引擎狀態
$context = $document->renderingContext();
$shaper = $context->typography->arabicShaper;
連字斷詞(Hyphenation)
use NextPDF\Core\Content\HyphenationPatterns;
$patterns = HyphenationPatterns::forLanguage('de-DE'); // 德文斷詞
$renderer->write(
text: 'Donaudampfschifffahrtsgesellschaft',
position: Position::at(x: 25.0, y: 200.0),
fontSize: 12.0,
fontName: 'NotoSans',
language: 'de-DE',
hyphenation: $patterns,
);
支援的斷詞語言
OpenType 特性
$renderer->write(
text: 'fi fl ffi ffl',
position: Position::at(x: 25.0, y: 250.0),
fontName: 'NotoSans',
openTypeFeatures: ['liga', 'kern', 'calt'],
);
字型子集化
文字預處理管線
延伸閱讀