首页 > 解决方案 > 为什么 django 库 xhtml2pdf 返回错误?

问题描述

我在 drf viewset 中使用 xhtml2pdf 库从列出给定模型的所有记录的 html 模板生成 pdf 文档。以下是我在我的视图中定义的动作

@action(detail=False)
def pdf(self, request, *args, **kwargs):
    import datetime
    filter = self.filter_queryset(Ledger.objects.values_list('name','groupid__name','panno', gstin'))
    pdf_obj = render_to_pdf('reports/report.html', {'data': filter})
    if pdf_obj:
        response = HttpResponse(pdf_obj, content_type='application/pdf')
        filename = 'Ledgers-' + str(datetime.date.today()) + '.pdf'
        content = "inline; filename=%s" % (filename)
        download = request.GET.get("download")
        if download:
            content = "attachment; filename=%s" % (filename)
        response['Content-Disposition'] = content
        return response

这会导致 ValueError 异常并显示以下消息

<PmlTable@0x7F7C4304B390 2 rows x 0 cols>... 必须至少有一行和一列

我不知道我在这里做错了什么,请指教。

标签: djangodjango-rest-frameworkdjango-templatesxhtml2pdf

解决方案


推荐阅读