XHTML2PDF Django [错误 10061] 无法连接,目标机器积极拒绝

1 投票
1 回答
1625 浏览
提问于 2025-04-17 20:48

我用XAMPP和mod_wsgi部署了我的django应用程序。在部署之前,一切都很好。但是在部署之后,PDF下载功能无法正常工作,并且返回了错误。

这是我代码的快照

def render_to_pdf(template_src, context_dict, file_name):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        response = http.HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="%s"' %(file_name,)
        response.write(result.getvalue())
        return response
    return http.HttpResponse('We had some errors<pre>%s</pre>' % cgi.escape(html))

这是错误信息

[Errno 10061] No connection could be made because the target machine actively refused it

这是导致错误的代码行

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

异常发生的位置:

C:\Python27x32\Lib\socket.py in create_connection, line 571

这是我的wsgi.py的代码

<VirtualHost *:80>
    WSGIScriptAlias / "c:/xampp/htdocs/ghb/ghb/wsgi.py"

    <Directory "c:/xampp/htdocs/ghb/">
        <Files wsgi.py>
            Order deny,allow
            Allow from all
        </Files>
    </Directory>

    Alias /static/ C:/xampp/htdocs/ghb/static/

    <Directory c:/xampp/htdocs/ghb/static/>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

这是我的错误日志

1 个回答

4

相关的错误信息可以在你的 error.log 文件中找到,从第400行开始。根据这个错误信息,看来你的HTML里有一个网址(可能是指向一张图片的链接?)是无法访问的(可能这个链接指向 localhost,在你的电脑上能用,但在服务器上不行?);xhtml2pdf 尝试去获取这个网址(可能是想把图片放到PDF里?)但失败了。你需要检查一下你的 html 变量(也就是你传给 xhtml2pdf 的HTML代码)里有没有坏掉的 http:https: 链接。通过查看错误信息中的文件名和行号(比如 File "C:\\Python27x32\\lib\\site-packages\\xhtml2pdf\\parser.py", line 448),你可以更准确地找到 xhtml2pdf 出现问题的具体元素。

撰写回答