Extract text for specific PDF pages (1-indexed). Prefer cached pages, fallback to PDF.
(doc_info: dict, page_nums: list[int])
| 34 | |
| 35 | |
| 36 | def _get_pdf_page_content(doc_info: dict, page_nums: list[int]) -> list[dict]: |
| 37 | """Extract text for specific PDF pages (1-indexed). Prefer cached pages, fallback to PDF.""" |
| 38 | cached_pages = doc_info.get('pages') |
| 39 | if cached_pages: |
| 40 | page_map = {p['page']: p['content'] for p in cached_pages} |
| 41 | return [ |
| 42 | {'page': p, 'content': page_map[p]} |
| 43 | for p in page_nums if p in page_map |
| 44 | ] |
| 45 | path = doc_info['path'] |
| 46 | with open(path, 'rb') as f: |
| 47 | pdf_reader = PyPDF2.PdfReader(f) |
| 48 | total = len(pdf_reader.pages) |
| 49 | valid_pages = [p for p in page_nums if 1 <= p <= total] |
| 50 | return [ |
| 51 | {'page': p, 'content': pdf_reader.pages[p - 1].extract_text() or ''} |
| 52 | for p in valid_pages |
| 53 | ] |
| 54 | |
| 55 | |
| 56 | def _get_md_page_content(doc_info: dict, page_nums: list[int]) -> list[dict]: |