0

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 %}

このバグを修正する方法がわかりません。このエラーをグーグル検索しましたが、あいまいな結果ページが 3 つしか表示されず、HttpResponse を参照しているものはありませんでした。

助けてください。何が間違っているのかわかりません。

Web アプリケーションからレンダリングされたすべての HTML テンプレートから pdf を作成する方法を学ぶことができるリンクがあれば、それも役に立ちます。

よろしくお願いします

4

1 に答える 1