Select text in a PDF, and find PDFs by their contents

Stage five. pdf.js draws pages as pictures, so selecting means laying
transparent spans over them exactly where each word was painted. The
layer carries the same scale as the canvas — they drift apart otherwise
and a selection lands on the wrong words. While drawing it stops taking
pointer events entirely, or a stroke would come out as a selection.

The text is also extracted and handed to the index that already holds the
notes, so one search box finds a sentence whether it was typed or
imported. Extraction runs after the document is on screen and yields
between pages: it must never stand between the reader and the first page.

The extracted text is deliberately not sent to the server. It is
derivable from a file the server already holds, so storing it again costs
space and a second thing to encrypt for nothing gained. The price, stated
plainly in the readme, is that a PDF becomes searchable on a device once
that device has opened it — the same shape as the limitation notes have.

Text runs are joined on pdf.js's hasEOL rather than concatenated. Without
it the last word of a line glues to the first of the next and matches
nothing when searched for.

The minimal text-layer CSS is written out rather than importing
pdfjs-dist/web/pdf_viewer.css, which carries the whole reference viewer's
toolbars and dialogs.

Suite now checks the layer sits over the page rather than stacked in a
corner, that the text is transparent, that dragging selects real words,
that a word from inside the file finds the note and one that is not in it
finds nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
marcandClaude Opus 5 committed 2026-08-08 21:12:06 +02:00
1 parent 1be3a9b9ba
commit b12b0d6142
6 files changed
+303 -6

No files matched your search

+54
View File
@@ -81,6 +81,60 @@ export async function openPdf(bytes: Uint8Array): Promise<LoadedPdf> {
return { doc, pageCount: doc.numPages, sizes };
}
/**
* Lays the selectable text over a rendered page.
*
* pdf.js draws pages as pictures, so selecting means positioning transparent
* spans exactly where the glyphs were painted. The scale must match the canvas
* or the two drift apart and a selection lands on the wrong words.
*
* Returns a cancel function — a page can be re-rendered at a new zoom before the
* previous text layer has finished building.
*/
export async function renderTextLayer(
page: PDFPageProxy,
container: HTMLElement,
cssWidth: number
): Promise<void> {
const pdfjs = await loadPdfjs();
const base = page.getViewport({ scale: 1 });
const scale = cssWidth / base.width;
const viewport = page.getViewport({ scale });
container.replaceChildren();
// Every span's transform is expressed against this, so the layer only lines up
// with the canvas when it carries the same scale.
container.style.setProperty("--scale-factor", String(scale));
const layer = new pdfjs.TextLayer({
textContentSource: await page.getTextContent(),
container,
viewport,
});
await layer.render();
}
/**
* The words on a page, in reading order.
*
* pdf.js hands back positioned runs, not sentences: a line can arrive in several
* pieces and `hasEOL` is the only marker of where one ends. Joining runs without
* it glues the last word of a line to the first of the next, which then matches
* nothing when searched for.
*/
export async function extractPageText(page: PDFPageProxy): Promise<string> {
const content = await page.getTextContent();
let out = "";
for (const item of content.items) {
const run = item as { str?: string; hasEOL?: boolean };
if (typeof run.str !== "string") continue;
out += run.str;
if (run.hasEOL) out += "\n";
else if (run.str && !run.str.endsWith(" ")) out += " ";
}
return out.replace(/[ \t]+/g, " ").trim();
}
/**
* Renders one page onto a canvas at the given CSS width.
*