在Ubuntu 20.04| 18.04/Debian 10 | 9上安装WKHTMLTOPDF和WKHTMLTOIMAGE

时间:2020-02-23 14:33:04  来源:igfitidea点击:

在本教程中,我们将在Ubuntu/Debian Linux系统上安装WKHTMLTopdf和WkhtmltoImage。
WKHTMLToPDF和WKHTMLToImage是开源(LGPLV3)命令行工具,用于将HTML渲染为PDF和各种图像格式,使用Qt WebKit渲染引擎。

Ubuntu/Debian Linux系统上可用的这些工具的版本旧。
为了获得Debian 10/Ubuntu 20.04/18.04 Linux上安装的最新WKHTMLTopdf&WKHTMLTMLIMAGE,我们需要手动下载和安装.deb包。
这些工具完全运行"无头",不需要显示或者显示服务。

在Ubuntu 20.04 | 18.04&debian 10 | 9上安装wkhtmltopdf

从发布页面下载最新的预编译二进制文件。
本文更新的最新版本为0.12.6.

在Ubuntu 20.04/18.04上安装WKHTMLTopdf

Ubuntu 20.04:

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb

Ubuntu 18.04:

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.bionic_amd64.deb
sudo apt install ./wkhtmltox_0.12.6-1.bionic_amd64.deb

Ubuntu 16.04:

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.xenial_amd64.deb
sudo apt install ./wkhtmltox_0.12.6-1.xenial_amd64.deb

成功的安装输出看起来像这样。

...
Unpacking xfonts-75dpi (1:1.0.4+nmu1) ...
Selecting previously unselected package xfonts-base.
Preparing to unpack .../17-xfonts-base_1%3a1.0.5_all.deb ...
Unpacking xfonts-base (1:1.0.5) ...
Selecting previously unselected package wkhtmltox.
Preparing to unpack .../18-wkhtmltox_0.12.6-1.focal_amd64.deb ...
Unpacking wkhtmltox (1:0.12.6-1.focal) ...
Setting up libxau6:amd64 (1:1.0.9-0ubuntu1) ...
Setting up libxdmcp6:amd64 (1:1.1.3-0ubuntu1) ...
Setting up libxcb1:amd64 (1.14-2) ...
Setting up x11-common (1:7.7+19ubuntu14) ...
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults
Setting up libfontenc1:amd64 (1:1.1.4-0ubuntu1) ...
Setting up libx11-data (2:1.6.9-2ubuntu1.1) ...
Setting up xfonts-encodings (1:1.0.5-0ubuntu1) ...
Setting up fonts-dejavu-core (2.37-1) ...
Setting up libjpeg-turbo8:amd64 (2.0.3-0ubuntu1.20.04.1) ...
Setting up libx11-6:amd64 (2:1.6.9-2ubuntu1.1) ...
Setting up libxrender1:amd64 (1:0.9.10-1) ...
Setting up fontconfig-config (2.13.1-2ubuntu3) ...
Setting up libxext6:amd64 (2:1.3.4-0ubuntu1) ...
Setting up xfonts-utils (1:7.7+6) ...
Setting up xfonts-base (1:1.0.5) ...
Setting up libfontconfig1:amd64 (2.13.1-2ubuntu3) ...
Setting up xfonts-75dpi (1:1.0.4+nmu1) ...
Setting up fontconfig (2.13.1-2ubuntu3) ...
Regenerating fonts cache... done.
Setting up wkhtmltox (1:0.12.6-1.focal) ...
Processing triggers for systemd (245.4-4ubuntu3.3) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...

在debian 10/debian 9上安装wkhtmltopdf

安装Wget命令行工具。

sudo apt update
sudo apt -y install wget

在Debian 10/9 Linux上安装WKHTMLTopdf&wkhtmltoImage

--- Debian 10 --
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb
sudo apt install ./wkhtmltox_0.12.6-1.buster_amd64.deb
--- Debian 9 --
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.stretch_amd64.deb
sudo apt install ./wkhtmltox_0.12.6-1.stretch_amd64.deb

检查wkhtmltopdf和wkhtmltoimage的版本以确认安装是否成功。

$wkhtmltopdf --version
wkhtmltopdf 0.12.6 (with patched qt)
$wkhtmltoimage --version
wkhtmltoimage 0.12.6 (with patched qt)

在Ubuntu/debian上使用wkhtmltopdff

例如,如果要将网页转换为PDF,请使用命令语法:

$ wkhtmltopdf <webpage> <destination-pdf-file>

请参阅以下示例。

wkhtmltopdf https://theitroad.com theitroad.pdf

执行

创建PDF文件,我们可以打开和查看内容。

$file theitroad.pdf 
theitroad.pdf: PDF document, version 1.4

转换为图像时,类似的语法适用。

$wkhtmltoimage https://theitroad.com theitroad.png
Loading page (1/2)
QFont::setPixelSize: Pixel size <= 0 (0)=============>       ] 88%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)==============>      ] 89%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)===============>     ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)===============>     ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)===============>     ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)===============>     ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)===============>     ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)
QFont::setPixelSize: Pixel size <= 0 (0)===============>     ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)=====================] 100%
QFont::setPixelSize: Pixel size <= 0 (0)
Rendering (2/2)                                                    
Done