IIS 上的 django-wkhtmltopdf:[WinError 6] 句柄无效 [英] django-wkhtmltopdf on IIS: [WinError 6] The handle is invalid

查看:53
本文介绍了IIS 上的 django-wkhtmltopdf:[WinError 6] 句柄无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,所以请原谅我,如果我忘记提及或有什么问题:)

this is my very first question, so please forgive me, if I forgot something to mention or there's something wrong with it :)

我在 IIS(10)-Windows Server 上设置了一个 python(3.5.3)-django(2.1.5)-项目.一切正常.

I set up a python(3.5.3)-django(2.1.5) -project on IIS(10)-Windows Server. Everything works great.

只有 wkhtmltopdf(0.12.5) 有一个奇怪的行为.

Only wkhtmltopdf(0.12.5) has a strange behavior.

当我在本地主机上运行它时,命令提示符给了我

When I run it on localhost the command prompt gives me

Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

并且我可以按预期在我的下载文件夹中找到生成的 .pdf 文件.

and I can find the generated .pdf-file in my downloads folder as expected.

当我将 ALLOWED_HOSTS 更改为服务器的 IP 并调用 url 以生成 pdf 时,它说有一个

When I change ALLOWED_HOSTS to the Server's IP and I call the url to generate a pdf it says there is a

OSError at/pdf/

OSError at /pdf/

[WinError 6] 句柄无效

[WinError 6] The handle is invalid

使用回溯:

文件"C:\my_project\myenv\lib\site-packages\django\core\handlers\exception.py"在内部34. response = get_response(request)

File "C:\my_project\myenv\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request)

文件"C:\my_project\myenv\lib\site-packages\django\core\handlers\base.py"在 _get_response 中156. response = self.process_exception_by_middleware(e, request)

File "C:\my_project\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response 156. response = self.process_exception_by_middleware(e, request)

文件"C:\my_project\myenv\lib\site-packages\django\core\handlers\base.py"在 _get_response 中154. response = response.render()

File "C:\my_project\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response 154. response = response.render()

文件"C:\my_project\myenv\lib\site-packages\django\template\response.py"在渲染中106. self.content = self.rendered_content

File "C:\my_project\myenv\lib\site-packages\django\template\response.py" in render 106. self.content = self.rendered_content

文件"C:\my_project\myenv\lib\site-packages\wkhtmltopdf\views.py"在 render_content80.cover_template=self.resolve_template(self.cover_template)

File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\views.py" in rendered_content 80. cover_template=self.resolve_template(self.cover_template)

文件"C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py"在 render_pdf_from_template 中237.cover_filename=cover.filename if cover else None)

File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py" in render_pdf_from_template 237. cover_filename=cover.filename if cover else None)

文件"C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py"在 convert_to_pdf166. return wkhtmltopdf(pages=pages, **cmd_options)

File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py" in convert_to_pdf 166. return wkhtmltopdf(pages=pages, **cmd_options)

文件"C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py"在 wkhtmltopdf147. 返回 check_output(ck_args, **ck_kwargs)

File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py" in wkhtmltopdf 147. return check_output(ck_args, **ck_kwargs)

check_output 中的文件C:\Program Files\Python35\lib\subprocess.py"316. **kwargs).stdout

File "C:\Program Files\Python35\lib\subprocess.py" in check_output 316. **kwargs).stdout

运行中的文件C:\Program Files\Python35\lib\subprocess.py"383.以Popen(*popenargs, **kwargs)为进程:

File "C:\Program Files\Python35\lib\subprocess.py" in run 383. with Popen(*popenargs, **kwargs) as process:

init 中的文件C:\Program Files\Python35\lib\subprocess.py"640. errread, errwrite) = self._get_handles(stdin, stdout, stderr)

File "C:\Program Files\Python35\lib\subprocess.py" in init 640. errread, errwrite) = self._get_handles(stdin, stdout, stderr)

_get_handles 中的文件C:\Program Files\Python35\lib\subprocess.py"884.errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)

File "C:\Program Files\Python35\lib\subprocess.py" in _get_handles 884. errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)

异常类型:OSError at/pdf/异常值:[WinError 6] Das处理ist ungültig

Exception Type: OSError at /pdf/ Exception Value: [WinError 6] Das Handle ist ungültig

我可以在 C:\Users\myapplicationpool\AppData\Local\Temp 文件夹中看到 wkhtmltopdf 正在生成一个 .html 文件,例如 wkhtmltopdfgn1s7k5r.html但不知何故,进度卡住了.

I can see in C:\Users\myapplicationpool\AppData\Local\Temp-folder that wkhtmltopdf is generating a .html-file called for example wkhtmltopdfgn1s7k5r.html but somehow the progress gets stuck.

如前所述此处这里 其他人也有同样的问题.但是改变了

As mentioned here and here others had the same issue, too. But changing

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)

没有效果.我认为此解决方案仅适用于 Python 2.7 的 subprocess.py 文件,而我使用的是 Python 3+,并且此文件的功能已更改.

has no effect. I think this solution only works for the subprocess.py-file of Python 2.7 and I am using Python 3+ and functions of this file changed.

我授予了 IUSR 的完全权限 &IIS_USRS -users 到 wkhtmltopdf-folder 其中 bin-folder &wkhtmltopdf.exe 在我读到这也有帮助时撒谎,但它没有.

I gave full permissions for IUSR & IIS_USRS -users to the wkhtmltopdf-folder where bin-folder & wkhtmltopdf.exe lie as I read that this could help as well, but it doesn't.

有没有其他人知道我可以尝试并帮助我什么?

Has anyone any other idea what I could try and help me please?

这个问题真的出在 wkhtmltopdf & 的子进程中吗?python 还是我宁愿更改/添加 IIS 中 FastCgiModule 的 djangohandler 的处理程序?我该怎么做?

Does this issue really lie in the subprocesses of wkhtmltopdf & python or do I rather have to change/add handlers of my djangohandler of the FastCgiModule in IIS? How would I do so?

为什么当我在本地作为 localhost 在服务器上运行它时它没有任何问题,但当我通过服务器的 IP 调用页面时它不起作用?-- 如前所述:其他一切正常.

Why is it working when I run it on the server locally as localhost without any problems but not when I call the page via the server's IP? -- as mentioned: everything else works totally fine.

我将 wkhtmltopdf 添加到 INSTALLED_APPS 并进行如下设置:

I added wkhtmltopdf to INSTALLED_APPS and set it up like so:

settings.py

settings.py

WKHTMLTOPDF_CMD = 'C:/wkhtmltopdf/bin/wkhtmltopdf'

(因为我还读到,当它安装在 'Program Files' 中时,由于路径中的空格经常会出现问题.)

(as I also read that there often is a problem due to spaces in paths when it is installed in 'Program Files'.)

urls.py

path('pdf/', views.MyPDFView.as_view(), name='pdfview'),

views.py

from wkhtmltopdf.views import PDFTemplateResponse
class MyPDFView(View):
    template_name='mypdfview.html'

    def get(self, request):
        response = PDFTemplateResponse(request=self.request,
                                       template=self.template_name,
                                       filename='hello' + '.pdf',
                                       context=self.context,
                                       show_content_in_browser=False,
                                       cmd_options={
                                        'margin-top': 50,
                                        },
                                       )
        return response

mypdfview.html

mypdfview.html

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Hello World</title>
</head>
<body>
    <h1>Some headline here.</h1>
</body>
</html>

<小时>

不知何故,我的问候消失了 - 添加了它......好像不能说:大家好"!?


Edit 1: Somehow my greeting disappeared - added it... Edit 2: It seems I am not allowed to say: "Hi everyone"!?

推荐答案

要执行为 python 2.7 提出的相同解决方法,您需要编辑 wkhtmltopdf/utils.py 文件,方法 wkhtmltopdf():

To do the same workaround as proposed for python 2.7, you need to edit the wkhtmltopdf/utils.py file, method wkhtmltopdf():

from .subprocess import check_output, PIPE

...

def wkhtmltopdf(pages, output=None, **kwargs):
    ...  

    except (AttributeError, IOError):
        # can't call fileno() on mod_wsgi stderr object
        pass

    # add this:
    if not ck_kwargs.get('stderr'):
        ck_kwargs['stderr'] = PIPE

    return check_output(ck_args, **ck_kwargs)

这篇关于IIS 上的 django-wkhtmltopdf:[WinError 6] 句柄无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆