File size: 2,573 Bytes
e89d11f
 
 
ed48751
e89d11f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed48751
e89d11f
 
 
 
ed48751
 
e89d11f
 
 
0b2053b
e89d11f
 
 
 
 
 
 
 
 
eca402f
e89d11f
 
 
 
 
 
 
 
 
eca402f
ed48751
 
eca402f
ed48751
e89d11f
ed48751
e89d11f
 
ed48751
 
e89d11f
ed48751
e89d11f
 
 
ed48751
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import streamlit as st
from pdf_to_svg import convert_pdf_to_svg
from svg_to_pdf import convert_svg_to_pdf
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, output_pdf):
    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")
        log_file.write(f"Output PDF: {output_pdf}\n\n")

# Streamlit app interface
def main():
    st.title("PDF Dimension Enhancer")

    # 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 dimensions
        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: Convert SVG to PDF
                convert_svg_to_pdf(SVG_OUTPUT, PDF_OUTPUT)

                # Step 3: Log the process
                log_conversion("Success", input_pdf, SVG_OUTPUT, PDF_OUTPUT)

                # Provide download links
                st.success("File processed successfully!")
                st.download_button("Download SVG", open(SVG_OUTPUT, "rb"), file_name="processed_diagram.svg")
                st.download_button("Download PDF", open(PDF_OUTPUT, "rb"), file_name="processed_diagram.pdf")
                st.info("Both SVG and PDF outputs are ready for download.")
            except Exception as e:
                log_conversion("Failed", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
                st.error(f"Error processing file: {e}")

if __name__ == "__main__":
    main()