File size: 1,179 Bytes
f499c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pdfplumber
import io


def extract_text_with_links(file_bytes: bytes) -> str:
    """
    Extracts text and explicitly recovers embedded URLs from the PDF structure.
    """
    full_content = []
    try:
        with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
            for page in pdf.pages:
                # 1. Get the standard text
                page_text = page.extract_text() or ""

                # 2. Extract Hyperlinks (Annotations)
                links = page.hyperlinks
                link_descriptions = []

                if links:
                    for link in links:
                        # Extract the destination URL
                        uri = link.get('uri')
                        if uri:
                            link_descriptions.append(f"[Link Found: {uri}]")

                # 3. Combine text and found links for the AI to process
                combined_page = page_text + "\n" + "\n".join(link_descriptions)
                full_content.append(combined_page)

        return "\n--- Page Boundary ---\n".join(full_content)
    except Exception as e:
        raise ValueError(f"Failed to recover links from PDF: {str(e)}")