laravel-dompdf

https://github.com/barryvdh/laravel-dompdf#tip-utf-8-support

使用

安装

  1. composer require barryvdh/laravel-dompdf
  2. // config/app.php
  3. Barryvdh\DomPDF\ServiceProvider::class,
  4. 'PDF' => Barryvdh\DomPDF\Facade::class,

  1. public function pdf($html)
  2. {
  3. $pdf = app('dompdf.wrapper');
  4. $pdf->loadHTML($html);
  5. return $pdf->stream();
  6. }

模板

  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <meta charset="UTF-8">
  6. <meta name="viewport"
  7. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  8. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  9. <title>测试</title>
  10. <style>
  11. @font-face {
  12. font-family: 'msyh';
  13. font-style: normal;
  14. font-weight: normal;
  15. src: url({{asset('/storage/fonts/msyh.ttf')}}) format('truetype');
  16. }
  17. body {
  18. font-family: 'msyh', serif;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div>
  24. 您好,北京!
  25. </div>
  26. </body>
  27. </html>

中文字体

这里使用了msyh.ttf,在模板文件中如图引用即可

字体路径配置

  1. 'links' => [
  2. public_path('storage/fonts') => storage_path('fonts'),
  3. ],

运行下php artisan storage:link
默认运行缓存文件是保存在storage/fonts/文件夹下的,需要手动创建改文件夹,并赋予写的权限