渲染从SimpleDocTemplate构建的ReportLab pdf [英] rendering a ReportLab pdf built from SimpleDocTemplate

查看:55
本文介绍了渲染从SimpleDocTemplate构建的ReportLab pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django应用程序,该应用程序当前使用用户可以下载的画布生成pdf.我创建一个StringIO缓冲区,做一些事情,然后发送调用response.write.

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)

我现在想使用platypus和SimpleDocTemplate构建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

但是这不起作用,它会创建一个无法打开的错误pdf.我猜想这行

But this doesn't work, it creates a bad pdf that cannot be opened. I presume the line

response.write(menu_pdf)

不正确.

如何渲染pdf?

推荐答案

您的错误实际上是一个非常简单的错误.这只是尝试写错东西的问题.在您的代码中, menu_pdf 不是PDF,而是 SimpleDocTemplate ,并且PDF已存储在 pdf_name 中,尽管我怀疑> pdf_name 是路径名而不是文件对象.要对其进行修复,请像在原始代码中一样更改代码以使用内存文件:

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 a SimpleDocTemplate, and the PDF has been stored in pdf_name, although here I suspect pdf_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:

# Set up response
response = HttpResponse(mimetype='application/pdf')
pdf_name = "menu-%s.pdf" % str(menu_id)
response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name

buff = StringIO()

menu_pdf = SimpleDocTemplate(buff, 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(buff.getvalue())
buff.close()
return response

我不确定文档中是否提到使用文件对象而不是Platypus的路径,但是如果您深入研究代码,将会发现这是可能的.

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.

这篇关于渲染从SimpleDocTemplate构建的ReportLab pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆