改源碼:
打開vendor/mpdf/mpdf/src/Config/FontVariables.php
在最后一行加入:
"gb" => [ 'R' => 'gb.ttf', 'B' => 'gb.ttf', 'I' => 'gb.ttf' ]
new的時候,使用剛剛設置的字體
$mpdf = new Mpdf(['utf-8', 'A4', 16, '', 10, 10, 15, 1, 'default_font' => 'gb']);
然后就可以用了,幫到你的話,頂一下推薦唄!
PS:字體包放在vendor/mpdf/mpdf/ttfonts該目錄下面
字體鏈接:
鏈接:https://pan.baidu.com/s/1wy0Ik95AjM5WjSC3nzOzqA 提取碼:f26j 復制這段內容后打開百度網盤手機App,操作更方便哦
本文是以thinkphp來寫的,實際上都一樣,只是mpdf的目錄vendor不一樣
還有一個版本,就莫名奇妙的。
首先在路徑:vendor\mpdf\mpdf\ttfonts放入微軟雅黑字體
然后代碼
1 /** 2 * 導出報告 3 * @param string $filename 文件名.pdf 4 * @param string $content 文件內容 5 * @param string $type D下載,F保存,I瀏覽器直接打開。默認下載 6 */ 7 public static function exportToPdf($filename, $content, $filePaht, $type = 'D') 8 { 9 $noName = ['/', '\\', ':', '*', '"', '<', '>', '?']; 10 $filename = str_replace($noName, '_', $filename); 11 if (!is_dir($filePaht) && $type == "F") { 12 mkdir($filePaht); 13 } 14 $mpdf = new Mpdf(['utf-8', 'A4', 16, '', 10, 10, 15, 1, 'default_font' => 'gb']); 15 $mpdf->SetDisplayMode('fullpage'); 16 $mpdf->autoScriptToLang = true; 17 $mpdf->autoLangToFont = true; 18 $mpdf->useAdobeCJK = true; 19 $mpdf->WriteHTML($content); 20 $mpdf->Output($filePaht . "/" . $filename . ".pdf", $type); 21 }