Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from svg_editor import apply_transformations # Ensure this function exists
|
| 5 |
-
from svg_to_pdf import convert_svg_to_pdf # Ensure this function exists
|
| 6 |
import datetime
|
|
|
|
| 7 |
|
| 8 |
# Folder paths
|
| 9 |
INPUT_FOLDER = "./input/"
|
|
@@ -26,6 +25,16 @@ def log_conversion(status, input_pdf, output_svg, output_pdf):
|
|
| 26 |
log_file.write(f"Output SVG: {output_svg}\n")
|
| 27 |
log_file.write(f"Output PDF: {output_pdf}\n\n")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Gradio interface function
|
| 30 |
def process_pdf(uploaded_file, brightness, text_scale, line_scale, width, height):
|
| 31 |
try:
|
|
@@ -35,23 +44,26 @@ def process_pdf(uploaded_file, brightness, text_scale, line_scale, width, height
|
|
| 35 |
f.write(uploaded_file.read())
|
| 36 |
|
| 37 |
# Step 1: Convert PDF to SVG
|
| 38 |
-
convert_pdf_to_svg(input_pdf, SVG_OUTPUT, width, height)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
log_conversion("Failed", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
| 54 |
-
return
|
| 55 |
|
| 56 |
# Gradio interface setup
|
| 57 |
inputs = [
|
|
@@ -76,4 +88,4 @@ gr.Interface(
|
|
| 76 |
live=True,
|
| 77 |
title="PDF to Editable SVG and PDF Converter",
|
| 78 |
description="Upload a PDF to convert it to both an editable SVG and a PDF with transformations applied."
|
| 79 |
-
).launch(
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import cairosvg
|
|
|
|
|
|
|
| 4 |
import datetime
|
| 5 |
+
import tempfile
|
| 6 |
|
| 7 |
# Folder paths
|
| 8 |
INPUT_FOLDER = "./input/"
|
|
|
|
| 25 |
log_file.write(f"Output SVG: {output_svg}\n")
|
| 26 |
log_file.write(f"Output PDF: {output_pdf}\n\n")
|
| 27 |
|
| 28 |
+
# Function to convert PDF to SVG using cairosvg
|
| 29 |
+
def convert_pdf_to_svg(input_pdf, output_svg, width, height):
|
| 30 |
+
try:
|
| 31 |
+
# Use cairosvg to convert the PDF to SVG
|
| 32 |
+
cairosvg.svg2pdf(url=input_pdf, write_to=output_svg)
|
| 33 |
+
return True
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error converting PDF to SVG: {e}")
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
# Gradio interface function
|
| 39 |
def process_pdf(uploaded_file, brightness, text_scale, line_scale, width, height):
|
| 40 |
try:
|
|
|
|
| 44 |
f.write(uploaded_file.read())
|
| 45 |
|
| 46 |
# Step 1: Convert PDF to SVG
|
| 47 |
+
if convert_pdf_to_svg(input_pdf, SVG_OUTPUT, width, height):
|
| 48 |
+
# Step 2: Apply transformations to the SVG (this can be customized)
|
| 49 |
+
# Assuming `apply_transformations` function exists for SVG processing
|
| 50 |
+
|
| 51 |
+
apply_transformations(SVG_OUTPUT, brightness, text_scale, line_scale)
|
| 52 |
|
| 53 |
+
# Step 3: Convert SVG to PDF (assuming `convert_svg_to_pdf` exists)
|
| 54 |
+
convert_svg_to_pdf(SVG_OUTPUT, PDF_OUTPUT)
|
| 55 |
|
| 56 |
+
# Step 4: Log the process
|
| 57 |
+
log_conversion("Success", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
| 58 |
|
| 59 |
+
# Return output files for download
|
| 60 |
+
return (SVG_OUTPUT, PDF_OUTPUT)
|
| 61 |
+
else:
|
| 62 |
+
return "Error converting PDF to SVG."
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
log_conversion("Failed", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
| 66 |
+
return str(e)
|
| 67 |
|
| 68 |
# Gradio interface setup
|
| 69 |
inputs = [
|
|
|
|
| 88 |
live=True,
|
| 89 |
title="PDF to Editable SVG and PDF Converter",
|
| 90 |
description="Upload a PDF to convert it to both an editable SVG and a PDF with transformations applied."
|
| 91 |
+
).launch()
|