add local pdf page extraction and text search tools - #6
Merged
Conversation
extract_document_pages pulls pages out of a document's pdf into a new file on disk (pdf-lib), and find_document_pages locates the pages containing a text snippet via the pdf text layer (unpdf). both run locally and never modify the document in paperless.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two tools that process a document's PDF locally in the MCP server, complementing the existing server-side editing tools (
rotate_documents,edit_pdf_document, ...) which always modify the stored document:extract_document_pages— downloads the PDF, extracts the requested pages (1-based, order preserved) with pdf-lib, and writes a new PDF to disk. Supportsoriginal: trueto use the original file instead of the archived version. Errors clearly on out-of-range pages and non-PDF content.find_document_pages— extracts per-page text with unpdf (Mozilla pdf.js) and returns the pages matching a case-insensitive, whitespace-normalized query, each with a snippet. Pairs withextract_document_pagesto answer "give me just the page that mentions X" — Paperless's own full-text search has no page numbers.Both are regular dependencies (pure JS, no native bindings), so unlike the embeddings stack they don't need the optional-dependency treatment.
Test plan
src/__tests__/pdf-tools.test.tsbuild real PDFs with pdf-lib, stubclient.download, and verify the request path, the written output PDF's page count, out-of-range/non-PDF errors, and text matching incl. the no-match caseformat:check,lint,typecheck, and the full suite (144 tests) pass🤖 Generated with Claude Code