我在我的系统上使用 laravel-dompdf 来输出 pdf。
我最近更新了 php 和 laravel。
但是,有些pdf是乱码的。
字体由css中的font-face定义。
字体在 public/assets/pdf.css 中定义。
内的字符为乱码。
font-weight为“bold”的字符也是乱码。
这是为什么?
.a 类定义在 public/assets/pdf.css 中。已反映出来。
更新前在php7.4.33和laravel6.20.44中运行无乱码。
听朋友说需要两个font-face定义才能支持utf-8中的粗体,所以我定义了两个font-face定义,但是没有体现。
.a 类定义在 public/assets/pdf.css 中。已反映出来。
公共/资产/pdf.css
.a {
font-size: 80px;
}
html {
font-size: 10px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
@font-face {
font-family: mcs;
src: url('../fonts/mcs.TTF');
}
@font-face {
font-family: mcs;
font-weight: bold;
font-style: bold;
src: url('../fonts/mcs.TTF');
}
body {
font-family: ipag;
font-weight: normal !important;
}
资源/视图/pdf.blade.php
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="{{ public_path('/assets/pdf.css') }}" rel="stylesheet" type="text/css" media="all">
<style>
</style>
</head>
<body>
<table>
<tr>
<th>カタカナ</th>
<td>カタカナ</td>
</tr>
<tr>
<th>漢字</th>
<td>漢字</td>
</tr>
<tr>
<th>ひらがな</th>
<td>ひらがな</td>
</tr>
</table>
<p class="a">あいうえお</p>
<p style="font-weight: bold">あいうえお<p>
</body>
</html>
控制器中的功能
$export = $this->pdf->loadView('pdf');
return $export->download('test.pdf');
TTF 文件存在于 public/fonts/mcs.TTF 中,没有问题。
sh-4.2# php -v
PHP 8.1.23 (cli) (built: Sep 15 2023 21:56:29) (NTS)
sh-4.2# php artisan --版本
Laravel Framework 9.52.16
我已经安装了 mbstring 扩展。
yum install php-mbstring
输出pdf
我将 barryvdh/laravel-dompdf 更新到 1.0.0,稍后它有所改进。不知道为什么它有所改善。