Django 和 Reportlab 问题
我写了这个小的 Django 视图来返回 pdf。
@login_required
def code_view(request,myid):
try:
deal = Deal.objects.get(id=myid)
except:
raise Http404
header = deal.header
code = deal.code
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=code.pdf'
p = canvas.Canvas(response)
p.drawString(10, 800, header)
p.drawString(10, 700, code)
p.showPage()
p.save()
return response
我的问题是:
- pdf 中未正确显示 Utf-8 字符。
- 我怎样才能包含图像?
- 我如何包含一个非常基本的 html,例如:
.
<ul>
<li>List One</li>
<li>List Two</li>
<li>List Three</li>
</ul>
I have written this small Django view to return pdf.
@login_required
def code_view(request,myid):
try:
deal = Deal.objects.get(id=myid)
except:
raise Http404
header = deal.header
code = deal.code
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=code.pdf'
p = canvas.Canvas(response)
p.drawString(10, 800, header)
p.drawString(10, 700, code)
p.showPage()
p.save()
return response
And my questions:
- Utf-8 characters are not shown correctly within the pdf.
- How can I include an image ?
- How can I include a very basic html such as:
.
<ul>
<li>List One</li>
<li>List Two</li>
<li>List Three</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该进入下一个级别并使用 DocTemplates。
图像很容易,但使用项目符号真的很困难 - 您必须定义样式等等!
我使用一组如下所示的类:
然后您可以在视图中处理 pdf 生成,如下所示:
You should move to the next level and use DocTemplates.
Images are quite easy, but using bullets is really hard - you have to define styles and more!
I use a set of classes like the below:
You could then handle the pdf generation in your view, like this:
Reportlab 可以处理一些基本的 HTML 格式(
、
),不确定是否可以处理列表。您可以使用
pisa
将 HTML 转换为 PDF。然后您还可以使用标签来包含图像(您需要安装 PIL 用于使用图像)
Reportlab can handle some basic HTML formatting (
<b>
,<i>
), not sure if it can do lists. You could usepisa
for HTML to PDF conversion. Then you could also use<img>
tag for image inclusion (You need to install PIL for using image)