Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
-
|
| 4 |
from io import BytesIO
|
| 5 |
import base64
|
| 6 |
|
|
@@ -9,49 +9,60 @@ app = FastAPI()
|
|
| 9 |
@app.post("/api/convert")
|
| 10 |
async def convert_pdf(file: bytes = File(...)):
|
| 11 |
try:
|
| 12 |
-
# Parse PDF
|
| 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 |
return JSONResponse(content=result)
|
| 57 |
except Exception as e:
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
+
import pdfplumber
|
| 4 |
from io import BytesIO
|
| 5 |
import base64
|
| 6 |
|
|
|
|
| 9 |
@app.post("/api/convert")
|
| 10 |
async def convert_pdf(file: bytes = File(...)):
|
| 11 |
try:
|
| 12 |
+
# Parse PDF with pdfplumber
|
| 13 |
+
with pdfplumber.open(BytesIO(file)) as pdf:
|
| 14 |
+
page = pdf.pages[0] # Process first page for simplicity
|
| 15 |
+
width, height = page.width, page.height
|
| 16 |
|
| 17 |
+
# Initialize result
|
| 18 |
+
result = {
|
| 19 |
+
"width": width,
|
| 20 |
+
"height": height,
|
| 21 |
+
"texts": [],
|
| 22 |
+
"images": [],
|
| 23 |
+
"shapes": []
|
| 24 |
+
}
|
| 25 |
|
| 26 |
+
# Extract text
|
| 27 |
+
for char in page.chars:
|
| 28 |
+
result["texts"].append({
|
| 29 |
+
"content": char["text"],
|
| 30 |
+
"x": char["x0"],
|
| 31 |
+
"y": char["y0"],
|
| 32 |
+
"font_family": char["fontname"].split("+")[-1] or "Arial",
|
| 33 |
+
"font_style": "Regular", # pdfplumber doesn't provide style directly
|
| 34 |
+
"font_size": char["size"],
|
| 35 |
+
"color": {
|
| 36 |
+
"r": 0, # Simplified: assume black text (enhance with actual color extraction if needed)
|
| 37 |
+
"g": 0,
|
| 38 |
+
"b": 0
|
| 39 |
+
}
|
| 40 |
+
})
|
| 41 |
|
| 42 |
+
# Extract images
|
| 43 |
+
for img in page.images:
|
| 44 |
+
img_data = img["stream"].get_data() # Raw image data
|
| 45 |
+
result["images"].append({
|
| 46 |
+
"data": base64.b64encode(img_data).decode('utf-8'),
|
| 47 |
+
"x": img["x0"],
|
| 48 |
+
"y": img["y0"],
|
| 49 |
+
"width": img["width"],
|
| 50 |
+
"height": img["height"]
|
| 51 |
+
})
|
| 52 |
|
| 53 |
+
# Extract shapes (basic lines/curves)
|
| 54 |
+
for curve in page.curves:
|
| 55 |
+
path = " ".join([f"M {p['x']},{p['y']}" for p in curve["points"]]) # Simplified SVG path
|
| 56 |
+
result["shapes"].append({
|
| 57 |
+
"path": path,
|
| 58 |
+
"x": curve["x0"],
|
| 59 |
+
"y": curve["y0"],
|
| 60 |
+
"color": {
|
| 61 |
+
"r": 0,
|
| 62 |
+
"g": 0,
|
| 63 |
+
"b": 0
|
| 64 |
+
}
|
| 65 |
+
})
|
| 66 |
|
| 67 |
return JSONResponse(content=result)
|
| 68 |
except Exception as e:
|