CentOS 中安装和使用 wkhtmltopdf 和 wkhtmltoimage

工具说明

wkhtmltopdfwkhtmltoimage 是一个开源的命令行工具,可以将 HTML 转换为 pdf 文档和图片。

安装

查看系统版本

CentOS 查看版本信息

cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

我这边是 CentOS 8

下载 RPM 包

安装之前需要下载依赖

$ yum install -y xorg-x11-fonts-75dpi
$ yum install -y xorg-x11-fonts-Type1

官网下载 RPM

下载页面

选中自己系统对应版本的版本下载,可以直接点击下载或者复制地址 https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos8.x86_64.rpm 下载

$ wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos8.x86_64.rpm

下载完成文件 wkhtmltox-0.12.6-1.centos8.x86_64.rpm

在系统中安装RMP包

$ rpm -Uvh wkhtmltox-0.12.6-1.centos8.x86_64.rpm
Preparing...                ########################################### [100%]
   1:wkhtmltox              ########################################### [100%]

如上,已经成功安装了,默认安装路径 /usr/local/bin/wkhtmltopdf

$ whereis wkhtmltopdf
wkhtmltopdf: /usr/local/bin/wkhtmltopdf

查看安装版本

$ /usr/local/bin/wkhtmltopdf --version
wkhtmltopdf 0.12.6 (with patched qt)

用法介绍

  • 网络资源地址 url 转为 pdf
$ /usr/local/bin/wkhtmltopdf http://www.baidu.com baidu.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

百度首页 http://www.baidu.com 转为 baidu.pdf 文件

  • 本地 html 转为 pdf
$ /usr/local/bin/wkhtmltopdf test.html test.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

更多用法查看 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

遇到的问题

使用过程中报错

error while loading shared libraries: libpng12.so.0: cannot open shared object file: No such file or directory

这种一般是安装版本不匹配问题,需要 CentOS版本和 RPM 版本一致

转换过程中文不显示

查看 Linux 是否有中文字体

$ fc-list :lang=zh

无任何输出说明没有中文字体

安装中文字体 simsun.ttc

字体下载 http://www.font5.com.cn/fontlist/fontlist_1_1.html
将下载后的字体放到文件夹 /usr/share/fonts

$ ll /usr/share/fonts/
总用量 18M
drwxr-xr-x 4 root root 4.0K 4月  13 2017 default
-rwxr-xr-x 1 root root  18M 8月   5 11:38 simsun.ttc
$ fc-list :lang=zh
新宋体,NSimSun:style=常规,Regular
宋体,SimSun:style=常规,Regular

你可能感兴趣的:(CentOS 中安装和使用 wkhtmltopdf 和 wkhtmltoimage)