解决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 ...
随机推荐
- 初始化IoC容器(Spring源码阅读)
初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...
- .Net程序员面试试题
1:简述private protected public internal修饰符的访问权限? private:私有成员在类的内部可以访问: protected:受保护的成员,在类的内部和继承类中可以访 ...
- Visual Studio 2012设置Jquery/Javascript智能提示
Visual Studio 2012设置Jquery/Javascript智能提示 在Visual Studio 2008 Visual Studio 2010中微软已经开始支持jquery/java ...
- 读书笔记:《HTML5开发手册》Web表单
这是补充HTML5基础知识的第五篇内容,其他为: 一.HTML5-- 新的结构元素 二.HTML5-- figure.time.details.mark 三.HTML5-- details活学活用 四 ...
- java基础练习 4
import java.util.Scanner; public class Forth { public static void main(String[] args){ /*请输入星期几的第一个字 ...
- Jdon框架开发指南
Jdon框架快速开发指南 开发主要步骤如下: JdonFramework6.0以上两步开发见这里. 快速配置指南 新增/查询/修改/删除(CRUD); 批量查询和分页显示 本文Step By Step ...
- HTML5之canvas细节详谈
一.canvas基础 1.canvas是HTML5 的新标签,其默认宽高为300*150. canvas 元素本身是没有绘图能力的.所有的绘制工作必须在 JavaScript 内部完成. ...
- codewars-random(4)
问题简介:算多长时间用本金能得到想要的利息,基础题. 代码:function calculateYears(principal, interest, tax, desired) { // you ...
- iOS 10 之后,相机权限问题及易出现的Crash
1: iOS 10 之后,访问相机需要设置相关的权限 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Priva ...
- Python中is和==的区别的
在python中,is检查两个对象是否是同一个对象,而==检查他们是否相等. str1 = 'yangshl' str2 = 'yang' + 'shl' print('str1 == str2:', ...