cd /news/developer-tools/building-a-custom-ai-pdf-reader-in-p… Β· home β€Ί topics β€Ί developer-tools β€Ί article
[ARTICLE Β· art-97045] src=dev.to β†— pub= topic=developer-tools verified=true sentiment=Β· neutral

Building a Custom AI PDF Reader in Python: From a Jupyter Prototype to Tested Modules

A developer built a custom AI PDF reader in Python, starting with a Jupyter prototype and evolving it into tested modules. The reader uses PyMuPDF for rendering and search, with features like bookmarks, notes, and zoom. The project highlights the importance of separating PDF coordinates from image coordinates and using a centralized state for UI consistency.

read7 min views1 publishedAug 14, 2026

Research papers are much easier to read when the reader fits the way you work. I wanted a PDF reader that could eventually support bookmarks, notes, annotations, summaries, question answering, text-to-speech, and voice commands.

Rather than trying to write a complete PDF engine from scratch, I started with a small Python prototype. The goal was simple: learn each layer of the application properly, build a working foundation, and only then move toward a desktop application.

This article documents the first stage of that journey: an interactive PDF reader in Jupyter, local persistence for bookmarks and notes, a cleaner module structure, automated tests, and the lessons learned along the way.

The long-term goal is a customizable desktop PDF reader for research reading. The eventual application may include:

For the first milestone, I deliberately kept the scope smaller. I focused on PDF rendering, navigation, search, persistence, and tests.

I chose Python because it allowed me to experiment quickly. My first stack was:

Need Tool
PDF rendering, text extraction, and search PyMuPDF
Interactive prototype interface JupyterLab and ipywidgets
Image handling Pillow
Local saved data JSON
Version control Git and GitHub
Automated tests pytest

The future desktop UI will use PySide6, but Jupyter was a useful place to learn the reader logic before dealing with desktop-window layouts, signals, menus, and packaging.

The prototype opens a local PDF with PyMuPDF:

from pathlib import Path
import pymupdf

PDF_PATH = Path("test.pdf")
document = pymupdf.open(PDF_PATH)

print(document.page_count)

A PDF page is not automatically an image. PyMuPDF renders a page into a pixmap, and Pillow converts the pixel data into an image that Jupyter can display.

from PIL import Image

page = document[0]
pixmap = page.get_pixmap(dpi=120)

image = Image.frombytes(
    "RGB",
    (pixmap.width, pixmap.height),
    pixmap.samples
)

image

This gave me the basic page view. From there, I added buttons for Previous, Next, Go to page, Zoom In, and Zoom Out.

One important concept I learned was application state. Instead of letting each button manage unrelated variables, I kept the reader's current information together:

reader_state = {
    "current_page": 0,
    "zoom_dpi": 120,
    "search_results": [],
    "search_index": 0,
    "bookmarks": [],
    "notes": []
}

The UI follows a simple pattern:

reader_state

.For example, page navigation uses one shared function:

def change_page(new_page):
    if not 0 <= new_page < document.page_count:
        return

    reader_state["current_page"] = new_page
    save_current_reader_data()
    refresh_reader()

Using a single function for navigation prevents different controls from handling page changes in slightly different ways.

PyMuPDF can find the rectangles where a text query appears on a page.

rectangles = page.search_for("research")

The returned rectangles use PDF coordinates, measured in points. The rendered page image uses pixels. Since PDF points are based on 72 points per inch, I learned to scale each search rectangle with this formula:

scale = dpi / 72
pixel_x = pdf_x * scale

That conversion lets the reader draw highlights in the correct position on the rendered image.

from PIL import ImageDraw

draw = ImageDraw.Draw(image, "RGBA")

for rect in rectangles:
    draw.rectangle(
        [
            rect.x0 * scale,
            rect.y0 * scale,
            rect.x1 * scale,
            rect.y1 * scale
        ],
        fill=(255, 235, 0, 90),
        outline=(255, 0, 0, 255),
        width=3
    )

This was one of the most useful lessons in the project: PDF document coordinates and screen-image coordinates are not the same thing.

Bookmarks and notes should survive a restart. I did not want to modify the original PDF for this first version, so I stored personal reader data in a JSON file.

A document entry looks like this:

{
  "documents": {
    "/absolute/path/to/test.pdf": {
      "file_name": "test.pdf",
      "bookmarks": [
        {
          "page_number": 2,
          "label": "Important result"
        }
      ],
      "notes": [
        {
          "page_number": 5,
          "text": "Review this figure before the presentation.",
          "created_at": "2026-08-11 15:30"
        }
      ],
      "last_page": 5
    }
  }
}

A bookmark points to a page and has a label. A note points to a page, has text, and stores when it was created. The current page is also saved whenever the user navigates.

I used a temporary file before replacing the main JSON file:

def save_all_reader_data(data):
    temporary_path = DATA_PATH.with_suffix(".tmp")

    with temporary_path.open("w", encoding="utf-8") as file:
        json.dump(data, file, indent=2, ensure_ascii=False)

    os.replace(temporary_path, DATA_PATH)

This is safer than directly overwriting the main file because it reduces the risk of leaving a partially written JSON file if a save is interrupted.

The first notebook worked, but it was becoming one large cell. That is acceptable for exploration, but difficult to maintain.

I moved reusable code into separate modules:

custom-ai-pdf-reader/
β”œβ”€β”€ data/
β”‚   └── reader_data.json
β”œβ”€β”€ notebooks/
β”‚   └── Untitled.ipynb
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ pdf_service.py
β”‚   β”œβ”€β”€ reader_state.py
β”‚   └── storage_service.py
β”œβ”€β”€ tests/
β”‚   └── test_storage.py
β”œβ”€β”€ .gitignore
β”œβ”€β”€ pytest.ini
└── requirements.txt

pdf_service.py

This module is responsible for PDF-specific tasks:

def open_pdf(pdf_path):
    ...

def get_page_count(document):
    ...

def render_page(document, page_number, dpi, highlight_rectangles=None):
    ...

def search_document(document, query):
    ...

storage_service.py

This module handles JSON persistence:

def load_all_reader_data():
    ...

def save_all_reader_data(data):
    ...

def load_document_data(document_id, pdf_path):
    ...

def save_document_data(document_id, pdf_path, bookmarks, notes, last_page):
    ...

reader_state.py

This module stores the reader's active state:

reader_state = {
    "current_page": 0,
    "zoom_dpi": 120,
    "search_results": [],
    "search_index": 0,
    "bookmarks": [],
    "notes": []
}

The notebook now focuses on the interface and event handlers, while the reusable logic lives in Python files.

I added pytest tests for the storage layer. The tests use temporary folders, so they do not touch my real bookmarks, notes, or reader_data.json

file.

Examples of what the tests verify:

The first successful test run was a good milestone:

collected 5 items

5 passed in 0.02s

This was also my first practical lesson in why automated tests matter. The interface can look correct while a save or load function still has a hidden problem. Tests give the project a repeatable safety net before making larger changes.

Building this project involved several useful mistakes and fixes.

I initially considered a Tkinter desktop interface, but the Linux Python environment did not include the required Tk bindings. Instead of spending the first phase on GUI installation problems, I switched to Jupyter widgets for the prototype and chose PySide6 for the future desktop application.

An Ubuntu background update held the package-manager lock. The correct response was to wait and inspect the running update, not to delete lock files or force-stop the process.

At one stage, interactive Jupyter widgets appeared as plain text or did not respond to clicks. The problem was environment setup: Jupyter, the Python kernel, and ipywidgets need to be connected to the same project environment. Restarting the kernel and testing a minimal button helped isolate the issue.

When I moved code into modules, I accidentally kept old versions of functions such as render_page()

, search_document()

, and JSON storage functions inside the notebook. This created duplicate names and confusing behavior. The fix was to keep the reusable function in one module and import it into the notebook.

I created a new project folder inside an existing Git repository and accidentally ran git init

inside the nested folder. That created a second .git

directory. The correct approach was to remove only the accidental nested .git

folder and use the original repository at the parent level.

src

My tests initially failed with ModuleNotFoundError: No module named 'src'

. I fixed that by adding src/__init__.py

and configuring pytest with a pytest.ini

file.

[pytest]
pythonpath = .
testpaths = tests

The tests then passed.

I learned to use a safer Git workflow for each tested milestone:

git status
git add custom-ai-pdf-reader/src/
git add custom-ai-pdf-reader/tests/
git add custom-ai-pdf-reader/notebooks/
git commit -m "Add modular PDF reader prototype and storage tests"
git push

The .gitignore

file is important because local PDFs, the virtual environment, temporary cache files, and personal reader data should not be uploaded.

__pycache__/
*.py[cod]
.pytest_cache/
.venv/
.ipynb_checkpoints/
data/
*.pdf

This project taught me more than how to display a PDF in Python. The main lessons were:

The Jupyter prototype now has a tested storage layer. The next milestone is a PySide6 desktop MVP with:

After the desktop foundation is stable, I plan to migrate persistence from JSON to SQLite, add text selection and annotations, then explore AI summaries, PDF question answering with citations, text-to-speech, and voice commands.

Starting small was the right choice. I did not begin by trying to build an AI system or a complete commercial PDF application. I started with one page, then navigation, then search, then bookmarks, notes, saved progress, modules, tests, and version control.

Each feature made the next one easier to understand. The project is still at an early stage, but it already has a working and tested foundation for a more capable custom research reader.

If you are building your own developer project, my advice is simple: keep the first version small, make each milestone testable, and commit working progress often.

── more in #developer-tools 4 stories Β· sorted by recency
── more on @pymupdf 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain β€” perfect for shipping the agent you just read about.

$git push zahid main
β†’ Live at https://your-agent.zahid.host βœ“
Get free account β†’ Pricing
from €0/mo Β· no card required
LIVE [news/building-a-custom-ai…] indexed:0 read:7min 2026-08-14 Β· β€”