karthikmn commited on
Commit
4b26bdf
·
verified ·
1 Parent(s): 81907e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -83
app.py CHANGED
@@ -1,17 +1,17 @@
1
  import os
2
- import datetime
3
- from PIL import Image
4
- from fpdf import FPDF
5
  import gradio as gr
6
- from PyPDF2 import PdfReader, PdfWriter
 
 
 
 
7
 
8
  # Folder paths
9
  INPUT_FOLDER = "./input/"
10
  OUTPUT_FOLDER = "./output/"
11
  LOG_FOLDER = "./logs/"
12
- PROCESSED_IMAGE = os.path.join(OUTPUT_FOLDER, "processed_image.png")
13
- OUTPUT_PDF = os.path.join(OUTPUT_FOLDER, "processed_file.pdf")
14
- LOG_FILE = os.path.join(LOG_FOLDER, "conversion_log.txt")
15
 
16
  # Ensure required folders exist
17
  os.makedirs(INPUT_FOLDER, exist_ok=True)
@@ -19,89 +19,62 @@ os.makedirs(OUTPUT_FOLDER, exist_ok=True)
19
  os.makedirs(LOG_FOLDER, exist_ok=True)
20
 
21
  # Function to log conversion steps
22
- def log_conversion(status, input_file, output_file):
23
- with open(LOG_FILE, "a") as log_file:
 
24
  log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
25
- log_file.write(f"Input File: {input_file}\n")
26
- log_file.write(f"Output File: {output_file}\n\n")
 
27
 
28
- # Processing function for PDF
29
- def process_pdf(uploaded_pdf, width_inches, height_inches, dpi=300):
30
  try:
31
- # Save the uploaded PDF temporarily
32
- input_pdf_path = os.path.join(INPUT_FOLDER, "uploaded_file.pdf")
33
- with open(input_pdf_path, "wb") as f:
34
- f.write(uploaded_pdf.read())
35
-
36
- # Read the PDF
37
- reader = PdfReader(input_pdf_path)
38
- writer = PdfWriter()
39
 
40
- for page_num, page in enumerate(reader.pages):
41
- # Extract the image from the PDF page
42
- if "/XObject" in page["/Resources"]:
43
- xObject = page["/Resources"]["/XObject"].get_object()
44
- for obj in xObject:
45
- if xObject[obj]["/Subtype"] == "/Image":
46
- size = (xObject[obj]["/Width"], xObject[obj]["/Height"])
47
- data = xObject[obj].get_data()
48
- img = Image.frombytes("RGB", size, data)
49
 
50
- # Convert the image to grayscale and resize
51
- grayscale_image = img.convert("L")
52
- width_pixels = int(width_inches * dpi)
53
- height_pixels = int(height_inches * dpi)
54
- resized_image = grayscale_image.resize((width_pixels, height_pixels))
55
 
56
- # Save processed image temporarily
57
- processed_image_path = os.path.join(OUTPUT_FOLDER, f"processed_page_{page_num + 1}.png")
58
- resized_image.save(processed_image_path)
59
 
60
- # Add the processed image to the new PDF
61
- pdf = FPDF(unit="in", format=(width_inches, height_inches))
62
- pdf.add_page()
63
- pdf.image(processed_image_path, x=0, y=0, w=width_inches, h=height_inches)
64
 
65
- # Save the final PDF
66
- pdf.output(OUTPUT_PDF)
67
-
68
- # Log success
69
- log_conversion("Success", input_pdf_path, OUTPUT_PDF)
70
-
71
- # Return processed PDF and log file
72
- return OUTPUT_PDF, LOG_FILE
73
 
74
  except Exception as e:
75
- log_conversion("Failed", "Uploaded PDF", "Processing Error")
76
- return None, f"Error: {str(e)}"
77
-
78
- # Gradio Interface
79
- def gradio_app():
80
- with gr.Blocks() as demo:
81
- gr.Markdown("# PDF Grayscale Converter and Resizer")
82
-
83
- with gr.Row():
84
- pdf_input = gr.File(type="file", label="Upload PDF File")
85
- with gr.Column():
86
- width_input = gr.Number(value=8.5, label="Width (in inches)")
87
- height_input = gr.Number(value=11.0, label="Height (in inches)")
88
-
89
- process_button = gr.Button("Process PDF")
90
-
91
- with gr.Row():
92
- pdf_output = gr.File(label="Download Processed PDF")
93
- log_file_output = gr.File(label="Download Log File")
94
-
95
- # Define the button click functionality
96
- process_button.click(
97
- process_pdf,
98
- inputs=[pdf_input, width_input, height_input],
99
- outputs=[pdf_output, log_file_output],
100
- )
101
-
102
- return demo
103
-
104
- # Launch the Gradio app
105
- if __name__ == "__main__":
106
- app = gradio_app()
107
- app.launch()
 
1
  import os
 
 
 
2
  import gradio as gr
3
+ from pdf_to_svg import convert_pdf_to_svg
4
+ from svg_editor import apply_transformations
5
+ from svg_to_pdf import convert_svg_to_pdf
6
+ import datetime
7
+ import tempfile
8
 
9
  # Folder paths
10
  INPUT_FOLDER = "./input/"
11
  OUTPUT_FOLDER = "./output/"
12
  LOG_FOLDER = "./logs/"
13
+ SVG_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.svg")
14
+ PDF_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.pdf")
 
15
 
16
  # Ensure required folders exist
17
  os.makedirs(INPUT_FOLDER, exist_ok=True)
 
19
  os.makedirs(LOG_FOLDER, exist_ok=True)
20
 
21
  # Function to log conversion steps
22
+ def log_conversion(status, input_pdf, output_svg, output_pdf):
23
+ log_path = os.path.join(LOG_FOLDER, "conversion_log.txt")
24
+ with open(log_path, "a") as log_file:
25
  log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
26
+ log_file.write(f"Input PDF: {input_pdf}\n")
27
+ log_file.write(f"Output SVG: {output_svg}\n")
28
+ log_file.write(f"Output PDF: {output_pdf}\n\n")
29
 
30
+ # Gradio interface function
31
+ def process_pdf(uploaded_file, brightness, text_scale, line_scale, width, height):
32
  try:
33
+ # Save uploaded file
34
+ input_pdf = os.path.join(INPUT_FOLDER, "uploaded_file.pdf")
35
+ with open(input_pdf, "wb") as f:
36
+ f.write(uploaded_file.read())
 
 
 
 
37
 
38
+ # Step 1: Convert PDF to SVG
39
+ convert_pdf_to_svg(input_pdf, SVG_OUTPUT, width, height)
 
 
 
 
 
 
 
40
 
41
+ # Step 2: Apply transformations to the SVG
42
+ apply_transformations(SVG_OUTPUT, brightness, text_scale, line_scale)
 
 
 
43
 
44
+ # Step 3: Convert SVG to PDF
45
+ convert_svg_to_pdf(SVG_OUTPUT, PDF_OUTPUT)
 
46
 
47
+ # Step 4: Log the process
48
+ log_conversion("Success", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
 
 
49
 
50
+ # Return output files for download
51
+ return (SVG_OUTPUT, PDF_OUTPUT)
 
 
 
 
 
 
52
 
53
  except Exception as e:
54
+ log_conversion("Failed", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
55
+ return str(e)
56
+
57
+ # Gradio interface setup
58
+ inputs = [
59
+ gr.File(label="Upload your PDF file", type="file"),
60
+ gr.Slider(minimum=0.5, maximum=5.0, step=0.5, default=1.0, label="Brightness (0.5 to 5.0)"),
61
+ gr.Slider(minimum=0.5, maximum=2.0, step=0.1, default=1.0, label="Text Size Scale (0.5 to 2.0)"),
62
+ gr.Slider(minimum=0.5, maximum=5.0, step=0.5, default=1.0, label="Line Thickness Scale (0.5 to 5.0)"),
63
+ gr.Number(value=8.0, label="Width (in inches)", precision=0.1),
64
+ gr.Number(value=11.0, label="Height (in inches)", precision=0.1)
65
+ ]
66
+
67
+ outputs = [
68
+ gr.File(label="Download SVG", type="file"),
69
+ gr.File(label="Download PDF", type="file")
70
+ ]
71
+
72
+ # Launch Gradio interface
73
+ gr.Interface(
74
+ fn=process_pdf,
75
+ inputs=inputs,
76
+ outputs=outputs,
77
+ live=True,
78
+ title="PDF to Editable SVG and PDF Converter",
79
+ description="Upload a PDF to convert it to both an editable SVG and a PDF with transformations applied."
80
+ ).launch()