python调用imgkit将html转图片pdf问题实例wkhtmltox

发布时间 2023-04-21 17:20:37作者: 长虫山小木屋

wkhtmltox的下载地址:https://wkhtmltopdf.org/downloads.html

或者:https://github.com/wkhtmltopdf/wkhtmltopdf

0.12.6版本按网络上的教程会出现一个错误:

IOError: wkhtmltopdf exited with non-zero code 1. error: [blank]

解决方法参见:https://stackoverflow.com/questions/73599970/how-to-solve-wkhtmltopdf-reported-an-error-exit-with-code-1-due-to-network-err

options={"enable-local-file-access": ""}

能用的完整实例如下:

import imgkit
path_wkimg = r'.\控件\126\bin\wkhtmltoimage.exe'  # 工具路径如果是安装版本的一半在c盘program...
cfg = imgkit.config(wkhtmltoimage=path_wkimg)
options = {'crop-w': 1920,'crop-h': 1080,'encoding': 'UTF-8','quality':100,'format':'png','enable-local-file-access':''}
imgkit.from_file(r'./web模板/test.html',config=cfg,options=options,output_path="./out1.png")

问题解决,兼容性尚可,页面echart需要关闭加载动画animation: false

option = {
        animation: false,
        color: ['#00FFB1', '#01396B'],