Skip to main content

Highlight text chunks in PDFs – a FastAPI microservice for RAG pipelines

Project description

RAG PDF Highlighter

Highlight text chunks inside PDF documents — built for RAG pipelines.

Given a PDF URL and a list of text chunks (with page numbers), this service locates each chunk in the PDF and returns an annotated copy with highlights applied.

Features

  • 3-tier text matching — exact → sentence → collapsed-whitespace fallback
  • Async PDF download — non-blocking I/O via httpx
  • Stateless — temp files are cleaned up after every request
  • Docker-ready — single-command container deployment
  • Library-friendly — core logic raises plain Python exceptions, no FastAPI dependency required

Installation

pip install rag-pdf-highlighter

Or install from source:

git clone https://github.com/MuhammadSalmanAhmad/rag-pdf-highlighter.git
cd rag-pdf-highlighter
pip install -e ".[dev]"

Quick Start

As a microservice

uvicorn rag_pdf_highlighter.main:app --host 0.0.0.0 --port 8000

Then send a POST request:

curl -X POST http://localhost:8000/highlight \
  -H "Content-Type: application/json" \
  -d '{
    "pdf_url": "https://example.com/report.pdf",
    "documents": [
      {
        "page_content": "Text to highlight in the PDF",
        "metadata": {"page": 0}
      }
    ]
  }' \
  --output highlighted.pdf

With Docker

docker build -t rag-pdf-highlighter .
docker run -p 8000:8000 rag-pdf-highlighter

As a library

from langchain_core.documents import Document
from rag_pdf_highlighter.utils.pdf_helpers import highlight_chunks_in_pdf

documents = [
    Document(page_content="Text to find", metadata={"page": 0}),
]

output_path = highlight_chunks_in_pdf(
    pdf_path="./report.pdf",
    documents=documents,
)
print(f"Highlighted PDF saved to: {output_path}")

API Reference

GET /

Health check endpoint.

POST /highlight

Field Type Description
pdf_url string URL of the PDF to highlight
documents array List of {page_content, metadata} objects
documents[].page_content string The text chunk to locate and highlight
documents[].metadata.page int Zero-indexed page number

Returns: The highlighted PDF file (application/pdf).

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Lint
ruff check src/ test/

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rag_pdf_highlighter-0.1.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rag_pdf_highlighter-0.1.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file rag_pdf_highlighter-0.1.1.tar.gz.

File metadata

  • Download URL: rag_pdf_highlighter-0.1.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for rag_pdf_highlighter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a2952e0f4662b3587d2eb2c389edc20a0d30ea6efa1b2d8066e371c6f6f09bad
MD5 7517d1cf46aef505dcfb97b41ee466c4
BLAKE2b-256 059332c46739cc8d936c10bf5b49b61813ff33906567972aacc9a14917d32ef7

See more details on using hashes here.

File details

Details for the file rag_pdf_highlighter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for rag_pdf_highlighter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1655554bb36e9fe94d08577e3668ab32af58a4eab9a1173fa6f8520776ee812f
MD5 9f5d1e073c35425950d34ffc49492f80
BLAKE2b-256 d417ddf5f420029c22b8584be6724df30f55fbe72aae076b8b876ec77804ee34

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page