Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -194,33 +194,48 @@ elif selection == "PDF Signer":
|
|
| 194 |
except Exception as e:
|
| 195 |
print(f"Error creating temporary file: {e}")
|
| 196 |
st.error("Could not create temporary file. Please make sure your uploaded file is valid")
|
| 197 |
-
return
|
| 198 |
|
| 199 |
try:
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
except Exception as e:
|
| 195 |
print(f"Error creating temporary file: {e}")
|
| 196 |
st.error("Could not create temporary file. Please make sure your uploaded file is valid")
|
| 197 |
+
return
|
| 198 |
|
| 199 |
try:
|
| 200 |
+
doc = fitz.open(temp_pdf_path)
|
| 201 |
+
page = doc[0]
|
| 202 |
+
pix = page.get_pixmap()
|
| 203 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 204 |
+
st.image(img, use_container_width=True)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
if st.button("Add Signature"):
|
| 208 |
+
x = st.session_state.get("x", 50)
|
| 209 |
+
y = st.session_state.get("y", 50)
|
| 210 |
+
|
| 211 |
+
if 'clicked_image' not in st.session_state:
|
| 212 |
+
st.session_state.clicked_image = False
|
| 213 |
+
if st.session_state.clicked_image:
|
| 214 |
+
with st.spinner("Adding Signature..."):
|
| 215 |
+
if pdf_file and signature_image: # Check for file existence
|
| 216 |
+
output_file = add_signature_to_pdf(pdf_file, signature_image, x, y)
|
| 217 |
+
if output_file:
|
| 218 |
+
st.success("Signature added successfully")
|
| 219 |
+
with open(output_file, "rb") as f:
|
| 220 |
+
st.download_button("Download Signed PDF", f, file_name = output_file, mime = "application/pdf")
|
| 221 |
+
os.remove(output_file)
|
| 222 |
+
else:
|
| 223 |
+
st.error("An Error occurred when creating the signed PDF")
|
| 224 |
+
else:
|
| 225 |
+
st.error("Please upload both a PDF file and a signature image")
|
| 226 |
+
else:
|
| 227 |
+
clicked = st.image(img, use_container_width=True, key="clicked_image", on_click = set_session, )
|
| 228 |
+
if clicked:
|
| 229 |
+
st.session_state.x = clicked.x
|
| 230 |
+
st.session_state.y = clicked.y
|
| 231 |
+
st.session_state.clicked_image = True
|
| 232 |
+
st.experimental_rerun()
|
| 233 |
+
|
| 234 |
+
os.remove(temp_pdf_path)
|
| 235 |
except Exception as e:
|
| 236 |
+
print(f"Error with pdf: {e}")
|
| 237 |
+
st.error(f"Could not open PDF file {e}")
|
| 238 |
+
os.remove(temp_pdf_path)
|
| 239 |
+
|
| 240 |
+
def set_session():
|
| 241 |
+
st.session_state.clicked_image = True
|