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中文字体显示问题的更多相关文章

  1. html lang="zh-cn"解决Mac版Firefox中文字体显示问题

    这两天在Mac下被Firefox的中文字体显示问题所困扰.在Firefox中将Sans-serif字体设置为SimSun-ExtB(新宋体)或英文字体(这时会用Mac默认中文字体),如下图: 浏览园子 ...

  2. 关于JAVA应用中文字体显示小方框的问题解决

    最近碰到linux下jboss应用中中文字体显示为小方框: “在JRE 5以上的java环境中,java会自动加载$JAVA_HOME/jre/lib/fonts目录下的字体.链接或复制宋体或微软雅黑 ...

  3. 解决Ubuntu系统中文乱码显示问题,终端打开文件及查看目录

    解决Ubuntu系统中文乱码显示问题 [日期:2014-02-20] 来源:Linux社区  作者:njchenyi [字体:大 中 小]   我是先安装了Ubuntu 12.04 Server,然后 ...

  4. php导出pdf,dompdf中文字体乱码解决办法(特别是代码迁移引起的乱码)

    dompdf\lib\fonts\dompdf_font_family_cache.php记住这个文件里面存放的是字体生成的缓存,迁移时如果覆盖了这个文件会导致乱码而且很难找到出错的地方,相信我... ...

  5. matplotlib在MAC系统下中文字体显示问题

    最近想把部分python数据分析的代码从win系统迁移到MAC上,有部分图片上涉及中文显示,迁移到MAC上warning: UserWarning: findfont: Font family [u' ...

  6. 关于Matlab在绘图时中文字体显示不一致的问题

    我的运行环境: OS: Win10 教育版 64-bit Matlab版本:Matlab 2017a  64-bit 在使用Matlab绘图时,而横坐标轴.纵坐标轴.标题有汉字时,会发现在GUI的显示 ...

  7. 解决Android版Firefox字体显示过大的问题

    在用Android版Firefox查看博客园首页发现中间区域的字体显示非常大,开始以为是首页css对移动版浏览器支持不好. 后来发现原来这是Firefox for Android的知名bug: Tha ...

  8. deepin 下安装goland中文字体显示全是方块

    下载中文字体 apt-get install ttf-arphic-uming xfonts-intl-chinese 替换goland的汉化包,两个jar包.https://blog.csdn.ne ...

  9. wine的中文字体显示

    从1.1.4开始wine的界面就已经支持中文了,但是对于软件中的中文支持并不太好,主要原因.还是字体...Let's go 首先,copy一下字体:把simsun.ttc (即宋体)复制到 ~/.wi ...

随机推荐

  1. java用正则表达式获取domain

    在工作中经常用到获取url的来源和域名的黑白名单功能.前段时间写了一个获取url中域名的方法.但是在测试过程中发现有些小问题. /** * 根据URL获取domain * @param url * @ ...

  2. Centos6.3不能使用yum install安装gcc编辑器解决办法

    mv /var/lib/rpm/__db* /tmp rpm --rebuilddb yum clean all

  3. 谷歌、flick网站图片 一次性下载 javaWeb项目 多线程下载,

    源码下载:http://download.csdn.net/detail/liangrui1988/5760473

  4. Fixjs实践——标签、按钮控件

    Fixjs介绍 Fixjs是一款javascript界面基础框架,主要为开发复杂组件提供底层的框架支持. Fixjs 0.3.0主要增加了文本显示类fixjs.text.TextField 支持的主要 ...

  5. 编程利用利用curses库编程开始

    时间紧张,先记一笔,后续优化与完善. curses库常用函数: 注意编译时要用这样的格式:gcc xxx.c -l curses -o xxx 第一个小例子: include <stdio.h& ...

  6. Swift3.0服务端开发(三) Mustache页面模板与日志记录

    本篇博客主要介绍如果在Perfect工程中引入和使用Mustache页面模板与日志记录系统.Mustache页面模板类似于PHP中的smarty模板引擎或者Java中的JSTL标签.当然Mustach ...

  7. CentOS在线升级内核

    升级内核需要使用 elrepo 的yum 源首先我们导入 elrepo 的key  rpm –import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org ...

  8. springMVC框架中json处理

    1.在项目中导入jackson-core.jar包 2.使用@ResponseBody注解,此注解会自动装换为json 如图: 3.如此,便可以得到json对象

  9. 卸载jdk以及重新安装jdk

    新旧交替重复安装会混乱,个人解决办法是: 1. 用系统 control panel 中 uninstall 卸载java se development kit 和 java update. 2. 打开 ...

  10. 第六届全球游戏大会( GMGC 北京 2017 )有哪些看点?

    全球游戏大会作为亚太地区极具影响力的全球性游戏行业盛会,本次第六届全球游戏大会( GMGC 北京 2017 )将力邀全球50个国家.500多位业界领袖.5000多家企业.50000多位业界精英参展参会 ...