AkashKumarave commited on
Commit
b6dcd96
·
verified ·
1 Parent(s): fde25b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,17 +1,28 @@
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
 
7
  app = FastAPI()
8
 
 
 
 
 
 
 
 
 
 
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
@@ -30,18 +41,14 @@ async def convert_pdf(file: bytes = File(...)):
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"],
@@ -50,20 +57,20 @@ async def convert_pdf(file: bytes = File(...)):
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:
69
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
1
  from fastapi import FastAPI, File, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
  from fastapi.responses import JSONResponse
4
  import pdfplumber
5
  from io import BytesIO
6
  import base64
7
+ import uvicorn
8
 
9
  app = FastAPI()
10
 
11
+ # Add CORS middleware
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"], # Adjust for production
15
+ allow_credentials=True,
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
  @app.post("/api/convert")
21
  async def convert_pdf(file: bytes = File(...)):
22
  try:
23
  # Parse PDF with pdfplumber
24
  with pdfplumber.open(BytesIO(file)) as pdf:
25
+ page = pdf.pages[0] # First page for simplicity
26
  width, height = page.width, page.height
27
 
28
  # Initialize result
 
41
  "x": char["x0"],
42
  "y": char["y0"],
43
  "font_family": char["fontname"].split("+")[-1] or "Arial",
44
+ "font_style": "Regular",
45
  "font_size": char["size"],
46
+ "color": {"r": 0, "g": 0, "b": 0} # Simplified: enhance for color extraction
 
 
 
 
47
  })
48
 
49
  # Extract images
50
  for img in page.images:
51
+ img_data = img["stream"].get_data()
52
  result["images"].append({
53
  "data": base64.b64encode(img_data).decode('utf-8'),
54
  "x": img["x0"],
 
57
  "height": img["height"]
58
  })
59
 
60
+ # Extract shapes
61
  for curve in page.curves:
62
+ path = " ".join([f"M {p['x']},{p['y']}" for p in curve["points"]])
63
  result["shapes"].append({
64
  "path": path,
65
  "x": curve["x0"],
66
  "y": curve["y0"],
67
+ "color": {"r": 0, "g": 0, "b": 0}
 
 
 
 
68
  })
69
 
70
  return JSONResponse(content=result)
71
  except Exception as e:
72
+ raise HTTPException(status_code=500, detail=str(e))
73
+
74
+ # Run uvicorn server
75
+ if __name__ == "__main__":
76
+ uvicorn.run(app, host="0.0.0.0", port=7860)