【发布时间】:2019-10-17 09:16:11
【问题描述】:
我使用 Django 创建了一个网站,并创建了一个从数据库数据返回自动生成的 pdf 的函数。
为了制作 PDF,我使用 pyFPDF。
pdfreport = FPDF()
#Code for the PDF here
pdfreport.output(name='mypdf.pdf')
使用此代码一切正常,PDF 已正确生成。
但为了将文件作为 HTTP 响应返回,我将文件保存为 dest='S' 并做出响应。
pdf = pdfreport.output(dest='S')
response = = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="mypdf.pdf"'
return(response)
使用这个,响应是正确的,我得到了要保存的文件,浏览器将文件加载为 pdf,但 pdf 是空白的。而且我不知道为什么会这样。
我已尝试保存文件,使用open() 打开文件并附加其内容,但我仍然得到相同的结果,一个空白的 pdf。
pdfreport.output(name=pdfname)
pdf = open(pdfname, 'r')
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="mypdf.pdf"'
return(response)
谁能帮帮我?
【问题讨论】: