Wkhtmltopdf python包装器,使用webkit渲染引擎和qt将html转换为图像
项目描述
IMGKit:HTML到IMG包装的Python库
_____ __ __ _____ _ __ _ _
|_ _| | \/ | / ____| | |/ / (_) | |
| | | \ / | | | __ | ' / _ | |_
| | | |\/| | | | |_ | | < | | | __|
_| |_ | | | | | |__| | | . \ | | | |_
|_____| |_| |_| \_____| |_|\_\ |_| \__|
Python 2和3包装器,用于wkhtmltoimage实用程序,将HTML转换为图像。
安装
-
安装imgkit
pip install imgkit
-
安装wkhtmltopdf
-
Debian/Ubuntu
sudo apt-get install wkhtmltopdf
警告! Debian/Ubuntu仓库中的版本功能减少(因为没有编译wkhtmltopdf QT补丁),例如添加大纲、页眉、页脚、目录等。要使用这些选项,您应从wkhtmltopdf网站安装静态二进制文件,或者您可以使用此脚本。
-
MacOSX
brew install --cask wkhtmltopdf
-
Windows和其他选项
请查看wkhtmltopdf主页以获取二进制安装程序或Wiki页面。
-
用法
简单示例
import imgkit
imgkit.from_url('http://google.com', 'out.jpg')
imgkit.from_file('test.html', 'out.jpg')
imgkit.from_string('Hello!', 'out.jpg')
您也可以传递一个打开的文件
with open('file.html') as f:
imgkit.from_file(f, 'out.jpg')
如果您希望进一步处理生成的图像,您可以将它读取到变量中
# Use False instead of output path to save pdf to a variable
img = imgkit.from_url('http://google.com', False)
您可以通过输入wkhtmltoimage
命令或访问此手册找到所有wkhtmltoimage选项。您可以在选项名称中放置'--'。对于没有值的选项,请使用None, False或''作为字典值。对于可重复的选项(包括allow, cookie, custom-header, post, postfile, run-script, replace),您可以使用列表或元组。对于需要多个值的选项(例如,--custom-header Authorization secret),我们可以使用一个元组(见以下示例)。
options = {
'format': 'png',
'crop-h': '3',
'crop-w': '3',
'crop-x': '3',
'crop-y': '3',
'encoding': "UTF-8",
'custom-header' : [
('Accept-Encoding', 'gzip')
],
'cookie': [
('cookie-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'no-outline': None
}
imgkit.from_url('http://google.com', 'out.png', options=options)
在一些无头服务器上,可能需要安装 xvfb。
# at ubuntu server, etc.
sudo apt-get install xvfb
# at centos server, etc.
yum install xorg-x11-server-Xvfb
然后使用带有 xvfb 选项的 IMGKit: {"xvfb": ""}
。
默认情况下,IMGKit 会显示所有 wkhtmltoimage
输出。如果您不希望如此,则需要传递 quiet
选项。
options = {
'quiet': ''
}
imgkit.from_url('google.com', 'out.jpg', options=options)
由于 wkhtmltoimage
命令语法,目录(TOC)和 封面 选项必须单独指定。如果您需要在 TOC 之前显示封面,请使用 cover_first
选项。
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
imgkit.from_file('file.html', options=options, toc=toc, cover=cover)
imgkit.from_file('file.html', options=options, toc=toc, cover=cover, cover_first=True)
在转换文件或字符串时,您可以使用 css 选项指定外部 CSS 文件。
# Single CSS file
css = 'example.css'
imgkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
imgkit.from_file('file.html', options=options, css=css)
您还可以通过 HTML 中的元标签传递任何选项。
body = """
<html>
<head>
<meta name="imgkit-format" content="png"/>
<meta name="imgkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
imgkit.from_string(body, 'out.png')
配置
每次 API 调用都可以有一个可选的配置参数。这应该是一个 imgkit.config()
API 调用的实例。它使用配置选项作为初始参数。可用选项包括
wkhtmltoimage
-wkhtmltoimage
二进制文件的路径。默认情况下,imgkit
将尝试使用which
(在 UNIX 类型系统上)或where
(在 Windows 上)来定位此路径。xvfb
-xvfb-run
二进制文件的路径。默认情况下,imgkit
将尝试使用which
(在 UNIX 类型系统上)或where
(在 Windows 上)来定位此路径。meta_tag_prefix
-imgkit
特定元标签的前缀 - 默认情况下为imgkit-
示例 - 当 wkhtmltopdf
或 xvfb
不在 $PATH
中时
config = imgkit.config(wkhtmltoimage='/opt/bin/wkhtmltoimage', xvfb='/opt/bin/xvfb-run')
imgkit.from_string(html_string, output_file, config=config)
故障排除
-
IOError: '未找到 wkhtmltopdf 可执行文件'
:请确保您已将
wkhtmltoimage
添加到您的$PATH
或通过自定义配置设置(参见上一节)。在 Windows 上 where wkhtmltoimage 或在 Linux 上 which wkhtmltoimage 应返回二进制文件的实际路径。 -
IOError: '未找到 xvfb 可执行文件'
:请确保您已将 xvfb-run 添加到您的
$PATH
或通过自定义配置设置(参见上一节)。在 Windows 上 where xvfb 或在 Linux 上 which xvfb-run 或 which Xvfb 应返回二进制文件的实际路径。 -
IOError: '命令失败'
:此错误表示 IMGKit 无法处理输入。您可以尝试直接从错误消息中运行命令,并查看导致失败的错误(在某些
wkhtmltoimage
版本中,这可能是由于段错误引起的)。
致谢
IMGKit 作者
- jarrekk https://github.com/jarrekk
贡献者
- v-hunt https://github.com/v-hunt
- archydeberker https://github.com/archydeberker
- arayate https://github.com/arayate
- xtrntr https://github.com/xtrntr
- mike1703 https://github.com/mike1703
- themeewa https://github.com/themeewa
变更日志
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源代码发行版
构建分发包
imgkit-1.2.3.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c9ade93d0281277c898b983c959f11f0cc1cb4a5fed144560dfa4d273fb50cd8 |
|
MD5 | 451a5f75e5727e4f6784219624ab11a4 |
|
BLAKE2b-256 | 966649089f7ca929e854f4f110259de392ec8bbed5ac3f2f9e1ff493f6623b15 |
imgkit-1.2.3-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cba8e3f67da6e0a5c87345a8b41605337851f9b9b75719500fa6810ecff84791 |
|
MD5 | 75b98a1f7af2a5f41c2e1206a52870da |
|
BLAKE2b-256 | bfb0e0c8aaafb20377eccb9aedbb9e00c21aeaac9cdc13d31d1c7532e1241bab |