ego008 avatar

Debian 安装 wkhtmltopdf 及中文乱码解决记

🕦 by ego008

wkhtmltopdf 是一款很优秀的 HTML 渲染为 PDF 的工具,因为各系统的安装包不同,这里做一个安装过程笔记以备忘。

编译安装更麻烦,官方发布的各平台安装包,如果有就直接下载

首先查看自己的 VPS 的操作系统及版本

Bash: os-release
1
2
3
4
5
6
7
8
9
10
11
12
# uname -a
Linux localhost 4.19.0-5-amd64 #1 SMP Debian 4.19.37-5+deb10u2 (2019-08-08) x86_64 GNU/Linux
# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

这里是 debian 10 amd64

https://wkhtmltopdf.org/downloads.html 下载对应的包,

Bash: dpkg install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# dpkg -i wkhtmltox_0.12.6-1.buster_amd64.deb 
Selecting previously unselected package wkhtmltox.
(Reading database ... 35676 files and directories currently installed.)
Preparing to unpack wkhtmltox_0.12.6-1.buster_amd64.deb ...
Unpacking wkhtmltox (1:0.12.6-1.buster) ...
dpkg: dependency problems prevent configuration of wkhtmltox:
 wkhtmltox depends on fontconfig; however:
  Package fontconfig is not installed.
 wkhtmltox depends on libjpeg62-turbo; however:
  Package libjpeg62-turbo is not installed.
 wkhtmltox depends on libxrender1; however:
  Package libxrender1 is not installed.
 wkhtmltox depends on xfonts-75dpi; however:
  Package xfonts-75dpi is not installed.
 wkhtmltox depends on xfonts-base; however:
  Package xfonts-base is not installed.

dpkg: error processing package wkhtmltox (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 wkhtmltox

错误,安装依赖,

Bash: apt-get
1
2
apt-get install fontconfig libjpeg62-turbo libxrender1 xfonts-utils xfonts-75dpi xfonts-base
apt --fix-broken install

安装中文字体,否则会中文都变成方块,我这里是下载一款 Google font,你也可以用其它中文字体

Bash: fonts
1
# wget https://fonts.google.com/download?family=Noto%20Sans%20SC -O Noto.zip

解压,把所有的 .otf 文件拷贝到 /usr/share/fonts/zh_CN/

Bash:
1
2
3
4
5
6
7
8
# unzip Noto.zip
  inflating: NotoSansSC-Thin.otf     
  inflating: NotoSansSC-Light.otf    
  inflating: NotoSansSC-Regular.otf  
  inflating: NotoSansSC-Medium.otf   
  inflating: NotoSansSC-Bold.otf     
  inflating: NotoSansSC-Black.otf
# cp *.otf /usr/share/fonts/zh_CN/

运行 wkhtmltopdf 试试

Bash: wkhtmltopdf
1
2
3
4
5
6
7
8
9
10
# wkhtmltopdf https://youbbs.org/t/3292 3292.pdf
Loading pages (1/6)
Warning: A finished ResourceObject received a loading finished signal. This might be an indication of an iframe taking too long to load.
Warning: A finished ResourceObject received a loading progress signal. This might be an indication of an iframe taking too long to load.
Counting pages (2/6)
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done                                                                      
Warning: Received createRequest signal on a disposed ResourceObject's NetworkAccessManager. This might be an indication of an iframe taking too long to load.

出错提示是 Google adsense js 没加载

效果如下图

相关资源

💘 相关文章

写一条评论

Based on Golang + fastHTTP + sdb | go1.17.1 Processed in 1ms