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. 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: python 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. python 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: python 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. python 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: python 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: python 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: python 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.