laravel-dompdf
https://github.com/barryvdh/laravel-dompdf#tip-utf-8-support
使用
安装
composer require barryvdh/laravel-dompdf
// config/app.php
Barryvdh\DomPDF\ServiceProvider::class,
'PDF' => Barryvdh\DomPDF\Facade::class,
类
public function pdf($html)
{
$pdf = app('dompdf.wrapper');
$pdf->loadHTML($html);
return $pdf->stream();
}
模板
<!doctype html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>测试</title>
<style>
@font-face {
font-family: 'msyh';
font-style: normal;
font-weight: normal;
src: url({{asset('/storage/fonts/msyh.ttf')}}) format('truetype');
}
body {
font-family: 'msyh', serif;
}
</style>
</head>
<body>
<div>
您好,北京!
</div>
</body>
</html>
中文字体
这里使用了msyh.ttf
,在模板文件中如图引用即可
字体路径配置
'links' => [
public_path('storage/fonts') => storage_path('fonts'),
],
运行下php artisan storage:link
默认运行缓存文件是保存在storage/fonts/
文件夹下的,需要手动创建改文件夹,并赋予写的权限