今天发现一个问题,按照网上的教程实现html转图片写好demo,在本地可以跑,放到django也可以,但是放到iis 中就报错无效的句柄
我在终端手动启动项目 python manage.py runserver 没问题
错误日志:
File ".\Plugin\imgkit.py", line 25, in generate_img_html
cfg = imgkit.config(wkhtmltoimage=path_wkimg)
File "c:\api\venv\lib\site-packages\imgkit\api.py", line 101, in config
return Config(**kwargs)
File "c:\api\venv\lib\site-packages\imgkit\config.py", line 24, in __init__
stdout=subprocess.PIPE).communicate()[0].strip()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 665, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 919, in _get_handles
errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
OSError: [WinError 6] 句柄无效。
废话不说直接上:
修改imgkit/congfig.py 24 行
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL
附上config.py
原因猜测是django运行在iis中进程输出有问题
# -*- coding: utf-8 -*-
import subprocess
import sys
class Config(object):
def __init__(self, wkhtmltoimage='', meta_tag_prefix='imgkit-'):
self.meta_tag_prefix = meta_tag_prefix
self.wkhtmltoimage = wkhtmltoimage
self.xvfb = ''
if not self.wkhtmltoimage:
if sys.platform == 'win32':
self.wkhtmltoimage = subprocess.Popen(['where', 'wkhtmltoimage'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL).communicate()[0].strip()
else:
self.wkhtmltoimage = subprocess.Popen(['which', 'wkhtmltoimage'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL).communicate()[0].strip()
if not self.xvfb:
if sys.platform == 'win32':
self.xvfb = subprocess.Popen(['where', 'xvfb-run'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL).communicate()[0].strip()
else:
self.xvfb = subprocess.Popen(['which', 'xvfb-run'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL).communicate()[0].strip()
try:
with open(self.wkhtmltoimage):
pass
except IOError:
raise IOError('No wkhtmltoimage executable found: "{0}"\n'
'If this file exists please check that this process can '
'read it. Otherwise please install wkhtmltopdf - '
'http://wkhtmltopdf.org\n'.format(self.wkhtmltoimage))