【发布时间】:2019-08-11 01:56:37
【问题描述】:
这是我的第一个问题,所以请原谅我,如果我忘了提什么或者有什么问题:)
我在 IIS(10)-Windows Server 上设置了一个 python(3.5.3)-django(2.1.5) -项目。一切都很好。
问题
只有 wkhtmltopdf(0.12.5) 有奇怪的行为。
当我在 localhost 上运行它时,命令提示符会提示我
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
我可以按预期在我的下载文件夹中找到生成的 .pdf 文件。
当我将ALLOWED_HOSTS 更改为服务器的 IP 并调用 url 生成 pdf 时,它说有一个
/pdf/ 处的 OSError
[WinError 6]句柄无效
使用 Traceback:
文件 “C:\my_project\myenv\lib\site-packages\django\core\handlers\exception.py” 在内部 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)
文件 “C:\my_project\myenv\lib\site-packages\django\core\handlers\base.py” 在 _get_response 154. response = response.render()
文件 “C:\my_project\myenv\lib\site-packages\django\template\response.py” 在渲染中 106. self.content = self.rendered_content
文件 "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\views.py" 在渲染内容中 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)
文件 “C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py” 在 convert_to_pdf 166. return wkhtmltopdf(pages=pages, **cmd_options)
文件 “C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py” 在 wkhtmltopdf 147. return check_output(ck_args, **ck_kwargs)
check_output 中的文件“C:\Program Files\Python35\lib\subprocess.py” 316. **kwargs).stdout
运行中的文件“C:\Program Files\Python35\lib\subprocess.py” 383. 使用 Popen(*popenargs, **kwargs) 作为进程:
init 中的文件“C:\Program Files\Python35\lib\subprocess.py” 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)
异常类型:/pdf/ 处的 OSError 异常值:[WinError 6] Das Handle ist ungültig
我可以在 C:\Users\myapplicationpool\AppData\Local\Temp-folder 中看到 wkhtmltopdf 正在生成一个名为 wkhtmltopdfgn1s7k5r.html 的 .html 文件,但不知何故进度卡住了。
正如提到的here 和here 其他人也有同样的问题。但是变化
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+,这个文件的功能发生了变化。
我将 IUSR 和 IIS_USRS -users 授予 wkhtmltopdf-folder 的完全权限,因为我读到这也有帮助,但它没有。
问题
还有其他人知道我可以尝试什么来帮助我吗?
这个问题真的存在于 wkhtmltopdf 和 python 的子进程中,还是我必须在 IIS 中更改/添加我的 FastCgiModule 的 djangohandler 的处理程序?我该怎么做?
为什么当我在本地作为 localhost 在服务器上运行它时它可以正常工作,但当我通过服务器的 IP 调用页面时却没有? -- 如前所述:其他一切都很好。
设置
我将 wkhtmltopdf 添加到 INSTALLED_APPS 并设置如下:
settings.py
WKHTMLTOPDF_CMD = 'C:/wkhtmltopdf/bin/wkhtmltopdf'
(我还读到,安装在'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
<!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>
编辑 1: 不知何故,我的问候消失了-添加了它... 编辑2: 看来我不能说:“大家好”!?
【问题讨论】:
-
是的,这不是你写的信 :-) 见How to Ask。顺便问下好问题。抱歉,我没有答案。如果您在 python 3.x 上运行,只需确认更改 django-wkhtmltopdf 中的 subprocess.py 的代码不会做任何事情,因为
check_output已经定义。 -
在wkhtmltopdf/utils.py中,在方法@987654339@末尾,就在
return check_output(..)行之前,尝试添加:if not ck_kwargs.get('stderr'): ck_kwargs['stderr'] = PIPE或直接@ 987654342@(如果您总是在 IIS 中运行)。你需要from subprocess import PIPE。 -
是的,成功了!非常感谢 dirkgroten!
-
真的吗?然后我将其添加为答案。您可能想将此作为评论添加到 django-wkhtmltopdf github repo 中的问题线程中,似乎有更多人遇到此问题。
-
这样做了......:-)
标签: django python-3.x subprocess iis-10