Do 您需要一个简单的开源跨平台命令行工具,将网页和 HTML 转换为 PDF 文件? 不要再犹豫了,试试 wkhtmltopdf。
从项目主页:
使用 webkit 渲染引擎和 qt 将 html 转换为 pdf 的简单 shell 实用程序。 在网上搜索,我发现了几个命令行工具,它们允许您将 HTML 文档转换为 PDF 文档,但是它们似乎都使用了自己的渲染引擎,而且渲染引擎相当不完整,导致质量很差。 最近QT 4.4 发布了一个WebKit 小部件(WebKit 是Apples Safari 的引擎,它是KDE KHtml 的一个分支),制作一个好的工具变得非常容易。
软件功能
- 跨平台。
- 开源。
- 使用 webkit 将任何网页转换为 PDF 文档。
- 您可以添加页眉和页脚。
- TOC 生成。
- 批处理模式转换。
- 可以在带有 XServer 的 Linux 服务器上运行(必须安装 X11 客户端库)。
- 可以通过绑定到 libwkhtmltox 直接由 PHP 或 Python 使用。
关于 Debian / Ubuntu Linux 用户的说明
您可以使用 apt-get 命令安装 wkhtmltopdf:$ sudo apt-get install wkhtmltopdf
$ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf
示例输出:
[sudo] password for vivek: Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: wkhtmltopdf 0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded. Need to get 116 kB of archives. After this operation, 303 kB of additional disk space will be used. Get:1 https://debian.osuosl.org/debian/ squeeze/main wkhtmltopdf amd64 0.9.9-1 [116 kB] Fetched 116 kB in 2s (49.4 kB/s) Selecting previously deselected package wkhtmltopdf. (Reading database ... 274164 files and directories currently installed.) Unpacking wkhtmltopdf (from .../wkhtmltopdf_0.9.9-1_amd64.deb) ... Processing triggers for man-db ... Setting up wkhtmltopdf (0.9.9-1) ...
在 MacOS unix 上安装 wkhtmltopdf
首先,在 macOS 上安装 Homebrew,然后键入以下 brew 命令:$ brew install wkhtmltopdf
或者$ brew cask install wkhtmltopdf
下载 wkhtmltopdf
访问这个 页 抓取 Linux / MS-Windows / Apple Mac OS X 的 wkhtmltopdf。您也可以使用 wget 命令,如下所示:$ wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
示例输出:
Resolving wkhtmltopdf.googlecode.com... 74.125.135.82, 2404:6800:4001:c01::52 Connecting to wkhtmltopdf.googlecode.com|74.125.135.82|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 11175276 (11M) [application/octet-stream] Saving to: `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2' 100%[======================================>] 1,11,75,276 480K/s in 23s 2012-10-04 01:21:43 (477 KB/s) - `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2' saved [11175276/11175276]
Linux下安装wkhtmltopdf
键入以下 tar 命令以提取文件:$ tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
示例输出:
wkhtmltopdf-amd64
在您的私有 ~/bin/ 目录或 /usr/local/bin 目录中安装相同的内容:$ mv wkhtmltopdf-amd64 ~/bin/
ln -s ~/bin/wkhtmltopdf-amd64 ~/bin/html2pdf
或者$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/
ln -s /usr/local/bin/wkhtmltopdf-amd64 /usr/local/bin/html2pdf
如何使用 wkhtmltopdf?
语法如下:
html2pdf https://www.cyberciti.biz/path/to/url.html output.pdf html2pdf https://www.cyberciti.biz/blog/print/url-slut.html output.pdf html2pdf -option1 -option2 https://www.cyberciti.biz/blog/print/url-slut.html output.pdf
或者
wkhtmltopdf https://www.cyberciti.biz/path/to/url.html output.pdf wkhtmltopdf https://www.cyberciti.biz/blog/print/url-slut.html output.pdf wkhtmltopdf -option1 -option2 https://www.cyberciti.biz/blog/print/url-slut.html output.pdf
示例:简单的 html 到 pdf 文件
在此示例中,转换为 bash for循环页面到pdf文件:$ html2pdf https://www.cyberciti.biz/faq/bash-for-loop/print/ /tmp/bash.for.loop.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文件点击这里. 验证pdf文件,输入:$ file /tmp/bash.for.loop.pdf
示例输出:
/tmp/bash.for.loop.pdf: PDF document, version 1.4
使用 pdfinfo 命令从可移植文档格式 (PDF) 文件中打印“信息”字典的内容(以及一些其他有用的信息):$ pdfinfo /tmp/bash.for.loop.pdf
示例输出:
Title: Frequently Asked Questions About Linux / UNIX » Bash For Loop Examples » Print Creator: Producer: wkhtmltopdf CreationDate: Thu Oct 4 01:29:33 2012 Tagged: no Pages: 4 Encrypted: no Page size: 595 x 842 pts (A4) File size: 98792 bytes Optimized: no PDF version: 1.4
灰度pdf
将以灰度生成以下 PDF:$ html2pdf -g https://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
将方向设置为横向或纵向
使用以下语法:$ html2pdf -O Landscape https://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
在哪里,
- -O 风景|肖像。 默认为纵向。
如何设置页面大小?
使用以下语法:$ html2pdf -S SIZE https://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
在哪里,
- -s 尺寸:将纸张尺寸设置为:A4、Letter 等(默认 A4)
如何生成目录?
通过添加 toc 命令行选项,可以将目录添加到文档中。 例如:$ html2pdf toc https://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
示例输出:
请注意,目录是根据输入文档中的 H 标签生成的。
如何查看所有可用选项?
可以看到常用选项列表,输入:$ wkhtmltopdf --help
或查看所有可用选项,即显示更广泛的帮助,详细说明不太常见的命令开关,运行:$ wkhtmltopdf -H | less
参考:
- wkhtmltopdf 项目主页。