wkhtmltox 是一个开源的将网页内容转换成PDF的软件包,常嵌套在网页页面里边做打印功能。
ubuntu16.04 安装wkhtmltopdf参考:https://www.cnblogs.com/hellojesson/p/11206184.html
以微软雅黑字体为例(其他的宋体、黑体等点阵字体都一样的),我们的雅黑字体文件是:Yahei.ttf(放在自己的主目录下)(在widows目录的Fonts目录下找需要的字体)
由于我是双系统,我直接从Win7中复制了 msyh.ttf和msyhbd.ttf 到Ubuntu下;如果在docker容器里边,需要在将字体复制到容器里边。
windows系统,字体文件的目录一般在:C:WindowsFonts
开始操作:
(注1:这里操作系统以ubuntu系统为例,其他系统操作原理一样,请做相应调整。)
(注2:如果你的程序用了Docker部署,记得相应的操作是在容器里边,而不是宿主机上。)
1. 在/usr/share/fonts/下新建文件夹 如:winFonts目录,准备把从windows下拷贝过来的字体都放在这个目录里,这里就是把雅黑字体Yahei.ttf 放进来
sudo mkdir -p /usr/share/fonts/winFonts
sudo mkdir -p /usr/share/fonts/winFonts
2. 然后将字体文件拷贝到此文件夹下
sudo cp /home/username(用户名)/fonts/Yahei.ttf /usr/share/fonts/winFonts/
sudo cp /home/username(用户名)/fonts/Yahei.ttf /usr/share/fonts/winFonts/
3. 然后,改变权限:
sudo chmod 644 /usr/share/fonts/winFonts/Yahei.ttf
sudo chmod 644 /usr/share/fonts/winFonts/Yahei.ttf
(改成755也没问题的,但起码要444,修改雅黑字体文件的权限为644,即当前用户读写,当前组用户只读,其他用户只读)
4. 开始安装:
root@hellojesson:/usr/share/fonts/winFonts# chown root msyh* root@hellojesson:/usr/share/fonts/winFonts# ls -l total 35524 -rwxrwxr-x 1 root root 21767952 Oct 21 04:53 msyh.ttf -rwxrwxr-x 1 root root 14602860 Oct 21 04:52 msyhbd.ttf root@hellojesson:/usr/share/fonts/winFonts# root@hellojesson:/usr/share/fonts/winFonts# root@hellojesson:/usr/share/fonts/winFonts# root@hellojesson:/usr/share/fonts/winFonts# root@hellojesson:/usr/share/fonts/winFonts# mkfontscale root@hellojesson:/usr/share/fonts/winFonts# mkfontdir root@hellojesson:/usr/share/fonts/winFonts# ls fonts.dir fonts.scale msyh.ttf msyhbd.ttf root@hellojesson:/usr/share/fonts/winFonts# fc-cache -fv /usr/share/fonts: caching, new cache contents: 0 fonts, 4 dirs /usr/share/fonts/X11: caching, new cache contents: 0 fonts, 4 dirs /usr/share/fonts/X11/75dpi: caching, new cache contents: 358 fonts, 0 dirs /usr/share/fonts/X11/encodings: caching, new cache contents: 0 fonts, 1 dirs /usr/share/fonts/X11/encodings/large: caching, new cache contents: 0 fonts, 0 dirs /usr/share/fonts/X11/misc: caching, new cache contents: 59 fonts, 0 dirs /usr/share/fonts/X11/util: caching, new cache contents: 0 fonts, 0 dirs /usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 2 dirs /usr/share/fonts/truetype/dejavu: caching, new cache contents: 6 fonts, 0 dirs /usr/share/fonts/truetype/liberation: caching, new cache contents: 16 fonts, 0 dirs /usr/share/fonts/type1: caching, new cache contents: 0 fonts, 1 dirs /usr/share/fonts/type1/gsfonts: caching, new cache contents: 35 fonts, 0 dirs /usr/share/fonts/winFonts: caching, new cache contents: 2 fonts, 0 dirs /usr/X11R6/lib/X11/fonts: skipping, no such directory /usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs /root/.local/share/fonts: skipping, no such directory /root/.fonts: skipping, no such directory /usr/share/fonts: caching, new cache contents: 0 fonts, 4 dirs /usr/X11R6/lib/X11/fonts: skipping, no such directory /usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs /root/.local/share/fonts: skipping, no such directory /root/.fonts: skipping, no such directory /var/cache/fontconfig: cleaning cache directory /root/.cache/fontconfig: not cleaning non-existent cache directory /root/.fontconfig: not cleaning non-existent cache directory fc-cache: succeeded
---异常处理1:执行mkfontscale报错: 处理 apt install xfonts-utils
root@ubuntu:/usr/share/fonts/winFonts# mkfontscale The program 'mkfontscale' is currently not installed. You can install it by typing: apt install xfonts-utils
---异常处理2:
root@ubuntu:/usr/share/fonts/winFonts# fc-cache -fv The program 'fc-cache' is currently not installed. You can install it by typing: apt install fontconfig
cd /usr/share/fonts/winFonts/
sudo mkfontscale (创建雅黑字体的fonts.scale文件,它用来控制字体旋转缩放)
sudo mkfontdir (创建雅黑字体的fonts.dir文件,它用来控制字体粗斜体产生)
sudo fc-cache -fv (建立字体缓存信息,也就是让系统认识认识雅黑)
sudo mkfontdir (创建雅黑字体的fonts.dir文件,它用来控制字体粗斜体产生)
sudo fc-cache -fv (建立字体缓存信息,也就是让系统认识认识雅黑)
5. 尝试打印相关网页页面:终端命令行执行
wkhtmltopdf https://www.baidu.com/ baidu.pdf
wkhtmltopdf https://www.taobao.com/ taobao.pdf
参考:
http://huqiji.iteye.com/blog/2278225
http://blog.csdn.net/shenshen123jun/article/details/17334857
http://blog.csdn.net/vmboys/article/details/54581369
http://www.laozuo.org/9998.html
http://www.cnblogs.com/kfx2007/p/5926467.html
http://wiki.ubuntu.org.cn/%E5%AD%97%E4%BD%93
http://blog.163.com/bjcclzw@126/blog/static/129968414201281124457883/