Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,57 @@
|
|
| 1 |
import fitz # PyMuPDF for PDF manipulation
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import tempfile
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
# Open the PDF and load the first page
|
| 10 |
pdf_document = fitz.open(pdf_file.name)
|
| 11 |
page = pdf_document.load_page(0)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
svg_data = page.get_svg_image(matrix=fitz.Matrix(8, 8)) # 8x
|
| 15 |
|
| 16 |
-
#
|
| 17 |
if not svg_data:
|
| 18 |
print("Error: SVG data extraction failed.")
|
| 19 |
return None
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
temp_svg_path = tempfile.mktemp(suffix=".svg", prefix="enhanced_diagram_")
|
| 23 |
-
|
| 24 |
-
# Apply thickness adjustment directly if needed
|
| 25 |
if thickness_scale != 1.0:
|
| 26 |
-
import re
|
| 27 |
svg_data = re.sub(
|
| 28 |
r'stroke-width="([\d.]+)"',
|
| 29 |
lambda match: f'stroke-width="{float(match.group(1)) * thickness_scale}"',
|
| 30 |
svg_data
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
with open(temp_svg_path, 'w') as svg_file:
|
| 34 |
svg_file.write(svg_data)
|
| 35 |
|
| 36 |
-
return temp_svg_path # Return
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Error during SVG conversion: {str(e)}")
|
| 39 |
return None
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
def adjust_and_convert_pdf(pdf_file, thickness_scale=1.0):
|
| 43 |
-
svg_path = pdf_to_svg(pdf_file, thickness_scale=thickness_scale)
|
| 44 |
|
| 45 |
# Handle cases where the SVG path is None
|
| 46 |
if svg_path is None:
|
|
@@ -48,15 +59,18 @@ def adjust_and_convert_pdf(pdf_file, thickness_scale=1.0):
|
|
| 48 |
|
| 49 |
return svg_path # Return the SVG file path for download
|
| 50 |
|
|
|
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=adjust_and_convert_pdf,
|
| 53 |
inputs=[
|
| 54 |
gr.File(label="Upload PDF"),
|
|
|
|
|
|
|
| 55 |
gr.Slider(minimum=1, maximum=3, step=0.1, value=1.0, label="Thickness Scale")
|
| 56 |
],
|
| 57 |
outputs=gr.File(label="Editable SVG"),
|
| 58 |
-
title="
|
| 59 |
-
description="Upload a PDF
|
| 60 |
)
|
| 61 |
|
| 62 |
# Launch the interface on Hugging Face Spaces
|
|
|
|
| 1 |
import fitz # PyMuPDF for PDF manipulation
|
| 2 |
+
import cairosvg # For precise SVG rendering
|
| 3 |
import gradio as gr
|
| 4 |
import tempfile
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
+
# Convert inches to pixels for dimension scaling
|
| 8 |
+
def inches_to_pixels(inches, dpi=300):
|
| 9 |
+
return int(inches * dpi)
|
| 10 |
+
|
| 11 |
+
# Convert PDF to SVG with specified dimensions and thickness scaling
|
| 12 |
+
def pdf_to_svg(pdf_file, width_in, height_in, thickness_scale=1.0):
|
| 13 |
try:
|
| 14 |
# Open the PDF and load the first page
|
| 15 |
pdf_document = fitz.open(pdf_file.name)
|
| 16 |
page = pdf_document.load_page(0)
|
| 17 |
|
| 18 |
+
# Render PDF page as SVG data using a high-resolution matrix
|
| 19 |
+
svg_data = page.get_svg_image(matrix=fitz.Matrix(8, 8)) # 8x for high fidelity
|
| 20 |
|
| 21 |
+
# Ensure we have valid SVG data
|
| 22 |
if not svg_data:
|
| 23 |
print("Error: SVG data extraction failed.")
|
| 24 |
return None
|
| 25 |
|
| 26 |
+
# Adjust stroke width in the SVG data if thickness scaling is applied
|
|
|
|
|
|
|
|
|
|
| 27 |
if thickness_scale != 1.0:
|
|
|
|
| 28 |
svg_data = re.sub(
|
| 29 |
r'stroke-width="([\d.]+)"',
|
| 30 |
lambda match: f'stroke-width="{float(match.group(1)) * thickness_scale}"',
|
| 31 |
svg_data
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Convert width and height to pixels at 300 DPI for SVG scaling
|
| 35 |
+
width_px = inches_to_pixels(width_in)
|
| 36 |
+
height_px = inches_to_pixels(height_in)
|
| 37 |
+
|
| 38 |
+
# Embed dimensions into the SVG
|
| 39 |
+
svg_data = re.sub(r'width="([\d.]+)pt"', f'width="{width_px}px"', svg_data)
|
| 40 |
+
svg_data = re.sub(r'height="([\d.]+)pt"', f'height="{height_px}px"', svg_data)
|
| 41 |
+
|
| 42 |
+
# Save SVG data to a temporary file
|
| 43 |
+
temp_svg_path = tempfile.mktemp(suffix=".svg", prefix="enhanced_diagram_")
|
| 44 |
with open(temp_svg_path, 'w') as svg_file:
|
| 45 |
svg_file.write(svg_data)
|
| 46 |
|
| 47 |
+
return temp_svg_path # Return path of the saved SVG
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Error during SVG conversion: {str(e)}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
+
# Gradio Interface with width and height inputs
|
| 53 |
+
def adjust_and_convert_pdf(pdf_file, width_in, height_in, thickness_scale=1.0):
|
| 54 |
+
svg_path = pdf_to_svg(pdf_file, width_in, height_in, thickness_scale=thickness_scale)
|
| 55 |
|
| 56 |
# Handle cases where the SVG path is None
|
| 57 |
if svg_path is None:
|
|
|
|
| 59 |
|
| 60 |
return svg_path # Return the SVG file path for download
|
| 61 |
|
| 62 |
+
# Gradio interface with width, height, and thickness scale inputs
|
| 63 |
iface = gr.Interface(
|
| 64 |
fn=adjust_and_convert_pdf,
|
| 65 |
inputs=[
|
| 66 |
gr.File(label="Upload PDF"),
|
| 67 |
+
gr.Number(label="Width (inches)", value=5.0),
|
| 68 |
+
gr.Number(label="Height (inches)", value=4.0),
|
| 69 |
gr.Slider(minimum=1, maximum=3, step=0.1, value=1.0, label="Thickness Scale")
|
| 70 |
],
|
| 71 |
outputs=gr.File(label="Editable SVG"),
|
| 72 |
+
title="PDF to Fully Editable SVG Converter for CorelDRAW",
|
| 73 |
+
description="Upload a PDF to convert it to a fully editable SVG with specific width and height dimensions in inches. Ideal for CorelDRAW editing."
|
| 74 |
)
|
| 75 |
|
| 76 |
# Launch the interface on Hugging Face Spaces
|