解决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 ...
随机推荐
- zabbix实现对磁盘动态监控
zabbix实现对磁盘动态监控 前言 zabbix一直是小规模互联网公司服务器性能监控首选,首先是免费,其次,有专门的公司和社区开发维护,使其稳定性和功能都在不断地增强和完善.zabbix拥有详细的U ...
- Union 与 Union all 区别
原创,请园长不要删 Sql查询统计时,很多时候用到了union 和 union all,union与union all的区别就是联合查询的时候union会去重,union all不会去重.本人用uni ...
- Nginx学习之十四-GDB调试Nginx初试
本文的测试环境: Win7+虚拟机VMWareVMware-workstation-full-7.1.4-385536+Ubuntu12.04 Nginx-1.4.0 要想有效的研究Nginx源码,必 ...
- 深入浅出学习Spring框架(三):AOP 详解
AOP的英文解释——AOPAspect Oriented Programming面向切面编程.主要目的是通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. 在反 ...
- log4j2日志配置
背景 log4j2相对于log4j 1.x有了脱胎换骨的变化,其官网宣称的优势有多线程下10几倍于log4j 1.x和logback的高吞吐量.可配置的审计型日志.基于插件架构的各种灵活配置等. 1. ...
- 当我们在谈论kmeans(5)
本系列意在长期连载分享,内容上可能也会有所删改: 因此如果转载,请务必保留源地址,非常感谢! 博客园:http://www.cnblogs.com/data-miner/(暂时公式显示有问题) 其他: ...
- MVC源码解析 - 目录
尽管MVC6和MVC4,5已经有很大不同, 但是, 作为一个普通开发人员, 还真没有资格去选择使用哪个版本. So, 尽管已经是old的版本, 还是再次花点时间去温故知新. 我记得在15年初的时候, ...
- oracle 增加表空间
1.表空间使用情况 SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", D ...
- Could not locate executable E:\SoftWave\Hadoop-2.2.0\bin\winutils.exe in the Hadoop binaries解决办法
需要下载windows版本 bin目录下的文件,替换hadoop目录下原来的bin目录下的文件.下载网址是: https://github.com/srccodes/hadoop-common-2.2 ...
- Webservice WCF WebApi
注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Service ...