laravelPDF 导出包 dompdf 和 snappy 的使用

背景:工作需求要有个HTML转PDF功能,先是使用了laravel-dompdf,出现中文乱码问题,解决后发现还是没法显示HTML标签的中文,于是改为laravel-snappy,完整展示HTML标签内容

1.laravel-dompdf

下载安装使用:GitHub
中文乱码解决:

  1. 下载中文msyh.TTF丢到/public下
  2. 在HTML中添加对这个字体的引用
    @font-face {
    font-family: 'msyh';
    font-style: normal;
    font-weight: normal;
    src: url({{ asset('/msyh.TTF') }}) format('truetype');
    }

    3.这样就可以通过传参到view转为PDF,显示出中文
    4.但是奇怪的是一些标签里的中文还是问号,加上这个帖子里Summer大神等大部分人都推荐用laravel-snappy,速度快,内存少;于是就换包

2.laravel-snappy

下载安装使用:GitHub

Windows下使用

1.下载wkhtmltopdf,根据版本选择(我是win7/64位所以选第一个win64)
2.安装后测试,到bin的目录下(wkhtmltopdf.exe那个)打开终端可以用命令:

wkhtmltopdf https://www.baidu.com 1.pdf

运行后就会把网页保存为PDF到当前这个目录;
3.在laravel项目的config/snappy.php的wkhtmltopdf的binary填写wkhtmltopdf.exe的路径

binary=>'D:\laragon\www\source\wkhtmltopdf\bin\wkhtmltopdf.exe'

Windows下laravel-snappy就可以使用了

Linux下使用

1.根据自身系统版本(centos7/64)下载对应文件

wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz

2.安装,然后拷贝到usr/bin使得可以调用到

tar xvfJ wkhtmltox-0.12.3_linux-generic-amd64.tar.xz

3.在config/snappy.php的binary填写wkhtmltopdf路径

  binary=>'/usr/bin/wkhtmltopdf'

4.这样就可以正常使用了,但是可能会碰到一些问题,比如

1.error while loading shared libraries: libXrender.so.1
解决:yum install libXrender
2.error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
解决:yum install fontconfig urw-fonts
3.while loading shared libraries: libXext.so.6:
解决:yum install libXext

总体感受:

  • dompdf要慢一点
  • snappy因为是基于wkhtmltopdf,跟浏览器一样的渲染引擎,所以没有乱码的烦恼

坚持开源,坚持分享 :)

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 7

win下 laravel-snappy 还是有中文乱码

$pdf = \App::make('snappy.pdf.wrapper');
            $pdf->loadHTML('<h1>Test测试</h1>');
            return $pdf->inline();

file

6年前 评论
The exit status code '127' says something went wrong:\n
stderr: "/usr/bin/wkhtmltopdf: error while loading shared libraries: libXrender.so.1: cannot open shared object file: No such file or directory\n
"\n
stdout: ""\n
command: /usr/bin/wkhtmltopdf --lowquality '/tmp/knp_snappy5c2f16f0ae57f6.42950400.html' '/tmp/knp_snappy5c2f16f0ae61f8.02235555.pdf'.

The exit status code '127' says something went wrong:\n
stderr: "/usr/bin/wkhtmltopdf: error while loading shared libraries: libXext.so.6: cannot open shared object file: No such file or directory\n
"\n
stdout: ""\n
command: /usr/bin/wkhtmltopdf --lowquality '/tmp/knp_snappy5c2f228876b3d0.69052054.html' '/tmp/knp_snappy5c2f228876ba12.18901763.pdf'.

yum install libXrender fontconfig urw-fonts libXext

linux 上也是乱码 ,地址 http://118.24.158.116:9876/pdf

6年前 评论
bestcyt

@lovecn 中文字体问题,Linux安装wqy-zenhei-fonts这个字体试试

6年前 评论

@bestcyt 装完后还要什么操作,现在还是不行


[root@VM_0_14_centos blog]# yum install -y wqy-zenhei-fonts
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * webtatic: uk.repo.webtatic.com
Resolving Dependencies
--> Running transaction check
---> Package wqy-zenhei-fonts.noarch 0:0.9.46-11.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================

 Package                                Arch                         Version                              Repository                Size =========================================================================================================================================

Installing:
 wqy-zenhei-fonts                       noarch                       0.9.46-11.el7                        os                       7.7 M

Transaction Summary
=========================================================================================================================================

Install  1 Package

Total download size: 7.7 M
Installed size: 16 M
Downloading packages:
wqy-zenhei-fonts-0.9.46-11.el7.noarch.rpm              6% [==-                                         ]  0.0 B/s | 529 kB  --:--:-- ETA wqy-zenhei-fonts-0.9.46-11.el7.noarch.rpm             35% [===============-                            ] 3.0 MB/s | 2.8 MB  00:00:01 ETA wqy-zenhei-fonts-0.9.46-11.el7.noarch.rpm                                                                         | 7.7 MB  00:00:00

Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wqy-zenhei-fonts-0.9.46-11.el7.noarch                                                                                 1/1

  Verifying  : wqy-zenhei-fonts-0.9.46-11.el7.noarch                                                                                 1/1

Installed:
  wqy-zenhei-fonts.noarch 0:0.9.46-11.el7

Complete!
6年前 评论
bestcyt

@lovecn 字体加到/usr/share/fonts/ 里

6年前 评论

@bestcyt 安装后默认就在那个目录

[root@VM_0_14_centos ~]# ll /usr/share/fonts/wqy-zenhei/
total 16708
-rw-r--r-- 1 root root 17083583 May  2  2011 wqy-zenhei.ttc
6年前 评论

为什么我的是权限问题啊{
"status": "error",
"code": 400,
"message": "The exit status code '126' says something went wrong:\nstderr: \"sh: \/usr\/local\/bin\/wkhtmltopdf: Permission denied\n\"\nstdout: \"\"\ncommand: \/usr\/local\/bin\/wkhtmltopdf --lowquality --page-size 'a4' '\/tmp\/knp_snappy5dca27d35600b4.48048425.html' 'uploads\/myfile.pdf'."
}

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!