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)}")