如何使用wkhtmltopdf将URL转换为PDF格式

时间:2020-03-21 11:48:12  来源:igfitidea点击:

wkhtmltopdf是一个极好的工具,可用于将给定的url或者本地html页面转换为pdf格式。安装后,这个工具可以嵌入到php代码、shell脚本或者任何其他脚本语言中。

有几种基于web的在线工具,我们可以其中指定需要转换为pdf的URL作为输入,并下载转换后的pdf文件作为输出。

但是在控制台终端中工作时不能使用这些工具,或者在脚本中需要该功能时,这些工具不是一个可行的选项。

wkhtml to pdf使用webkit引擎和qt将html转换为pdf。此软件包可从googlecode.com

我们在一台ubuntu机器上进行了测试…(同样适用于基于redhat的机器)。

步骤1:安装wkhtmltopdf

安装wkhtml到pdf可以通过apt-get在ubuntu机器上完成,在其他机器上,比如redhat,你可以下载targz包来自

code.google.com/p/wkhtmltoppdf/.

我们可以使用下面的命令在wkhtmltopdf中获取所有可用的标签选项。

$wkhtmltopdf --help

在wkhmltopdf的帮助下,我们可以通过下面的方法轻松创建大多数pdf文件中可用的目录。

一些常见的选项如下。

--collate                                Collate when printing multiple copies
 --copies <number>               Number of copies to print into the pdf
                                             file (default 1)
 --extended-help                    Display more extensive help, detailing
                                             less common command switches

-h, --help                               Display help
-O, --orientation <orientation>     Set orientation to Landscape or Portrait
-s, --page-size <size>              Set paper size to: A4, Letter, etc.
--password <password>           HTTP Authentication password
-p, --proxy <proxy>                 Use a proxy
-q, --quiet                               Be less verbose
--username <username>           HTTP Authentication username
-V, --version                            Output version information an exit