这是我的第一个问题,所以请原谅我,如果我忘了要提的东西或者它有什么问题:)
我在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时,它说有一个
OS错误/ pdf /
[WinError 6]句柄无效
跟踪:
内部34中的文件“C:\ my_project \ myenv \ lib \ site-packages \ django \ core \ handlers \ exception.py”response = get_response(request)
_get_response 156中的文件“C:\ my_project \ myenv \ lib \ site-packages \ django \ core \ handlers \ base.py”.response = self.process_exception_by_middleware(e,request)
_get_response 154中的文件“C:\ my_project \ myenv \ lib \ site-packages \ django \ core \ handlers \ base.py”。response = response.render()
渲染106中的文件“C:\ my_project \ myenv \ lib \ site-packages \ django \ template \ response.py”.self.content = self.rendered_content
render_content中的文件“C:\ my_project \ myenv \ lib \ site-packages \ wkhtmltopdf \ views.py”80. cover_template = self.resolve_template(self.cover_template)
在render_pdf_from_template中文件“C:\ my_project \ myenv \ lib \ site-packages \ wkhtmltopdf \ utils.py”237. cover_filename = cover.filename if cover else None)
convert_to_pdf中的文件“C:\ my_project \ myenv \ lib \ site-packages \ wkhtmltopdf \ utils.py”166. return wkhtmltopdf(pages = pages,** cmd_options)
在wkhtmltopdf中输入文件“C:\ my_project \ myenv \ lib \ site-packages \ wkhtmltopdf \ utils.py”。return check_output(ck_args,** ck_kwargs)
在check_output 316中输入文件“C:\ Program Files \ Python35 \ lib \ subprocess.py”。** kwargs).stdout
运行383中的文件“C:\ Program Files \ Python35 \ lib \ subprocess.py”,其中Popen(* popenargs,** kwargs)为进程:
在init 640中文件“C:\ Program Files \ Python35 \ lib \ subprocess.py”.errread,errwrite)= self._get_handles(stdin,stdout,stderr)
_get_handles 884中的文件“C:\ Program Files \ Python35 \ lib \ subprocess.py”.errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
异常类型:OSError at / pdf /异常值:[WinError 6]句柄无效
我可以在C:\Users\myapplicationpool\AppData\Local\Temp
文件夹中看到wkhtmltopdf正在生成一个.html文件,例如wkhtmltopdfgn1s7k5r.html
,但不知怎的,进展卡住了。
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文件夹,其中bin-folder&wkhtmltopdf.exe所在,因为我读到这也有帮助,但事实并非如此。
有没有人知道我能尝试什么,请帮助我?
这个问题真的存在于wkhtmltopdf和python的子进程中,还是我不得不在IIS中更改/添加我的djangohandler的FastCgiModule处理程序?我该怎么办?
为什么当我在本地服务器上运行它作为localhost时没有任何问题但是当我通过服务器的IP调用页面时却没有? - 如上所述:其他一切都很好。
我将wkhtmltopdf添加到INSTALLED_APPS
并将其设置为:
settings.朋友
WKHTMLTOPDF_CMD = 'C:/wkhtmltopdf/bin/wkhtmltopdf'
(因为我还读到,当它安装在'Program Files'
中时,由于路径中的空格而经常出现问题。)
URLs.朋友
path('pdf/', views.MyPDFView.as_view(), name='pdfview'),
views.朋友
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)