渲染从 SimpleDocTemplate 构建的 ReportLab pdf
我有一个 django 应用程序,当前使用用户可以下载的画布生成 pdf。我创建一个 StringIO 缓冲区,执行一些操作,然后发送调用 response.write。
# Set up response
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=menu-%s.pdf' % str(menu_id)
# buffer
buff = StringIO()
# Create the pdf object
p = canvas.Canvas(buff)
# Add some elements... then
p.showPage()
p.save()
# Get the pdf from the buffer and return the response
pdf = buff.getvalue()
buff.close()
response.write(pdf)
我现在想使用 platypus 和 SimpleDocTemplate 构建我的 pdf 并编写了这个,
# Set up response
response = HttpResponse(mimetype='application/pdf')
pdf_name = "menu-%s.pdf" % str(menu_id)
response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name
menu_pdf = SimpleDocTemplate(pdf_name, rightMargin=72,
leftMargin=72, topMargin=72, bottomMargin=18)
# container for pdf elements
elements = []
styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER))
# Add the content as before then...
menu_pdf.build(elements)
response.write(menu_pdf)
return response
但这不起作用,它会创建一个无法打开的错误 pdf。我认为该行
response.write(menu_pdf)
不正确。
如何渲染 pdf?
I've a got a django app that currently generates pdfs using a canvas that the user can download. I create a StringIO buffer, do some stuff and then send call response.write.
# Set up response
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=menu-%s.pdf' % str(menu_id)
# buffer
buff = StringIO()
# Create the pdf object
p = canvas.Canvas(buff)
# Add some elements... then
p.showPage()
p.save()
# Get the pdf from the buffer and return the response
pdf = buff.getvalue()
buff.close()
response.write(pdf)
I now want to build my pdf using platypus and SimpleDocTemplate and have written this
# Set up response
response = HttpResponse(mimetype='application/pdf')
pdf_name = "menu-%s.pdf" % str(menu_id)
response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name
menu_pdf = SimpleDocTemplate(pdf_name, rightMargin=72,
leftMargin=72, topMargin=72, bottomMargin=18)
# container for pdf elements
elements = []
styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER))
# Add the content as before then...
menu_pdf.build(elements)
response.write(menu_pdf)
return response
But this doesn't work, it creates a bad pdf that cannot be opened. I presume the line
response.write(menu_pdf)
is incorrect.
How do I render the pdf?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的错误实际上是一个非常简单的错误。这只是一个试图写错东西的问题。在您的代码中,
menu_pdf
不是PDF,而是SimpleDocTemplate
,并且PDF已存储在pdf_name
中,尽管这里我怀疑pdf_name
是路径名而不是文件对象。要修复它,请更改您的代码以使用内存文件,就像您在原始代码中所做的那样:我不确定文档中是否提到使用文件对象而不是 Platypus 的路径,但如果您深入研究代码,您会发现看到这是可能的。
Your error is actually a pretty simple one. It's just a matter of trying to write the wrong thing. In your code,
menu_pdf
is not a PDF, but aSimpleDocTemplate
, and the PDF has been stored inpdf_name
, although here I suspectpdf_name
is a path name rather than a file object. To fix it, change your code to use a memory file like you did in your original code:I'm not sure if using file objects rather than paths with Platypus is mentioned in the documentation, but if you dig into the code you'll see that it is possible.
对于使用 python3 和 django 1.7+ 的人来说,需要对答案进行一些更改。
For people who are working with python3 and django 1.7+ some changes to the answer need to be done.