Delete core/pdf_utils.py
Browse files- core/pdf_utils.py +0 -30
core/pdf_utils.py
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
import io, shutil
|
| 3 |
-
from typing import List
|
| 4 |
-
from pdf2image import convert_from_path
|
| 5 |
-
import pdfplumber
|
| 6 |
-
|
| 7 |
-
def has_poppler() -> bool:
|
| 8 |
-
return bool(shutil.which("pdftoppm")) and bool(shutil.which("pdftocairo"))
|
| 9 |
-
|
| 10 |
-
def pdf_to_images(pdf_path: str, dpi: int = 220, max_pages: int = 6) -> List[bytes]:
|
| 11 |
-
pages = convert_from_path(pdf_path, dpi=dpi, fmt="png")
|
| 12 |
-
out: List[bytes] = []
|
| 13 |
-
for i, p in enumerate(pages):
|
| 14 |
-
if i >= max_pages:
|
| 15 |
-
break
|
| 16 |
-
buf = io.BytesIO()
|
| 17 |
-
p.save(buf, format="PNG")
|
| 18 |
-
out.append(buf.getvalue())
|
| 19 |
-
return out
|
| 20 |
-
|
| 21 |
-
def pdf_to_text(pdf_path: str, max_chars: int = 15000) -> str:
|
| 22 |
-
chunks: List[str] = []
|
| 23 |
-
with pdfplumber.open(pdf_path) as pdf:
|
| 24 |
-
for i, page in enumerate(pdf.pages):
|
| 25 |
-
t = (page.extract_text() or "").strip()
|
| 26 |
-
if t:
|
| 27 |
-
chunks.append(f"[page {i+1}]\n{t}")
|
| 28 |
-
if sum(len(c) for c in chunks) > max_chars:
|
| 29 |
-
break
|
| 30 |
-
return "\n\n".join(chunks)[:max_chars]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|