errantanomie commited on
Commit
c71352a
·
verified ·
1 Parent(s): 0b871d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -27
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 # This is the return we were checking
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
- clicked = st.image(img, use_container_width=True)
206
- os.remove(temp_pdf_path)
207
-
208
- if clicked:
209
- x = clicked.x
210
- y = clicked.y
211
- with st.spinner("Adding Signature..."):
212
- if pdf_file and signature_image: # Check for file existence
213
- output_file = add_signature_to_pdf(pdf_file, signature_image, x, y)
214
- if output_file:
215
- st.success("Signature added successfully")
216
- with open(output_file, "rb") as f:
217
- st.download_button("Download Signed PDF", f, file_name = output_file, mime = "application/pdf")
218
- os.remove(output_file)
219
- else:
220
- st.error("An Error occurred when creating the signed PDF - Error 101")
221
- else:
222
- st.error("Please upload both a PDF file and a signature image")
 
 
 
 
 
 
 
 
 
 
 
 
223
  except Exception as e:
224
- print(f"Error with pdf: {e}")
225
- st.error(f"Could not open PDF file {e}")
226
- os.remove(temp_pdf_path)
 
 
 
 
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