Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ["PATH"] += os.pathsep + "/usr/bin" + os.pathsep + "/usr/local/bin"
|
| 3 |
print(f"Current PATH: {os.environ['PATH']}")
|
| 4 |
import streamlit as st
|
| 5 |
from PyPDF2 import PdfMerger
|
|
@@ -24,13 +24,6 @@ else:
|
|
| 24 |
print("pdfinfo does not exist at /usr/bin/pdfinfo")
|
| 25 |
|
| 26 |
|
| 27 |
-
try:
|
| 28 |
-
output = subprocess.check_output(["/usr/bin/pdfinfo", "-version"])
|
| 29 |
-
print(f"Poppler version: {output.decode('utf-8')}")
|
| 30 |
-
except FileNotFoundError:
|
| 31 |
-
print("Poppler (pdfinfo) is not installed or not found in PATH.")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
# Load Google Cloud Vision credentials from secret
|
| 35 |
credentials_json = os.getenv("GOOGLE_CREDENTIALS_JSON")
|
| 36 |
if credentials_json:
|
|
@@ -100,83 +93,33 @@ def merge_pdfs(reordered_files):
|
|
| 100 |
merger.write(output_file)
|
| 101 |
return output_filename
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
temp_pdf_path = temp_pdf.name
|
| 111 |
-
print(f"temp file name: {temp_pdf_path}")
|
| 112 |
-
except Exception as e:
|
| 113 |
-
print(f"Error creating temp file: {e}")
|
| 114 |
-
return None
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
page
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
img_bytes.seek(0)
|
| 125 |
-
page.insert_image(rect, stream=img_bytes.read(), keep_proportion=True)
|
| 126 |
-
output_filename = "signed_document.pdf"
|
| 127 |
-
doc.save(output_filename)
|
| 128 |
-
os.remove(temp_pdf_path)
|
| 129 |
-
return output_filename
|
| 130 |
-
except Exception as e:
|
| 131 |
-
print(f"Error with pdf: {e}")
|
| 132 |
-
return None
|
| 133 |
-
|
| 134 |
-
def pdf_signer_ui(pdf_file, signature_image):
|
| 135 |
-
"""Handles the UI and logic for PDF signing."""
|
| 136 |
-
if st.button("Preview PDF"):
|
| 137 |
-
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
|
| 138 |
-
try:
|
| 139 |
-
temp_pdf.write(pdf_file.read())
|
| 140 |
-
temp_pdf_path = temp_pdf.name
|
| 141 |
-
print(f"temp file name: {temp_pdf_path}")
|
| 142 |
-
except Exception as e:
|
| 143 |
-
print(f"Error creating temporary file: {e}")
|
| 144 |
-
st.error("Could not create temporary file. Please make sure your uploaded file is valid")
|
| 145 |
-
return
|
| 146 |
-
|
| 147 |
-
try:
|
| 148 |
-
doc = fitz.open(temp_pdf_path)
|
| 149 |
-
page = doc[0]
|
| 150 |
-
pix = page.get_pixmap()
|
| 151 |
-
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 152 |
-
clicked = st.image(img, use_container_width=True)
|
| 153 |
-
if clicked:
|
| 154 |
-
x = clicked.x
|
| 155 |
-
y = clicked.y
|
| 156 |
-
if st.button("Add Signature"):
|
| 157 |
-
with st.spinner("Adding Signature..."):
|
| 158 |
-
if pdf_file and signature_image: # Check for file existence
|
| 159 |
-
output_file = add_signature_to_pdf(pdf_file, signature_image, x, y)
|
| 160 |
-
if output_file:
|
| 161 |
-
st.success("Signature added successfully")
|
| 162 |
-
with open(output_file, "rb") as f:
|
| 163 |
-
st.download_button("Download Signed PDF", f, file_name=output_file, mime="application/pdf")
|
| 164 |
-
os.remove(output_file)
|
| 165 |
-
else:
|
| 166 |
-
st.error("An Error occurred when creating the signed PDF")
|
| 167 |
-
else:
|
| 168 |
-
st.error("Please upload both a PDF file and a signature image")
|
| 169 |
-
|
| 170 |
-
os.remove(temp_pdf_path)
|
| 171 |
-
|
| 172 |
-
except Exception as e:
|
| 173 |
-
print(f"Error with pdf: {e}")
|
| 174 |
-
st.error(f"Could not open PDF file {e}")
|
| 175 |
-
os.remove(temp_pdf_path)
|
| 176 |
|
| 177 |
# Sidebar Navigation
|
| 178 |
st.sidebar.title("Tool Selector")
|
| 179 |
-
selection = st.sidebar.radio("Choose a tool:", ["PDF Combiner", "PDF Transcriber", "PDF
|
| 180 |
|
| 181 |
# PDF Combiner with Preview and Reordering
|
| 182 |
if selection == "PDF Combiner":
|
|
@@ -212,8 +155,7 @@ if selection == "PDF Combiner":
|
|
| 212 |
if st.button("Merge PDFs"):
|
| 213 |
output_file = merge_pdfs(reordered_files)
|
| 214 |
st.success("PDF pages combined successfully!")
|
| 215 |
-
|
| 216 |
-
st.download_button("Download Combined PDF", f, file_name=output_file, mime="application/pdf")
|
| 217 |
os.remove(output_file)
|
| 218 |
|
| 219 |
# PDF Transcriber Tool
|
|
@@ -229,15 +171,20 @@ elif selection == "PDF Transcriber":
|
|
| 229 |
st.success("Text extraction complete!")
|
| 230 |
st.text_area("Extracted Text", pdf_text, height=400)
|
| 231 |
output_file_name = f"(T){os.path.splitext(uploaded_file.name)[0]}.txt"
|
| 232 |
-
|
| 233 |
|
| 234 |
|
| 235 |
-
# PDF
|
| 236 |
-
elif selection == "PDF
|
| 237 |
-
st.title("PDF
|
| 238 |
-
st.write("Upload a PDF and
|
| 239 |
|
| 240 |
pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
#os.environ["PATH"] += os.pathsep + "/usr/bin" + os.pathsep + "/usr/local/bin" #Removed redundant path setting
|
| 3 |
print(f"Current PATH: {os.environ['PATH']}")
|
| 4 |
import streamlit as st
|
| 5 |
from PyPDF2 import PdfMerger
|
|
|
|
| 24 |
print("pdfinfo does not exist at /usr/bin/pdfinfo")
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Load Google Cloud Vision credentials from secret
|
| 28 |
credentials_json = os.getenv("GOOGLE_CREDENTIALS_JSON")
|
| 29 |
if credentials_json:
|
|
|
|
| 93 |
merger.write(output_file)
|
| 94 |
return output_filename
|
| 95 |
|
| 96 |
+
def download_file(output_file, file_name, mime_type):
|
| 97 |
+
with open(output_file, "rb") as f:
|
| 98 |
+
st.download_button(
|
| 99 |
+
label="Download File",
|
| 100 |
+
data=f,
|
| 101 |
+
file_name=file_name,
|
| 102 |
+
mime=mime_type,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
def rotate_pdf(pdf_file, rotation_angle):
|
| 106 |
+
"""Rotates all pages of a PDF by a specified angle."""
|
| 107 |
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
|
| 108 |
+
temp_pdf.write(pdf_file.read())
|
| 109 |
+
temp_pdf_path = temp_pdf.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
doc = fitz.open(temp_pdf_path)
|
| 112 |
+
for page in doc:
|
| 113 |
+
page.set_rotation(rotation_angle)
|
| 114 |
+
output_filename = "rotated_document.pdf"
|
| 115 |
+
doc.save(output_filename)
|
| 116 |
+
os.remove(temp_pdf_path)
|
| 117 |
+
|
| 118 |
+
return output_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Sidebar Navigation
|
| 121 |
st.sidebar.title("Tool Selector")
|
| 122 |
+
selection = st.sidebar.radio("Choose a tool:", ["PDF Combiner", "PDF Transcriber", "PDF Rotator"])
|
| 123 |
|
| 124 |
# PDF Combiner with Preview and Reordering
|
| 125 |
if selection == "PDF Combiner":
|
|
|
|
| 155 |
if st.button("Merge PDFs"):
|
| 156 |
output_file = merge_pdfs(reordered_files)
|
| 157 |
st.success("PDF pages combined successfully!")
|
| 158 |
+
download_file(output_file, "combined_document.pdf", "application/pdf")
|
|
|
|
| 159 |
os.remove(output_file)
|
| 160 |
|
| 161 |
# PDF Transcriber Tool
|
|
|
|
| 171 |
st.success("Text extraction complete!")
|
| 172 |
st.text_area("Extracted Text", pdf_text, height=400)
|
| 173 |
output_file_name = f"(T){os.path.splitext(uploaded_file.name)[0]}.txt"
|
| 174 |
+
download_file(output_file_name, f"(T){os.path.splitext(uploaded_file.name)[0]}.txt", "text/plain")
|
| 175 |
|
| 176 |
|
| 177 |
+
# PDF Rotator Tool
|
| 178 |
+
elif selection == "PDF Rotator":
|
| 179 |
+
st.title("PDF Rotator")
|
| 180 |
+
st.write("Upload a PDF and rotate all pages by a specified angle.")
|
| 181 |
|
| 182 |
pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 183 |
+
rotation_angle = st.selectbox("Select rotation angle:", [90, 180, 270])
|
| 184 |
+
|
| 185 |
+
if pdf_file and st.button("Rotate PDF"):
|
| 186 |
+
with st.spinner("Rotating PDF..."):
|
| 187 |
+
output_file = rotate_pdf(pdf_file, rotation_angle)
|
| 188 |
+
st.success("PDF rotated successfully!")
|
| 189 |
+
download_file(output_file, "rotated_document.pdf", "application/pdf")
|
| 190 |
+
os.remove(output_file)
|