gpt4 book ai didi

python - Bottle框架生成pdf

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:23 25 4
gpt4 key购买 nike

我需要使用 Bottle 框架生成 PDF 文档。

我尝试了类似于 Django 的方法,但没有成功:

@bottle.route('/pd')
def create_pdf():
response.headers['Content-Type'] = 'application/pdf; charset=UTF-8'
response.headers['Content-Disposition'] = 'attachment; filename="test.pdf"'
from io import BytesIO
buffer = BytesIO()
from reportlab.pdfgen import canvas
p = canvas.Canvas(buffer)
p.drawString(100,100,'Hello World')
p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response

最佳答案

Bottle 函数不应该返回 response 对象,它们应该返回一个可迭代对象(字符串、列表、生成器等)。

所以你想要这个:

from io import BytesIO
from reportlab.pdfgen import canvas

@bottle.route('/pd')
def create_pdf():
response.headers['Content-Type'] = 'application/pdf; charset=UTF-8'
response.headers['Content-Disposition'] = 'attachment; filename="test.pdf"'

buffer = BytesIO()
p = canvas.Canvas(buffer)
p.drawString(100,100,'Hello World')
p.showPage()
p.save()

return buffer.getvalue()

关于python - Bottle框架生成pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22875561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com