python imgkit wkhtmltopdf OSError: [WinError 6] 句柄无效

今天发现一个问题,按照网上的教程实现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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值