Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,64 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PyPDF2 import PdfMerger
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
uploaded_files = st.file_uploader("Upload PDF pages", type="pdf", accept_multiple_files=True)
|
| 10 |
|
| 11 |
if uploaded_files:
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
merger
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
merger.write(output_file)
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PyPDF2 import PdfMerger
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
import os
|
| 7 |
+
from streamlit_sortables import sort_items
|
| 8 |
|
| 9 |
+
# Function to generate thumbnail from PDF
|
| 10 |
+
def get_pdf_thumbnail(uploaded_file):
|
| 11 |
+
doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
|
| 12 |
+
page = doc.load_page(0)
|
| 13 |
+
pix = page.get_pixmap()
|
| 14 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 15 |
+
img.thumbnail((100, 140))
|
| 16 |
+
return img
|
| 17 |
+
|
| 18 |
+
# App layout
|
| 19 |
+
st.title("PDF Combiner with Preview & Reordering")
|
| 20 |
+
st.write("Upload individual PDF pages, visualize them, reorder, and merge into a single PDF.")
|
| 21 |
|
| 22 |
uploaded_files = st.file_uploader("Upload PDF pages", type="pdf", accept_multiple_files=True)
|
| 23 |
|
| 24 |
if uploaded_files:
|
| 25 |
+
# Generate thumbnails for each PDF
|
| 26 |
+
thumbnails = []
|
| 27 |
+
filenames = []
|
| 28 |
+
|
| 29 |
+
for file in uploaded_files:
|
| 30 |
+
thumbnails.append(get_pdf_thumbnail(file))
|
| 31 |
+
filenames.append(file.name)
|
| 32 |
+
|
| 33 |
+
# Display thumbnails with filenames for reordering
|
| 34 |
+
st.write("**Drag and drop to reorder the PDFs:**")
|
| 35 |
+
reordered_filenames = sort_items(filenames, labels=[file.name for file in uploaded_files])
|
| 36 |
+
|
| 37 |
+
# Map the filenames back to the corresponding files
|
| 38 |
+
reordered_files = [uploaded_files[filenames.index(name)] for name in reordered_filenames]
|
| 39 |
+
|
| 40 |
+
# Display the thumbnails in the new order
|
| 41 |
+
st.write("**Preview of selected order:**")
|
| 42 |
+
cols = st.columns(len(reordered_files))
|
| 43 |
+
for idx, file in enumerate(reordered_files):
|
| 44 |
+
with cols[idx]:
|
| 45 |
+
st.image(get_pdf_thumbnail(file), caption=file.name, use_column_width=True)
|
| 46 |
|
| 47 |
+
# Merge PDFs in the specified order
|
| 48 |
+
if st.button("Merge PDFs"):
|
| 49 |
+
merger = PdfMerger()
|
| 50 |
+
for file in reordered_files:
|
| 51 |
+
merger.append(file)
|
| 52 |
|
| 53 |
+
output_filename = "combined_document.pdf"
|
| 54 |
+
with open(output_filename, "wb") as output_file:
|
| 55 |
+
merger.write(output_file)
|
|
|
|
| 56 |
|
| 57 |
+
st.success("PDF pages combined successfully!")
|
| 58 |
|
| 59 |
+
# Provide download link
|
| 60 |
+
with open(output_filename, "rb") as f:
|
| 61 |
+
st.download_button("Download Combined PDF", f, file_name=output_filename, mime="application/pdf")
|
| 62 |
|
| 63 |
+
# Clean up
|
| 64 |
+
os.remove(output_filename)
|