使用webkit渲染引擎和qt将html转换为图像的wkhtmltopdf python包装器
项目描述
Python 3异步包装器,用于IMGKIT。
安装
安装async-imgkit
pip install async-imgkit
安装wkhtmltopdf
Debian/Ubuntu
sudo apt-get install wkhtmltopdf
警告! Debian/Ubuntu仓库中的版本功能有所减少(因为它是在没有wkhtmltopdf QT补丁的情况下编译的),例如添加目录、页眉、页脚、目录等。要使用这些选项,您应该从wkhtmltopdf网站安装静态二进制文件,或者您可以使用此脚本。
MacOSX
brew install wkhtmltopdf
Windows和其他选项:请检查wkhtmltopdf主页上的二进制安装程序或wiki页面。
使用
简单示例
import async_imgkit.api as imgkit
await imgkit.from_url('http://google.com', 'out.jpg')
await imgkit.from_file('test.html', 'out.jpg')
await imgkit.from_string('Hello!', 'out.jpg')
您可以通过传递包含多个URL或文件的列表
await imgkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.jpg')
await imgkit.from_file(['file1.html', 'file2.html'], 'out.jpg')
您还可以传递一个打开的文件
with open('file.html') as f:
await imgkit.from_file(f, 'out.jpg')
如果您希望进一步处理生成的IMG,您可以将其读取到变量中
# Use False instead of output path to save pdf to a variable
img = await imgkit.from_url('http://google.com', False)
您可以通过类型 wkhtmltoimage 命令或访问此 手册 找到所有wkhtmltoimage选项。您可以在选项名称中省略‘-’。对于没有值的选项,使用 None, False 或 ‘’ 作为字典值。对于可重复的选项(包括allow、cookie、custom-header、post、postfile、run-script、replace),您可以使用列表或元组。对于需要多个值的选项(例如,–custom-header Authorization secret),我们可以使用一个2元组(见下面的示例)。
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
}
await 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': ''
}
await imgkit.from_url('google.com', 'out.jpg', options=options)
由于 wkhtmltoimage 命令的语法,目录 和 封面 选项必须单独指定。如果您需要在目录之前有封面,请使用 cover_first 选项。
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
await imgkit.from_file('file.html', options=options, toc=toc, cover=cover)
await imgkit.from_file('file.html', options=options, toc=toc, cover=cover, cover_first=True)
在转换文件或字符串时,您可以使用 css 选项指定外部 CSS 文件。
# Single CSS file
css = 'example.css'
await imgkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
await 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>
"""
await imgkit.from_string(body, 'out.png')
配置
每个 API 调用都接受一个可选的配置参数。这应该是一个 async_imgkit.api.config() API 调用的实例。它接受配置选项作为初始参数。可用的选项包括:
wkhtmltoimage - wkhtmltoimage 二进制文件的路径。默认情况下,async_imgkit 将尝试使用 which(在 UNIX 类型系统上) 或 where (在 Windows 上) 定位此路径。
meta_tag_prefix - async_imgkit 特定元标签的前缀 - 默认情况下,这是 imgkit-
示例 - 当 wkhtmltopdf 不在 $PATH 中时
config = imgkit.config(wkhtmltoimage='/opt/bin/wkhtmltoimage')
await imgkit.from_string(html_string, output_file, config=config)
故障排除
IOError: '未找到 wkhtmltopdf 可执行文件':
请确保您在 $PATH 中有 wkhtmltoimage 或通过自定义配置设置(参见上一节)。在 Windows 上,where wkhtmltoimage 应返回二进制文件的实际路径;在 Linux 上,which wkhtmltoimage 应返回实际路径。
IOError: '命令失败':
此错误表示 IMGKit 无法处理输入。您可以尝试直接从错误消息运行命令,并查看导致失败的错误(在某些 wkhtmltoimage 版本中,这可能是由于段错误引起的)。
致谢
项目详情
async-imgkit-0.0.5.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | b1a7496f46a66dce563ad9b967abc38cf09d334f9e9aaed5eb4b74a357a7853a |
|
MD5 | 07281488e2b2a402cbc187cc0c9405ca |
|
BLAKE2b-256 | ea85a34372d93b166a75c4a191909a423445d4c5004ce889dbf7f27ce81219a0 |