SathvikGanta's picture
Create app.py
e89d11f verified
raw
history blame
2.68 kB
import os
import streamlit as st
from pdf_to_svg import convert_pdf_to_svg
from svg_editor import apply_transformations
import datetime
# Folder paths
INPUT_FOLDER = "./input/"
OUTPUT_FOLDER = "./output/"
LOG_FOLDER = "./logs/"
SVG_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.svg")
PDF_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.pdf")
# Ensure required folders exist
os.makedirs(INPUT_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
os.makedirs(LOG_FOLDER, exist_ok=True)
# Function to log conversion steps
def log_conversion(status, input_pdf, output_svg):
log_path = os.path.join(LOG_FOLDER, "conversion_log.txt")
with open(log_path, "a") as log_file:
log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
log_file.write(f"Input PDF: {input_pdf}\n")
log_file.write(f"Output SVG: {output_svg}\n\n")
# Streamlit app interface
def main():
st.title("PDF to Editable SVG Converter")
# File upload
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
if uploaded_file:
input_pdf = os.path.join(INPUT_FOLDER, "uploaded_file.pdf")
with open(input_pdf, "wb") as f:
f.write(uploaded_file.read())
st.success(f"File uploaded: {uploaded_file.name}")
# User controls for adjustments
brightness = st.slider("Brightness (0.5 to 5.0)", 0.5, 5.0, 1.0, step=0.5)
text_scale = st.slider("Text Size Scale (0.5 to 2.0)", 0.5, 2.0, 1.0, step=0.1)
line_scale = st.slider("Line Thickness Scale (0.5 to 5.0)", 0.5, 5.0, 1.0, step=0.5)
width = st.number_input("Width (in inches)", value=8.0, step=0.1)
height = st.number_input("Height (in inches)", value=11.0, step=0.1)
# Process the file
if st.button("Process PDF"):
try:
# Step 1: Convert PDF to SVG
convert_pdf_to_svg(input_pdf, SVG_OUTPUT, width, height)
# Step 2: Apply transformations to the SVG
apply_transformations(SVG_OUTPUT, brightness, text_scale, line_scale)
# Step 3: Log the process
log_conversion("Success", input_pdf, SVG_OUTPUT)
# Provide download link
st.success("File processed successfully!")
st.download_button("Download SVG", open(SVG_OUTPUT, "rb"), file_name="processed_diagram.svg")
st.info("You can now import this SVG into CorelDRAW.")
except Exception as e:
log_conversion("Failed", input_pdf, SVG_OUTPUT)
st.error(f"Error processing file: {e}")
if __name__ == "__main__":
main()