django - 为什么 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>... 必须至少有一行和一列
我不知道我在这里做错了什么,请指教。
解决方案
推荐阅读
- r - ggplot2 - 2 变量聚集柱形图 - 变量 1 的渐变填充和变量 2 的实心填充
- angularjs - Angularjs 中的构造函数
- java - 如何缩小数字以猜测数字程序的逻辑错误
- c# - 如何在 Arcmap 中闪烁隐藏 DockableWindow 的选项卡
- javascript - PHP & AJAX - 获取数据时在另一个页面上不刷新
- java - 为什么 LinkedHashSet 没有 addFirst 方法?
- javascript - 显示月份和日期的 Javascript 日历
- css - 将图像大小调整为居中
- c# - SQLKata 简单 SELECT 查询返回合法表的“无效对象名称”?
- python - Python数学用科学计数法计算变量