ZaraThoustra
ZaraThoustra

Reputation: 115

Flask - PyPDF2 - Exporting in memory pdf file

I'm trying to export a pdf file from a flask app but for some reason I can't seem to write it correctly. It does work when I'm exporting to my local folder but I get a blank pdf when exporting through Flask. Any ideas ?

    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'})

Upvotes: 0

Views: 1091

Answers (2)

ZaraThoustra
ZaraThoustra

Reputation: 115

Here is the clean code thanks to @darth baba. I've also added a pptx export that works with this method :

#
#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')

Upvotes: 0

darth baba
darth baba

Reputation: 1398

BytesIO is just a buffer containing some data it isn't associated with any real file on the disk, It's just a chunk of memory that behaves like a file. You first need to write the contents of PdfFileWriter() to the buffer then write the values of the buffer to the pdf file. Flask has a neat function send_file which allows you to send the contents of a file to the client.

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')

Upvotes: 2

Related Questions