【问题标题】:Django xhtml2pdf 'HttpResponse' does not have the buffer interfaceDjango xhtml2pdf 'HttpResponse' 没有缓冲区接口
【发布时间】:2015-03-08 03:41:55
【问题描述】:

我正在尝试进行一些测试以使用 Django 输出 pdf。我正在使用通过 pip 安装的 xhtml2pdf 项目。

我查看了一个将 pdf 发送到浏览器并成功的示例,但是在尝试使用我的模板时,出现错误。上面写着:

'HttpResponse' does not have the buffer interface

接下来是我的查看代码:

def generate_pdf(request):

    from xhtml2pdf import pisa
    from person.views import alumn_list

    html = alumn_list(request, 12, 0) # This function returns a render('alumn_list.html)

    pdfFile = open(os.path.join(base.TEMPLATE_DIRS[0], 'test.pdf'), 'w+b')
    pisaStatus = pisa.CreatePDF(html, dest=pdfFile) # The errors happens here

    pdfFile.seek(0)
    pdf = pdfFile.read()
    pdfFile.close()
    return HttpResponse(pdf, content_type='application/pdf')

我的模板是下一个:

{% extends "alumns.html" %}
{% load i18n %}

{% block extra_css %}
    <style>
    @page {
        size: letter portrait;
        @frame header_frame {           /* Static frame */
            -pdf-frame-content: header_content;
            left: 50pt; width: 512pt; top: 50pt; height: 40pt;
        }
        @frame col1_frame {             /* Content frame 1 */
            left: 44pt; width: 445pt; top: 90pt; height: 632pt;
            text-align:center;
        }

        @frame footer_frame {           /* Static frame */
            -pdf-frame-content: footer_content;
            left: 50pt; width: 512pt; top: 772pt; height: 20pt;
        }
    }
    </style>
{% endblock extra_css %}

{% block title %}
    {% trans "Alumn list" %}
{% endblock title %}

{% block page_title %}
    {% trans "Alumn list" %}
{% endblock page_title %}

{% block content %}
    <table class="infoTable">
        {% csrf_token %}
        {% for index in "12" %}
        <caption {% ifequal index "2" %}align="bottom"{% endifequal%}>
            <a href="{{hrefs.0}}" class="pager">&lt;&lt; {% trans "Previous" %}</a>
            <a href="{{hrefs.1}}" class="pager">{% trans "Next" %} &gt;&gt;</a>
        </caption>
        {% endfor %}

        <thead>
            <th>{% trans "ID" %}</th>
            <th>{% trans "Name" %}</th>
        </thead>
    {% for alumn in alumnList %}
        <tr id="alumn{{alumn.alumnId}}">
            <td>{{ alumn.alumnId }}</td>
            <td><button class="profileAlumn" alumn_id="{{alumn.alumnId}}">{% trans "Profile" %}</button></td>
            <td><button class="deleteAlumn" alumn_id="{{alumn.alumnId}}">{% trans "Delete" %}</button></td>
        </tr>
    {% endfor %}
    </table>
{% endblock content %}

我不知道如何修复这个错误。我用谷歌搜索了这个错误,但只出现了三个模棱两可的结果页面,没有提到 HttpResponse。

请帮忙。我不知道我做错了什么。

如果有人有一些链接,我可以在其中学习如何从我的 web 应用程序中的每个呈现的 HTML 模板制作 pdf,这也会对我有所帮助。

提前非常感谢

【问题讨论】:

  • 您显示的不是有效的视图功能。请提供真实的。
  • @JuniorCompressor 我现在正在做。
  • @JuniorCompressor 我认为现在已经完成了。如果您发现除此之外的任何问题,请告诉我
  • 我又看不到file 的定义位置
  • 那里有一个错误。它应该一直是 pdfFile 。但是错误一直出现。

标签: python django httpresponse xhtml2pdf


【解决方案1】:

alumn_list 是一个视图。所以它返回一个HttpResponse 对象。但是您需要将一个字节字符串传递给pisa.CreatePDF,这是响应的内容。因此,您可以执行以下操作:

pisaStatus = pisa.CreatePDF(html.content, dest=pdfFile)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多