| import fitz |
| from reportlab.pdfgen import canvas |
| from reportlab.lib.pagesizes import letter |
| import streamlit as st |
| import tempfile |
| import os |
| from PIL import Image |
|
|
|
|
| def convert_pdf_to_images(pdf_path): |
| """ |
| Converts PDF pages to images using PyMuPDF (fitz). |
| """ |
| doc = fitz.open(pdf_path) |
| image_paths = [] |
| for page_num in range(len(doc)): |
| page = doc.load_page(page_num) |
| pix = page.get_pixmap(dpi=300) |
| temp_image_path = f"temp_page_{page_num + 1}.png" |
| pix.save(temp_image_path) |
| image_paths.append(temp_image_path) |
| return image_paths |
|
|
|
|
| def ocr_text_from_image(image_path): |
| """ |
| Perform OCR on the given image to extract text. |
| """ |
| image = Image.open(image_path) |
| text = pytesseract.image_to_string(image, lang="eng") |
| return text |
|
|
|
|
| def extract_to_editable_pdf(uploaded_file, output_pdf_path): |
| """ |
| Extracts text using OCR and diagrams separately, and saves as an editable PDF. |
| """ |
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: |
| temp_file.write(uploaded_file.read()) |
| temp_file_path = temp_file.name |
|
|
| |
| images = convert_pdf_to_images(temp_file_path) |
| c = canvas.Canvas(output_pdf_path, pagesize=letter) |
|
|
| for i, image_path in enumerate(images): |
| |
| text = ocr_text_from_image(image_path) |
|
|
| |
| c.drawString(50, 750, f"Page {i + 1}") |
| text_lines = text.split("\n") |
| y = 730 |
| for line in text_lines: |
| c.drawString(50, y, line) |
| y -= 12 |
|
|
| |
| c.drawImage(image_path, 50, 300, width=500, height=400) |
|
|
| |
| os.remove(image_path) |
|
|
| c.save() |
|
|
| |
| os.remove(temp_file_path) |
|
|
|
|
| def main(): |
| """ |
| Streamlit app for converting PDFs into editable formats. |
| """ |
| st.title("PDF to Editable CorelDRAW Converter") |
| st.write("Upload a vector-based PDF to convert it into editable formats.") |
|
|
| |
| uploaded_file = st.file_uploader("Upload PDF", type=["pdf"]) |
|
|
| if uploaded_file: |
| |
| with st.spinner("Processing..."): |
| output_pdf_path = "editable_output.pdf" |
| extract_to_editable_pdf(uploaded_file, output_pdf_path) |
|
|
| st.success("Processing complete!") |
|
|
| |
| with open(output_pdf_path, "rb") as f: |
| st.download_button( |
| label="Download Editable PDF", |
| data=f, |
| file_name="editable_output.pdf", |
| mime="application/pdf" |
| ) |
|
|
| |
| os.remove(output_pdf_path) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|