这是我的第一个问题,所以如果我忘记了要提及的内容或者有什么问题,请原谅我:)
我在 IIS(10)-Windows Server 上设置了一个 python(3.5.3)-django(2.1.5) 项目。一切都很好。
问题
只有 wkhtmltopdf(0.12.5) 有奇怪的行为。
当我在本地主机上运行它时,命令提示符会给我
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 时,它说有一个
OSError at /pdf/
[WinError 6] The handle is invalid
使用回溯:
File "C:\my_project\myenv\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(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)
File "C:\my_project\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response 154. response = response.render()
File "C:\my_project\myenv\lib\site-packages\django\template\response.py" in render 106. self.content = self.rendered_content
File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\views.py" in rendered_content 80. cover_template=self.resolve_template(self.cover_template)
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)
File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py" in convert_to_pdf 166. return wkhtmltopdf(pages=pages, **cmd_options)
File "C:\my_project\myenv\lib\site-packages\wkhtmltopdf\utils.py" in wkhtmltopdf 147. return check_output(ck_args, **ck_kwargs)
File "C:\Program Files\Python35\lib\subprocess.py" in check_output 316. **kwargs).stdout
File "C:\Program Files\Python35\lib\subprocess.py" in run 383. with Popen(*popenargs, **kwargs) as process:
File "C:\Program Files\Python35\lib\subprocess.py" in init 640. errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Program Files\Python35\lib\subprocess.py" in _get_handles 884. errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
Exception Type: OSError at /pdf/ Exception Value: [WinError 6] Das Handle ist ungültig
我可以在C:\Users\myapplicationpool\AppData\Local\Temp
文件夹中看到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
用户授予了 bin-folder 和 wkhtmltopdf.exe 所在的 wkhtmltopdf-folder 的完全权限,因为我读到这也有帮助,但事实并非如此。
问题
还有人知道我可以尝试什么来帮助我吗?
这个问题真的存在于 wkhtmltopdf 和 python 的子进程中,还是我必须更改/添加 IIS 中 FastCgiModule 的 djangohandler 的处理程序?我该怎么做?
为什么当我在服务器上以本地主机身份运行它时它可以正常工作而没有任何问题,但当我通过服务器的 IP 调用该页面时却不能? -- 如前所述:其他一切都很好。
设置
我将 wkhtmltopdf 添加到 INSTALLED_APPS
并按如下方式进行设置:
设置.py
WKHTMLTOPDF_CMD = 'C:/wkhtmltopdf/bin/wkhtmltopdf'
(我还读到,当它安装在'Program Files'
中时,由于路径中的空格,经常会出现问题。)
url.py
path('pdf/', views.MyPDFView.as_view(), name='pdfview'),
View .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: 好像不可以说:“大家好”!?
最佳答案
要执行与 python 2.7 建议的相同解决方法,您需要编辑 wkhtmltopdf/utils.py 文件,方法 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] The handle is invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55259645/