Flask - PyPDF2 - 在内存中导出 pdf 文件

Flask - PyPDF2 - Exporting in memory pdf file

我正在尝试从 Flask 应用导出 pdf 文件,但出于某种原因,我似乎无法正确编写它。 当我导出到我的本地文件夹时它确实有效,但当通过 Flask 导出时我得到一个空白的 pdf。 有什么想法吗?

    pdf = PdfFileWriter()
    p1 = PdfFileReader(open(os.path.join(STATICDIR,'page1.pdf'), "rb"))
    p2 = PdfFileReader(open(os.path.join(STATICDIR,'page2.pdf'), "rb"))
    p3 = PdfFileReader(open(os.path.join(STATICDIR,'lastpage.pdf'), "rb"))

    pdf.addPage(p1.getPage(0))
    pdf.addPage(p2.getPage(0))
    pdf.addPage(p3.getPage(0))
    
    #this works
    #outputStream = open(r"output.pdf", "wb")
    #pdf.write(outputStream)

    outfile = BytesIO()
    pdf.write(outfile)

    return Response(pdf, mimetype='application/pdf',
                    headers={'Content-Disposition': 'attachment;filename=output.pdf'})

BytesIO 只是一个包含一些数据的缓冲区,它与磁盘上的任何真实文件都没有关联,它只是一块行为类似于文件的内存。您首先需要将 PdfFileWriter() 的内容写入缓冲区,然后将缓冲区的值写入 pdf 文件。 Flask 有一个简洁的函数 send_file 允许您将文件的内容发送到客户端。

from pypdf2 import PdfFileWriter
from flask import send_file

pdf = PdfFileWriter()
p1 = PdfFileReader(open(os.path.join(STATICDIR,'page1.pdf'), "rb"))
p2 = PdfFileReader(open(os.path.join(STATICDIR,'page2.pdf'), "rb"))
p3 = PdfFileReader(open(os.path.join(STATICDIR,'lastpage.pdf'), "rb"))

pdf.addPage(p1.getPage(0))
pdf.addPage(p2.getPage(0))
pdf.addPage(p3.getPage(0))
    
#this works
outputStream = open(r"output.pdf", "wb")
pdf.write(outputStream)

outfile = BytesIO()
pdf.write(outfile)

with open("output.pdf", "wb") as f:
    f.write(outfile.getvalue())

return send_file('./output.pdf', mimetype='application/pdf')

感谢@darth baba,这是干净的代码。 我还添加了一个适用于此方法的 pptx 导出:

#
#1. PDF EXPORT
#

from pypdf2 import PdfFileWriter
from flask import send_file

pdf = PdfFileWriter()
p1 = PdfFileReader(open(os.path.join(STATICDIR,'page1.pdf'), "rb"))
p2 = PdfFileReader(open(os.path.join(STATICDIR,'page2.pdf'), "rb"))
p3 = PdfFileReader(open(os.path.join(STATICDIR,'lastpage.pdf'), "rb"))

pdf.addPage(p1.getPage(0))
pdf.addPage(p2.getPage(0))
pdf.addPage(p3.getPage(0))

#this works
outputStream = open(r"output.pdf", "wb")
pdf.write(outputStream)

outfile = BytesIO()
pdf.write(outfile)
outfile.seek(0)

return send_file(outfile,
                 mimetype='application/pdf'
                 attachment_filename='output.pptx')

#
#2. PPTX EXPORT
#

from pptx import Presentation
from flask import send_file

prs = gen.GenPPT(STATICDIR, lst_ath, lst_choice_sous_cat) #creates presentation
outfile = BytesIO()
prs.save(outfile)
outfile.seek(0)

return send_file(outfile,
                 mimetype="application/vnd.openxmlformats-officedocument.presentationml.presentation",
                 as_attachment=True,
                 attachment_filename='output.pptx')