【问题标题】:How generate a pdf file with django-wkhtmltopdf on IIS 7.5 - 8如何在 IIS 7.5 - 8 上使用 django-wkhtmltopdf 生成 pdf 文件
【发布时间】:2016-02-12 17:33:56
【问题描述】:

我使用 django-wkhtmltopdf 在我的 webapp 中生成 pdf,使用 django 集成服务器可以正常工作,但是当我使用 IIS 7.5 或 8 时出现此错误:WindowsError [错误 6] 句柄无效

环境:

Request Method: GET
Request URL: http://127.0.0.1:8006/blabla/pdf/10/

Django Version: 1.8.5
Python Version: 2.7.10
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'NuevoTicket',
 'crispy_forms',
 'wkhtmltopdf',
 'ckeditor',
 'ckeditor_uploader')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  164.                 response = response.render()
File "C:\Python27\lib\site-packages\django\template\response.py" in render
  158.             self.content = self.rendered_content
File "C:\Python27\lib\site-packages\wkhtmltopdf\views.py" in rendered_content
  78.             cmd_options=cmd_options
File "C:\Python27\lib\site-packages\wkhtmltopdf\utils.py" in render_pdf_from_template
  159.                               cmd_options=cmd_options)
File "C:\Python27\lib\site-packages\wkhtmltopdf\utils.py" in convert_to_pdf
  121.     return wkhtmltopdf(pages=[filename], **cmd_options)
File "C:\Python27\lib\site-packages\wkhtmltopdf\utils.py" in wkhtmltopdf
  109.     return check_output(ck_args, **ck_kwargs)
File "C:\Python27\lib\subprocess.py" in check_output
  566.     process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py" in __init__
  702.          errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr)
File "C:\Python27\lib\subprocess.py" in _get_handles
  857.                 errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE)

Exception Type: WindowsError at /blabla/pdf/10/
Exception Value: 6 The handle is invalid

【问题讨论】:

标签: django pdf iis-7.5 wkhtmltopdf iis-8


【解决方案1】:

终于明白了,thank's to n1b0r。我必须像这样更新 python27/lib/subprocess.py 中的 check_out 方法

来自:

if 'stdout' in kwargs:
    raise ValueError('stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)

到:

if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
kwargs.pop('stderr', None)
process = Popen(stdout=PIPE, stderr=PIPE, stdin=PIPE, *popenargs, **kwargs)

这使它可以在 IIS 中工作,但如果使用 django 集成服务器则会出错。

【讨论】:

    【解决方案2】:

    我以前也有同样的问题。将选项 quiet 添加到 pdfkit 并尝试从 pdfkit 修改文件 configuration.py

    options = {'quiet': ''}
    pdfkit.from_file('in.html', 'out.pdf',options = options)
    

    配置.py

    从此:

    self.wkhtmltopdf = subprocess.Popen(['where','wkhtmltopdf'],stdout=subprocess.PIPE).communicate()[0].strip()
    

    到:

    self.wkhtmltopdf = subprocess.Popen(['which','wkhtmltopdf'],stdin=subprocess.PIPE,stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0].strip()
    

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 2013-12-18
      • 2014-07-03
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 2023-03-20
      相关资源
      最近更新 更多