first commit
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionsHandler;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\AnnoRoute\AnnoRoute;
|
||||
@@ -25,5 +26,41 @@ class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
// 注册路由
|
||||
$annoRoute->register(app_path('Http/Controllers'));
|
||||
|
||||
// 注册 DomPDF 中文字体(黑体),PDF 导出使用 font-family: SimHei
|
||||
$this->registerDompdfChineseFont();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册 DomPDF 中文字体(幂等:已注册则跳过)
|
||||
*/
|
||||
private function registerDompdfChineseFont(): void
|
||||
{
|
||||
$fontFile = resource_path('fonts/simhei.ttf');
|
||||
if (! file_exists($fontFile)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fontMetrics = Pdf::getDomPDF()->getFontMetrics();
|
||||
if ($fontMetrics->getFamily('SimHei')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! is_dir(storage_path('fonts'))) {
|
||||
mkdir(storage_path('fonts'), 0755, true);
|
||||
}
|
||||
|
||||
$variants = [
|
||||
['weight' => 'normal', 'style' => 'normal'],
|
||||
['weight' => 'bold', 'style' => 'normal'],
|
||||
['weight' => 'normal', 'style' => 'italic'],
|
||||
['weight' => 'bold', 'style' => 'italic'],
|
||||
];
|
||||
foreach ($variants as $variant) {
|
||||
$fontMetrics->registerFont(
|
||||
['family' => 'SimHei', 'weight' => $variant['weight'], 'style' => $variant['style']],
|
||||
$fontFile
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user