wkhtmltopdf:一个 Linux 中将网页转成 PDF 的智能工具
![](http://static.careerengine.us/api/aov2/http%3A_%7C__%7C_mmbiz.qpic.cn_%7C_mmbiz_png_%7C_eQYGxygoIB8Hre9ice0We4jDiaVn3czbhkOCibF93Tnsxick8SICzqu3JGUjJUCjzq2N7HB6epo0vaZX3QYtElQkcg_%7C_0%3Fwx_fmt%3Dpng.jpg)
![](http://static.careerengine.us/api/aov2/http%3A_%7C__%7C_mmbiz.qpic.cn_%7C_mmbiz_png_%7C_eQYGxygoIB8Hre9ice0We4jDiaVn3czbhkOCibF93Tnsxick8SICzqu3JGUjJUCjzq2N7HB6epo0vaZX3QYtElQkcg_%7C_0%3Fwx_fmt%3Dpng.jpg)
wkhtmltopdf 是一个开源、简单而有效的命令行 shell 程序,它可以将任何 HTML (网页)转换为 PDF 文档或图像(jpg、png 等)。
wkhtmltopdf 是用 C++ 编写的,并在 GNU/GPL (通用公共许可证)下发布。它使用 WebKit 渲染引擎将 HTML 页面转换为 PDF 文档且不会丢失页面的质量。这是一个用于实时创建和存储网页快照的非常有用且可信赖的解决方案。
wkhtmltopdf 的功能
- 开源并且跨平台。
- 使用 WebKit 引擎将任意 HTML 网页转换为 PDF 文件。
- 添加页眉和页脚的选项
- 目录生成 (TOC) 选项。
- 提供批量模式转换。
- 通过绑定 libwkhtmltox 来支持 PHP 或 Python。
在本文中,我们将介绍如何在 Linux 系统下使用 tar 包来安装 wkhtmltopdf。
安装 Evince (PDF 浏览器)
让我们在 Linux 系统中安装 evince (一个 PDF 阅读器)来浏览 PDF 文件。
$ sudoyum install evince [RHEL/CentOSandFedora]
$ sudo dnf install evince [OnFedora22+ versions]
$ sudoapt-get install evince [OnDebian/Ubuntu systems]
下载 wkhtmltopdf 源码文件
在 64 位 Linux 系统中:
$ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
在 32 位 Linux 系统中:
$ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-i386.tar.xz
在 Linux 中安装 wkhtmltopdf
使用 tar 命令[3]解压文件到当前目录中。
------On64-bit Linux OS ------
$ sudotar-xvf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
------On32-bit Linux OS ------
$ sudotar-xvzf wkhtmltox-0.12.4_linux-generic-i386.tar.xz
为了能从任意路径执行程序,将 wkhtmltopdf 安装到
/usr/bin
目录下。$ sudocp wkhtmltox/bin/wkhtmltopdf /usr/bin/
如何使用 wkhtmltopdf?
我们会看到如何将远程的 HTML 页面转换成 PDF 文件、验证信息、使用 evince 在 GNOME 桌面中浏览创建的文件。
将 HTML 网页转成 PDF 文件
要将任意 HTML 页面转换成 PDF,运行下面的命令。它会在当前目录下将页面转换成 10-Sudo-Configurations.pdf[4]。
# wkhtmltopdf http://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/ 10-Sudo-Configurations.pdf
示例输出:
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
浏览生成的 PDF 文件
为了验证创建的文件,使用下面的命令。
$ file10-Sudo-Configurations.pdf
示例输出:
10-Sudo-Configurations.pdf: PDF document, version 1.4
浏览生成的 PDF 文件细节
要浏览生成的文件信息,运行下面的命令。
$ pdfinfo 10-Sudo-Configurations.pdf
示例输出:
Title:10UsefulSudoersConfigurationsforSetting'sudo'inLinux
Creator: wkhtmltopdf 0.12.4
Producer:Qt4.8.7
CreationDate:SatJan2813:02:582017
Tagged:no
UserProperties:no
Suspects:no
Form: none
JavaScript:no
Pages:13
Encrypted:no
Pagesize:595 x 842 pts (A4)
Page rot:0
Filesize:697827 bytes
Optimized:no
PDF version:1.4
浏览创建的文件
在桌面中使用 evince 查看最新生成的 PDF 文件。
$ evince 10-Sudo-Configurations.pdf
示例截图:
在我的 Linux Mint 17 中看起来很棒。
![](http://static.careerengine.us/api/aov2/http%3A_%7C__%7C_mmbiz.qpic.cn_%7C_mmbiz_png_%7C_eQYGxygoIB8Hre9ice0We4jDiaVn3czbhk1CMUhcSFrmFlEkl6MNYLyVZ7Ts1shbnXGFVwzibA0GY8oUUVLRWiaicFQ_%7C_0%3Fwx_fmt%3Dpng.jpg)
在 PDF 中浏览网页
给 PDF 创建页面的 目录(TOC)
要创建一个 PDF 文件的目录,使用 toc 选项。
$ wkhtmltopdf toc http://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/ 10-Sudo-Configurations.pdf
示例输出:
Loading pages (1/6)
Counting pages (2/6)
Loading TOC (3/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
要查看已创建文件的 TOC,再次使用 evince。
$ evince 10-Sudo-Configurations.pdf
示例截图:
看一下下面的图。它上看去比上面的更好。
![](http://static.careerengine.us/api/aov2/http%3A_%7C__%7C_mmbiz.qpic.cn_%7C_mmbiz_png_%7C_eQYGxygoIB8Hre9ice0We4jDiaVn3czbhkpL38DOKQanjibRye72SoiavvDh8hzibPZHvGpRQ2sicPANIWicVxpaegxnQ_%7C_0%3Fwx_fmt%3Dpng.jpg)
在 PDF 中创建网页的目录
wkhtmltopdf 选项及使用
更多关于 wkhtmltopdf 的使用及选项,使用下面的帮助命令。它会显示出所有可用的选项。
$ wkhtmltopdf --help
作者简介:
我是 Ravi Saive,TecMint 的创建者。一个爱在网上分享的技巧和提示的电脑极客和 Linux 专家。我的大多数服务器运行在名为 Linux 的开源平台上。请在 Twitter、 Facebook 和 Google+ 等上关注我。
推荐文章
< 左右滑动查看相关文章 >
输入文章 ID 或长按二维码直达
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。