Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

11.5 KB
Raw Blame
"""
PDF text extraction service.

Provides efficient PDF text extraction with single-pass processing.
Complements pdf_service.py which handles PDF generation.
"""

import io
import time
from typing import Any, Dict, List

import pdfplumber
from loguru import logger

from local_deep_research.security.filename_sanitizer import sanitize_filename
from local_deep_research.utilities.pdf_extraction_limits import (
    MAX_PDF_EXTRACTED_CHARS,
    MAX_PDF_EXTRACTION_CPU_SECONDS,
    MAX_PDF_EXTRACTION_PAGES,
)
from local_deep_research.utilities.pdf_page_walk import iter_pdfplumber_pages
from local_deep_research.utilities.pdf_stream_release import (
    PdfplumberWalkReleaser,
)
from local_deep_research.utilities.resource_utils import safe_close


class PDFExtractionService:
    """Service for extracting text and metadata from PDF files."""

    @staticmethod
    def extract_text_and_metadata(
        pdf_content: bytes, filename: str
    ) -> Dict[str, Any]:
        """
        Extract text and metadata from PDF in a single pass.

        This method opens the PDF only once and extracts both text content
        and metadata (page count) in the same operation, avoiding the
        performance issue of opening the file multiple times.

        What is bounded (``utilities/pdf_extraction_limits``): text is
        extracted from at most ``MAX_PDF_EXTRACTION_PAGES`` pages, at most
        ``MAX_PDF_EXTRACTED_CHARS`` characters are kept (separators
        included; the page that crosses the ceiling is truncated to the
        budget left), and no new page is started once the calling thread
        has spent ``MAX_PDF_EXTRACTION_CPU_SECONDS`` of its own CPU time
        (``time.thread_time()``) since entry. The page tree is walked
        lazily (``utilities/pdf_page_walk``), so a document declaring more
        pages than the ceiling has at most the ceiling's pages (plus the
        one that shows there are more) built, not all of them. Every page
        the walk reaches is closed once done with, which releases its
        per-page layout caches; what pdfminer decodes and parses for a
        page stays cached on the document after that, so the walk hands
        each page to a releaser (``utilities/pdf_stream_release``) that
        releases those caches once they reach
        ``MAX_PDF_RETAINED_DECODED_BYTES``.

        What is NOT bounded: the budget is checked between pages only,
        so neither ``pdfplumber.open`` (the cross-reference parse) nor a
        single page's ``extract_text()`` can be interrupted, and what one
        page decodes and parses is held until the release after it.

        Args:
            pdf_content: Raw PDF file bytes
            filename: Original filename (for logging)

        Returns:
            Dictionary with keys:
            - 'text': Extracted text content
            - 'pages': Number of pages the walk reached. When the walk
              stops early (a ceiling, or the CPU budget) the pages after
              it are never built, so this is a lower bound and
              'truncated' is True. The page tree's declared ``/Count``
              is deliberately not read: resolving it can loop inside
              pdfminer (a self-referencing object, or an object-stream
              or ``/Length`` reference cycle) after the CPU budget has
              stopped applying
            - 'size': File size in bytes
            - 'filename': Original filename
            - 'truncated': True if a page or character ceiling, or the
              CPU budget, stopped the extraction early (False otherwise, including failures)
            - 'success': Boolean indicating success
            - 'error': Error message if failed (None if successful)

        Raises:
            No exceptions - errors are captured in return dict
        """
        # Defense in depth: re-sanitize even though callers should
        # have sanitized already — guards against new callers that forget
        try:
            filename = sanitize_filename(filename)
        except Exception:
            filename = "unnamed.pdf"

        # Started at entry so the budget covers the whole call, the open
        # included, as in the library download paths. Per-thread CPU time,
        # not wall time: waiting on other requests is not charged.
        deadline = time.thread_time() + MAX_PDF_EXTRACTION_CPU_SECONDS
        try:
            # Not a ``with`` block: ``PDF.close()`` accesses ``PDF.pages``,
            # which builds every page of the document. The stream is an
            # in-memory buffer, so nothing else needs closing; each page
            # the walk reaches is closed below.
            pdf = pdfplumber.open(io.BytesIO(pdf_content))
            text_parts = []
            extracted_chars = 0
            truncated = False
            pages_seen = 0
            releaser = PdfplumberWalkReleaser(pdf)
            # The walk calls releaser.before_page() before building each
            # page, so what building it resolves and caches (its
            # /Resources, /Contents, page-tree nodes) is counted too.
            for page_number, page in enumerate(
                iter_pdfplumber_pages(pdf, releaser)
            ):
                pages_seen = page_number + 1
                # Close every page this loop reaches, on the break
                # paths too: pdfplumber keeps a page's parsed layout
                # objects and text map cached on it until close().
                try:
                    if page_number >= MAX_PDF_EXTRACTION_PAGES:
                        truncated = True
                        logger.warning(
                            f"PDF extraction stopped at page ceiling "
                            f"({MAX_PDF_EXTRACTION_PAGES}) for {filename}"
                        )
                        break
                    if time.thread_time() >= deadline:
                        truncated = True
                        logger.warning(
                            "PDF extraction stopped at the CPU-time "
                            f"ceiling ({MAX_PDF_EXTRACTION_CPU_SECONDS}s) "
                            f"after {page_number} pages for {filename}"
                        )
                        break
                    separator_chars = 1 if text_parts else 0
                    remaining = (
                        MAX_PDF_EXTRACTED_CHARS
                        - extracted_chars
                        - separator_chars
                    )
                    if remaining <= 0:
                        truncated = True
                        logger.warning(
                            f"PDF extraction stopped at character "
                            f"ceiling ({MAX_PDF_EXTRACTED_CHARS}) "
                            f"for {filename}"
                        )
                        break
                    page_text = page.extract_text()
                    if page_text:
                        if len(page_text) > remaining:
                            # Keep the slice that fits rather than
                            # dropping the tripping page whole: a first
                            # page over the ceiling still yields the
                            # budget's text.
                            text_parts.append(page_text[:remaining])
                            extracted_chars += separator_chars + remaining
                            truncated = True
                            logger.warning(
                                f"PDF extraction truncated at character "
                                f"ceiling ({MAX_PDF_EXTRACTED_CHARS}) "
                                f"for {filename}"
                            )
                            break
                        extracted_chars += separator_chars + len(page_text)
                        text_parts.append(page_text)
                finally:
                    # safe_close: a failing close() must not mask the
                    # page's own exception.
                    safe_close(page, "pdfplumber page")
                    # close() leaves what pdfminer decoded and parsed
                    # for the page cached on the document; release it
                    # once it reaches MAX_PDF_RETAINED_DECODED_BYTES.
                    releaser.after_page()

            # A lower bound when truncated: the pages after the stop
            # were never built, and /Count is not consulted (see the
            # docstring) because no reference is resolved after the walk.
            page_count = pages_seen

            # Combine all text
            full_text = "\n".join(text_parts)

            # Check if any text was extracted
            if not full_text.strip():
                logger.warning(f"No extractable text found in {filename}")
                return {
                    "text": "",
                    "pages": page_count,
                    "size": len(pdf_content),
                    "filename": filename,
                    "truncated": truncated,
                    "success": False,
                    "error": "No extractable text found",
                }

            logger.info(
                f"Successfully extracted text from {filename} "
                f"({len(full_text)} chars, {page_count} pages)"
            )

            return {
                "text": full_text.strip(),
                "pages": page_count,
                "size": len(pdf_content),
                "filename": filename,
                "truncated": truncated,
                "success": True,
                "error": None,
            }

        except Exception:
            # Log full exception details server-side for debugging
            logger.exception(f"Error extracting text from {filename}")
            # Return generic error message to avoid exposing internal details
            return {
                "text": "",
                "pages": 0,
                "size": len(pdf_content),
                "filename": filename,
                "truncated": False,
                "success": False,
                "error": "Failed to extract text from PDF",
            }

    @staticmethod
    def extract_batch(files_data: List[Dict[str, Any]]) -> Dict[str, Any]:
        """
        Extract text from multiple PDF files.

        Args:
            files_data: List of dicts with 'content' (bytes) and 'filename' (str)

        Returns:
            Dictionary with:
            - 'results': List of extraction results
            - 'total_files': Total number of files processed
            - 'successful': Number of successfully processed files
            - 'failed': Number of failed files
            - 'errors': List of error messages
        """
        results: list[Dict[str, Any]] = []
        successful = 0
        failed = 0
        errors: list[str] = []

        for file_data in files_data:
            result = PDFExtractionService.extract_text_and_metadata(
                file_data["content"], file_data["filename"]
            )

            results.append(result)

            if result["success"]:
                successful += 1
            else:
                failed += 1
                errors.append(f"{file_data['filename']}: {result['error']}")

        return {
            "results": results,
            "total_files": len(files_data),
            "successful": successful,
            "failed": failed,
            "errors": errors,
        }


# Singleton pattern for service
_pdf_extraction_service = None


def get_pdf_extraction_service() -> PDFExtractionService:
    """Get the singleton PDF extraction service instance."""
    global _pdf_extraction_service
    if _pdf_extraction_service is None:
        _pdf_extraction_service = PDFExtractionService()
    return _pdf_extraction_service