Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import fitz # PyMuPDF for PDF handling
|
| 3 |
+
import easyocr # OCR for text extraction
|
| 4 |
+
import tempfile
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def extract_text_with_ocr(pdf_path):
|
| 9 |
+
"""
|
| 10 |
+
Extract text with bounding box positions using OCR for both English and Arabic text.
|
| 11 |
+
:param pdf_path: Path to the input PDF file.
|
| 12 |
+
:return: List of dictionaries containing text and positions for each page.
|
| 13 |
+
"""
|
| 14 |
+
extracted_data = []
|
| 15 |
+
doc = fitz.open(pdf_path)
|
| 16 |
+
|
| 17 |
+
# Convert each PDF page to an image for OCR processing
|
| 18 |
+
for page_num in range(len(doc)):
|
| 19 |
+
page = doc.load_page(page_num)
|
| 20 |
+
pix = page.get_pixmap(dpi=300) # Convert PDF page to image
|
| 21 |
+
image_path = f"temp_page_{page_num}.png"
|
| 22 |
+
pix.save(image_path)
|
| 23 |
+
|
| 24 |
+
# Perform OCR on the image
|
| 25 |
+
reader = easyocr.Reader(['en']) # Supports English (add 'ar' for Arabic if needed)
|
| 26 |
+
results = reader.readtext(image_path, detail=1) # detail=1 returns bounding box info
|
| 27 |
+
|
| 28 |
+
# Extract text and positions
|
| 29 |
+
page_data = []
|
| 30 |
+
for (bbox, text, confidence) in results:
|
| 31 |
+
(x0, y0), (x1, y1) = bbox[0], bbox[2]
|
| 32 |
+
page_data.append({
|
| 33 |
+
"text": text,
|
| 34 |
+
"x0": x0,
|
| 35 |
+
"y0": y1, # Adjust to bottom-left corner (PDF coordinates)
|
| 36 |
+
"font_size": y1 - y0, # Approximate font size
|
| 37 |
+
"confidence": confidence
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
extracted_data.append(page_data)
|
| 41 |
+
|
| 42 |
+
# Cleanup temporary image
|
| 43 |
+
os.remove(image_path)
|
| 44 |
+
|
| 45 |
+
return extracted_data
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def overlay_text_with_fonts(pdf_path, extracted_data, output_pdf_path):
|
| 49 |
+
"""
|
| 50 |
+
Overlay extracted text onto the original PDF using fonts from different font families.
|
| 51 |
+
:param pdf_path: Path to the input PDF file.
|
| 52 |
+
:param extracted_data: List of extracted text with positions.
|
| 53 |
+
:param output_pdf_path: Path to save the output PDF file.
|
| 54 |
+
"""
|
| 55 |
+
doc = fitz.open(pdf_path)
|
| 56 |
+
|
| 57 |
+
# Define default font settings
|
| 58 |
+
default_font = "Helvetica" # You can replace it with specific fonts like "Arial" or others.
|
| 59 |
+
|
| 60 |
+
for page_num, page_data in enumerate(extracted_data):
|
| 61 |
+
page = doc[page_num]
|
| 62 |
+
|
| 63 |
+
for item in page_data:
|
| 64 |
+
if item["confidence"] > 0.8: # Only overlay high-confidence text
|
| 65 |
+
page.insert_text(
|
| 66 |
+
(item["x0"], item["y0"]),
|
| 67 |
+
item["text"],
|
| 68 |
+
fontsize=item["font_size"],
|
| 69 |
+
fontname=default_font,
|
| 70 |
+
color=(0, 0, 0), # Black text
|
| 71 |
+
render_mode=0 # Ensure text is not outlined
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
doc.save(output_pdf_path)
|
| 75 |
+
print(f"PDF saved to: {output_pdf_path}")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def process_pdf(uploaded_pdf, output_pdf_path):
|
| 79 |
+
"""
|
| 80 |
+
Process the uploaded PDF to extract text using OCR and overlay it as editable text.
|
| 81 |
+
:param uploaded_pdf: The uploaded PDF file.
|
| 82 |
+
:param output_pdf_path: Path to save the output PDF file.
|
| 83 |
+
"""
|
| 84 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
| 85 |
+
temp_pdf.write(uploaded_pdf.read())
|
| 86 |
+
temp_pdf_path = temp_pdf.name
|
| 87 |
+
|
| 88 |
+
# Step 1: Extract text using OCR
|
| 89 |
+
extracted_data = extract_text_with_ocr(temp_pdf_path)
|
| 90 |
+
|
| 91 |
+
# Step 2: Overlay extracted text onto the original PDF
|
| 92 |
+
overlay_text_with_fonts(temp_pdf_path, extracted_data, output_pdf_path)
|
| 93 |
+
|
| 94 |
+
# Cleanup temporary file
|
| 95 |
+
if os.path.exists(temp_pdf_path):
|
| 96 |
+
os.remove(temp_pdf_path)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# Streamlit App
|
| 100 |
+
def main():
|
| 101 |
+
st.title("PDF Text Conversion Tool")
|
| 102 |
+
st.write("Upload a PDF to convert vector text into regular, editable text.")
|
| 103 |
+
|
| 104 |
+
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 105 |
+
if uploaded_file:
|
| 106 |
+
output_pdf_path = "converted_output.pdf"
|
| 107 |
+
|
| 108 |
+
with st.spinner("Processing your PDF..."):
|
| 109 |
+
process_pdf(uploaded_file, output_pdf_path)
|
| 110 |
+
|
| 111 |
+
st.success("PDF processing complete!")
|
| 112 |
+
|
| 113 |
+
# Provide a download button for the processed PDF
|
| 114 |
+
with open(output_pdf_path, "rb") as f:
|
| 115 |
+
st.download_button(
|
| 116 |
+
label="Download Converted PDF",
|
| 117 |
+
data=f,
|
| 118 |
+
file_name="converted_output.pdf",
|
| 119 |
+
mime="application/pdf"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Cleanup the processed output PDF
|
| 123 |
+
if os.path.exists(output_pdf_path):
|
| 124 |
+
os.remove(output_pdf_path)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
main()
|