如何在 IIS 7.5 - 8 上使用 django-wkhtmltopdf 生成 pdf 文件

How generate a pdf file with django-wkhtmltopdf on IIS 7.5 - 8

我使用 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

终于明白了,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 集成服务器会出错。

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

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

configuration.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()