1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<?php
use Dompdf\Dompdf;
$body = 'test 简化字 彝語/彝语 アメリカ test číslo € černý Češký test 深水埗 嘅 啲';
$cjk_scripts = 'Bopomofo|Han|Hiragana|Katakana'; $cjk_scripts = preg_replace('/[a-zA-Z_]+/', '\\p{$0}', $cjk_scripts);
// wrap the CJK characters into a span with it's own font $body = preg_replace("/($cjk_scripts)+/isu", '<span class="cjk">$0</span>', $body);
// a font that supports CJK characters // $cjk_font_path = FCPATH.'assets/fonts/DroidSansFallbackFull.ttf'; $cjk_font_path = FCPATH.'assets/fonts/msjh.ttf';
// vdump(file_exists($cjk_font_path));exit; $html = <<<HTML <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style type="text/css"> @font-face { font-family: 'DroidSansFallbackFull'; font-style: normal; font-weight: 400; src: url('$cjk_font_path') format('truetype'); } body { font-family: DejaVu Sans, sans-serif;; } .cjk { font-family: DroidSansFallbackFull, sans-serif; } </style> </head> <body>$body</body> </html> HTML;
$dompdf = new Dompdf(); $dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape'); $dompdf->set_option('defaultFont', 'Courier'); // 預設字型(僅支援英文) $dompdf->render();
// Attachment: 0 直接顯示, 1 強制下載 $dompdf->stream(null, ['Attachment' => 0]);
|