解决larave-dompdf中文字体显示问题

0、使用MPDF

dompdf个人感觉没有那么好用,最终的生产环境使用的是MPDF,github上有文档说明。
如果你坚持使用,下面是解决办法。可以明确的说,中文乱码是可以解决的。

1、安装laravel-dompdf依赖。

Packagist:https://packagist.org/packages/barryvdh/laravel-dompdf

composer require barryvdh/laravel-dompdf

2、配置config/app.php

// add the ServiceProvider to the providers array in config/app.php
// 添加到providers数组下
Barryvdh\DomPDF\ServiceProvider::class,

// Add this to your facades
// 添加到aliases数组下
'PDF' => Barryvdh\DomPDF\Facade::class,

3、创建view

  下载中文字体,这里我使用了msyh.ttf(微软雅黑)。

      在public目录下创建fonts目录并把下载好的字体复制到该目录下。

      

      创建view:

<!DOCTYPE html>
<html>
    <head>
        <title>测试pdf</title>

        <style>
            @font-face {
                font-family: 'msyh';
                font-style: normal;
                font-weight: normal;
                src: url(http://www.testpdf.com/fonts/msyh.ttf) format('truetype');
            }
            html, body {  height: 100%;  }
            body {  margin: 0;  padding: 0;  width: 100%;
                /*display: table;  */
                font-weight: 100;  font-family: 'msyh';  }
            .container {  text-align: center;
                /*display: table-cell; */
                vertical-align: middle;  }
            .content {  text-align: center;  display: inline-block;  }
            .title {  font-size: 96px;  }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">xiao{{$name}}</div>
            </div>
        </div>
    </body>
</html>

注意:引入字体,使用@font-face,并通过【font-family: 'msyh';】应用到这个body上。

@font-face {
                font-family: 'msyh';
                font-style: normal;
                font-weight: normal;
                src: url(http://www.testpdf.com/fonts/msyh.ttf) format('truetype');
            }

 

 4、创建controller

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use PDF;

class PdfController extends Controller
{
    /**
     * 测试下载pdf,使用loadView()方法
     * @return mixed
     */
    public function testDownloadPdf()
    {
        $data = array('name'=>'宝bao');
        $pdf = PDF::loadView('invoice', $data);
        return $pdf->download('invoice.pdf');
    }

    /**
     * 测试web页面展示pdf,使用loadHTML()方法加载
     * @return mixed
     */
    public function testStreamPdf()
    {
        $html = '<html><head><title>Laravel</title><meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'/><style>body{  font-family: \'msyh\';  }  @font-face {  font-family: \'msyh\';  font-style: normal;  font-weight: normal;  src: url(http://www.testenv.com/fonts/msyh.ttf) format(\'truetype\');  }</style></head><body><div class=\'container\'><div class=\'content\'><p style=\'font-family: msyh, DejaVu Sans,sans-serif;\'>献给母亲的爱</p><div style=\'font-family: msyh, DejaVu Sans,sans-serif;\' class=\'title\'>Laravel 5中文测试</div><div  class=\'title\'>测试三</div></div></div></body></html>';
        $pdf = PDF::loadHTML($html);
         return $pdf->stream();
    }
    
}

 

问题1:

ErrorException in AdobeFontMetrics.php line 45:
fopen(D:\workspace\php\testpdf\storage\fonts/\b0a260823b52b90d6a99ba3a53e67e9f.ufm): failed to open stream: No such file or directory

解决方法:在storage下创建fonts文件夹

 

问题2:

FatalThrowableError in TableCell.php line 31:
Call to a member function get_cellmap() on null

解决方法:

更多解释请查看:https://github.com/barryvdh/laravel-dompdf/issues/137

// 注释掉view中的这一行代码
display: table-cell;

 

参考资料:https://github.com/dompdf/dompdf/wiki/UnicodeHowTo

 

有问题可加QQ群询问【php/laravel技术交流群】:576269252

--------------------------------------

声明: 原创文章,未经允许,禁止转载!

--------------------------------------

posted @ 2016-11-20 21:16  景岳  阅读(10529)  评论(2编辑  收藏  举报