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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -53
app.py CHANGED
@@ -120,6 +120,61 @@ def add_signature_to_pdf(pdf_file, signature_image, x, y):
120
  print(f"Error with pdf: {e}")
121
  return None
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  # Sidebar Navigation
124
  st.sidebar.title("Tool Selector")
125
  selection = st.sidebar.radio("Choose a tool:", ["PDF Combiner", "PDF Transcriber", "PDF Signer"])
@@ -186,56 +241,4 @@ elif selection == "PDF Signer":
186
  pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
187
  signature_image = st.file_uploader("Upload Signature Image", type=["png", "jpg", "jpeg"])
188
  if pdf_file and signature_image:
189
- with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
190
- try:
191
- temp_pdf.write(pdf_file.read())
192
- temp_pdf_path = temp_pdf.name
193
- print(f"temp file name: {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
 
120
  print(f"Error with pdf: {e}")
121
  return None
122
 
123
+ def pdf_signer_ui(pdf_file, signature_image):
124
+ """Handles the UI and logic for PDF signing."""
125
+ with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
126
+ try:
127
+ temp_pdf.write(pdf_file.read())
128
+ temp_pdf_path = temp_pdf.name
129
+ print(f"temp file name: {temp_pdf_path}")
130
+ except Exception as e:
131
+ print(f"Error creating temporary file: {e}")
132
+ st.error("Could not create temporary file. Please make sure your uploaded file is valid")
133
+ return
134
+
135
+ try:
136
+ doc = fitz.open(temp_pdf_path)
137
+ page = doc[0]
138
+ pix = page.get_pixmap()
139
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
140
+ st.image(img, use_container_width=True)
141
+ if st.button("Add Signature"):
142
+ x = st.session_state.get("x", 50)
143
+ y = st.session_state.get("y", 50)
144
+ if 'clicked_image' not in st.session_state:
145
+ st.session_state.clicked_image = False
146
+ if st.session_state.clicked_image:
147
+ with st.spinner("Adding Signature..."):
148
+ if pdf_file and signature_image: # Check for file existence
149
+ output_file = add_signature_to_pdf(pdf_file, signature_image, x, y)
150
+ if output_file:
151
+ st.success("Signature added successfully")
152
+ with open(output_file, "rb") as f:
153
+ st.download_button("Download Signed PDF", f, file_name=output_file, mime="application/pdf")
154
+ os.remove(output_file)
155
+ else:
156
+ st.error("An Error occurred when creating the signed PDF")
157
+ else:
158
+ st.error("Please upload both a PDF file and a signature image")
159
+
160
+ else:
161
+ clicked = st.image(img, use_container_width=True, key="clicked_image", on_click=set_session, )
162
+ if clicked:
163
+ st.session_state.x = clicked.x
164
+ st.session_state.y = clicked.y
165
+ st.session_state.clicked_image = True
166
+ st.experimental_rerun()
167
+ os.remove(temp_pdf_path)
168
+
169
+ except Exception as e:
170
+ print(f"Error with pdf: {e}")
171
+ st.error(f"Could not open PDF file {e}")
172
+ os.remove(temp_pdf_path)
173
+
174
+ def set_session():
175
+ st.session_state.clicked_image = True
176
+
177
+
178
  # Sidebar Navigation
179
  st.sidebar.title("Tool Selector")
180
  selection = st.sidebar.radio("Choose a tool:", ["PDF Combiner", "PDF Transcriber", "PDF Signer"])
 
241
  pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
242
  signature_image = st.file_uploader("Upload Signature Image", type=["png", "jpg", "jpeg"])
243
  if pdf_file and signature_image:
244
+ pdf_signer_ui(pdf_file, signature_image)