File size: 1,384 Bytes
a0746e3
441273a
a0746e3
 
 
 
 
 
 
 
 
 
 
 
 
 
85296a5
441273a
a0746e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85296a5
a0746e3
 
85296a5
1
2
3
4
5
6
7
8
9
10
11
12
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
# pdf_reader.py
import io
from typing import Optional

# Бірнеше кітапхана арқылы оқуды қолдаймыз — ең алдымен PyPDF2, егер қажет болса pdfminer.six
try:
    from PyPDF2 import PdfReader
    _has_pypdf2 = True
except Exception:
    _has_pypdf2 = False

try:
    from pdfminer.high_level import extract_text as pdfminer_extract_text
    _has_pdfminer = True
except Exception:
    _has_pdfminer = False

def extract_text_from_pdf(pdf_bytes: bytes) -> str:
    """
    PDF-тен мәтін алу. Бірнеше әдісті қолданып көреді.
    """
    # 1) PyPDF2
    if _has_pypdf2:
        try:
            reader = PdfReader(io.BytesIO(pdf_bytes))
            text = ""
            for page in reader.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + "\n"
            if text.strip():
                return text
        except Exception:
            pass

    # 2) pdfminer.six (әдетте күрделі PDF-тарға жақсы)
    if _has_pdfminer:
        try:
            text = pdfminer_extract_text(io.BytesIO(pdf_bytes))
            if text and text.strip():
                return text
        except Exception:
            pass

    # Егер ештеңе шықпаса — бос қайтарың
    return ""