67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionsHandler;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Modules\AnnoRoute\AnnoRoute;
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
|
|
$this->app->bind(ExceptionsHandler::class, \App\Exceptions\ExceptionsHandler::class);
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(AnnoRoute $annoRoute): void
|
|
{
|
|
// 注册路由
|
|
$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
|
|
);
|
|
}
|
|
}
|
|
}
|