解决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
--------------------------------------
声明: 原创文章,未经允许,禁止转载!
--------------------------------------
解决larave-dompdf中文字体显示问题的更多相关文章
- html lang="zh-cn"解决Mac版Firefox中文字体显示问题
		
这两天在Mac下被Firefox的中文字体显示问题所困扰.在Firefox中将Sans-serif字体设置为SimSun-ExtB(新宋体)或英文字体(这时会用Mac默认中文字体),如下图: 浏览园子 ...
 - 关于JAVA应用中文字体显示小方框的问题解决
		
最近碰到linux下jboss应用中中文字体显示为小方框: “在JRE 5以上的java环境中,java会自动加载$JAVA_HOME/jre/lib/fonts目录下的字体.链接或复制宋体或微软雅黑 ...
 - 解决Ubuntu系统中文乱码显示问题,终端打开文件及查看目录
		
解决Ubuntu系统中文乱码显示问题 [日期:2014-02-20] 来源:Linux社区 作者:njchenyi [字体:大 中 小] 我是先安装了Ubuntu 12.04 Server,然后 ...
 - php导出pdf,dompdf中文字体乱码解决办法(特别是代码迁移引起的乱码)
		
dompdf\lib\fonts\dompdf_font_family_cache.php记住这个文件里面存放的是字体生成的缓存,迁移时如果覆盖了这个文件会导致乱码而且很难找到出错的地方,相信我... ...
 - matplotlib在MAC系统下中文字体显示问题
		
最近想把部分python数据分析的代码从win系统迁移到MAC上,有部分图片上涉及中文显示,迁移到MAC上warning: UserWarning: findfont: Font family [u' ...
 - 关于Matlab在绘图时中文字体显示不一致的问题
		
我的运行环境: OS: Win10 教育版 64-bit Matlab版本:Matlab 2017a 64-bit 在使用Matlab绘图时,而横坐标轴.纵坐标轴.标题有汉字时,会发现在GUI的显示 ...
 - 解决Android版Firefox字体显示过大的问题
		
在用Android版Firefox查看博客园首页发现中间区域的字体显示非常大,开始以为是首页css对移动版浏览器支持不好. 后来发现原来这是Firefox for Android的知名bug: Tha ...
 - deepin 下安装goland中文字体显示全是方块
		
下载中文字体 apt-get install ttf-arphic-uming xfonts-intl-chinese 替换goland的汉化包,两个jar包.https://blog.csdn.ne ...
 - wine的中文字体显示
		
从1.1.4开始wine的界面就已经支持中文了,但是对于软件中的中文支持并不太好,主要原因.还是字体...Let's go 首先,copy一下字体:把simsun.ttc (即宋体)复制到 ~/.wi ...
 
随机推荐
- 关于JS函数的bind
			
昨天被人问到js的bind的作用是什么? 这个倒还能回答出来,bind 之后返回一个新的函数,这个函数可以保持传递的this上下文. 接着又问了,那么bind两次不同的上下文会怎样? 这个一下子就蒙了 ...
 - TCP/IP capture/analysis tools in Unix/Linux
			
There are some useful tools in Unix/Linux to check out how the system is going on. Here is a short s ...
 - 新软件马上就要完成了,先发篇文章YY下
			
最近一直都在搞网站抓取方面的开发,闲着无聊逛逛论坛,发现有些帖子还是写的相当不错的,只是一篇一篇的点进去比较麻烦,于是就写了个小软件只是为了方便查看博客园和CSDN上的优秀文章.其实这个还可以拓展的, ...
 - kubernetes入门之快速部署
			
角色说明 这里主要有三个角色,分别部署不同的服务. 角色 服务 etcd etcd master kube-apiserver/kube-scheduler/kube-controller node ...
 - zookeeper删除kafka元数据
			
问题:卸载kafka前未删除kafka topic,重新安装kafka后,生成跟之前topic名字相同的topic时报错,显示topic已存在 [root@d96 ~]# kafka-topics - ...
 - [ios2]警告:Block的Retain Cycle的解决方法 【转】
			
<span style="background-color: rgb(248, 248, 248); font-family: 'PT Sans', Geogia, Baskervil ...
 - ASP.NET Web API中的Routing(路由)
			
[译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...
 - js如何判断是否在iframe中
			
JS代码://方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME") { alert ...
 - ACM第三题 完美立方
			
形如a3= b3 + c3 + d3的等式被称为完美立方等式.例如123= 63 + 83 + 103 .编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得 ...
 - (四)python自带解释器(LDIE)的使用
			
什么是IDE? Integrated Development Environment(集成开发环境) 打个不恰当的比方,如果说写代码是制作一件工艺品,那IDE就是机床.再打个不恰当的比方,PS就是图片 ...