thebassey01 commited on
Commit
fb1bf11
·
verified ·
1 Parent(s): 941941c

Update app/pdf_utils.py

Browse files
Files changed (1) hide show
  1. app/pdf_utils.py +28 -3
app/pdf_utils.py CHANGED
@@ -1,9 +1,12 @@
 
 
 
1
  import fitz # PyMuPDF
 
2
  from PIL import Image
3
- import io
4
 
5
 
6
- def pdf_to_images(pdf_bytes: bytes) -> list[Image.Image]:
7
  doc = fitz.open(stream=pdf_bytes, filetype="pdf")
8
  images = []
9
 
@@ -12,4 +15,26 @@ def pdf_to_images(pdf_bytes: bytes) -> list[Image.Image]:
12
  img = Image.open(io.BytesIO(pix.tobytes("png"))).convert("RGB")
13
  images.append(img)
14
 
15
- return images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ from typing import List, Dict
3
+
4
  import fitz # PyMuPDF
5
+ import pdfplumber
6
  from PIL import Image
 
7
 
8
 
9
+ def pdf_to_images(pdf_bytes: bytes) -> List[Image.Image]:
10
  doc = fitz.open(stream=pdf_bytes, filetype="pdf")
11
  images = []
12
 
 
15
  img = Image.open(io.BytesIO(pix.tobytes("png"))).convert("RGB")
16
  images.append(img)
17
 
18
+ return images
19
+
20
+
21
+ def extract_text_from_pdf(pdf_bytes: bytes) -> List[Dict]:
22
+ """
23
+ Extract text page-by-page directly from the PDF.
24
+ Returns a list like:
25
+ [
26
+ {"page": 1, "text": "..."},
27
+ {"page": 2, "text": "..."}
28
+ ]
29
+ """
30
+ results: List[Dict] = []
31
+
32
+ with pdfplumber.open(io.BytesIO(pdf_bytes)) as pdf:
33
+ for idx, page in enumerate(pdf.pages, start=1):
34
+ text = page.extract_text() or ""
35
+ results.append({
36
+ "page": idx,
37
+ "text": text
38
+ })
39
+
40
+ return results