在Ubuntu 22.04上安装wkhtmltopdf和wkhtmltoimage

本文介绍在Ubuntu 22.04系统上安装wkhtmltopdf和wkhtmltoimage的方法,附基本的使用方法。wkhtmltopdf和wkhtmltoimage是开源LGPLv3命令行工具,用于使用Qt WebKit渲染引擎将HTML渲染为PDF和各种图像格式,当前Ubuntu、Debian系统上可用的这些工具的版本是旧的,如果要安装新的,需要手动下载软件包并安装。

安装wkhtmltopdf和wkhtmltoimage

从发布页面下载最新的预编译二进制文件,地址在https://wkhtmltopdf.org/downloads.html,截至本文更新的最新版本为0.12.6:

在Ubuntu 22.04上安装wkhtmltopdf和wkhtmltoimage

安装wget实用程序包:

sudo apt update

sudo apt install wget

在Ubuntu 22.04上下载安装wkhtmltopdf:

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb

sudo apt install ./wkhtmltox_0.12.6.1-2.jammy_amd64.deb

同时,也提供arm64和ppc64el版本,下载地址分别为https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_arm64.deb、https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_ppc64el.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-2.jammy_amd64.deb ...

Unpacking wkhtmltox (1:0.12.6-1.jammy) ...

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.jammy) ...

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) ...

检查wkhtmltopdf和wkhtmltoimage的版本以确认安装成功:

$ wkhtmltopdf --version

wkhtmltopdf 0.12.6 (with patched qt)

$ wkhtmltoimage --version

wkhtmltoimage 0.12.6 (with patched qt)

基本使用wkhtmltopdf的方法

比如,需要将网页转换为PDF格式,可以使用以下语法达到目的:

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

例子:

wkhtmltopdf https://www.hmxthome.com/ hmxthome.pdf

或者:wkhtmltopdf https://computingforgeeks.com computingforgeeks.pdf

以下是输出示例:

在Ubuntu 22.04上安装wkhtmltopdf和wkhtmltoimage

将创建一个pdf文件,您可以打开并查看内容:

$ file computingforgeeks.pdf 

computingforgeeks.pdf: PDF document, version 1.4

类似的语法在转换为图像时仍然适用,如下图所示:

在Ubuntu 22.04上安装wkhtmltopdf和wkhtmltoimage

看到以上截图输出内容,表示使用wkhtmltopdf达到目的。

栏目相关文章