File size: 2,657 Bytes
f5d8698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d359b2e
f5d8698
 
 
 
 
d359b2e
 
 
 
f5d8698
 
 
 
aea88e6
 
 
 
846cbd4
52832c3
a32ec05
aea88e6
a32ec05
f5d8698
 
 
 
 
 
 
 
 
77dd209
f5d8698
 
846cbd4
d359b2e
aea88e6
77dd209
f5d8698
 
 
 
 
 
aea88e6
 
f5d8698
 
 
 
 
 
aea88e6
f5d8698
 
 
aea88e6
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
68
69
70
71
72
73
74
75
76
77
78
import os
import gradio as gr
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, error_message=None):
    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")
        if error_message:
            log_file.write(f"Error: {error_message}\n")
        log_file.write("\n")

# Gradio interface function
def process_pdf(file, width, height):
    try:
        # Check if file is uploaded
        if not file:
            raise ValueError("No file uploaded.")

        # Save the uploaded file to disk
        input_pdf = os.path.join(INPUT_FOLDER, os.path.basename(file.name))
        with open(input_pdf, "wb") as f:
            f.write(file.read())

        # 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)

        # Return paths to the output files for download
        return SVG_OUTPUT, PDF_OUTPUT
    except Exception as e:
        # Log the error
        log_conversion("Failed", "N/A", "N/A", "N/A", str(e))
        # Raise an error to be shown in Gradio UI
        return None, None

# Gradio interface
iface = gr.Interface(
    fn=process_pdf,
    inputs=[
        gr.File(label="Upload your PDF file", file_types=[".pdf"]),
        gr.Number(label="Width (in inches)", value=8.0, precision=1),
        gr.Number(label="Height (in inches)", value=11.0, precision=1),
    ],
    outputs=[
        gr.File(label="Download SVG"),
        gr.File(label="Download PDF"),
    ],
    title="PDF to Editable SVG and PDF Converter",
    description="Upload a PDF file, specify output dimensions (in inches), and convert it to editable SVG and PDF files.",
)

if __name__ == "__main__":
    iface.launch(debug=True)