bytesio to create zip file from png bytes, download it

This commit is contained in:
admin 2026-02-25 16:11:11 -08:00
parent 1f92945ee4
commit fa778d5360

18
app.py
View File

@ -73,6 +73,11 @@ def index():
stylesheets=[CSS(string=read_css_file())]
)
doc = fitz.open(stream=pdf_data, filetype="pdf")
memory_file = io.BytesIO()
# Create the zip file in the buffer
with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_DEFLATED) as zf:
for page_index in range(len(doc)):
page = doc.load_page(page_index)
pix = page.get_pixmap(dpi=240)
@ -82,9 +87,22 @@ def index():
# You can now send png_bytes to a response or store it in a DB
print(f"Page {page_index} is {len(png_bytes)} bytes.")
zf.writestr("SLIDES/{:04d}.png".format(page_index), png_bytes)
doc.close()
# Crucial step: rewind the buffer position to the beginning so the file can be read for download
memory_file.seek(0)
# Send the in-memory file using Flask's send_file
return send_file(
memory_file,
mimetype='application/zip',
as_attachment=True,
download_name='downloaded_files.zip' # Use download_name for modern Flask versions
)
print(f"Reference: {reference}, Version: {version}, Text: {verse_text}, Error: {error}")
return render_template('index.html', reference=reference, version=version, verse_html=verse_html, error=error)