Read a PDF where it is imported, and keep the text with the file

A PDF was searchable on a device once that device had opened it, because
the extracted text lived in that browser's IndexedDB and nowhere else. So
a document imported on the desktop was invisible to a search on the
phone — which is exactly the case that matters for filing invoices, since
those are imported and then never opened again.

The reading still never happens on the server. It has no PDF library and
is not getting one: the bytes arrive encrypted and are stored that way.
The device that imports the file reads it there and then, and the result
is stored beside the file in attachments.text_index, encrypted at rest
like everything else and opaque to the server. Every other device asks
for it after login and can then find a document it has never downloaded.

Not storing it was the earlier decision, on the reasoning that text
derivable from bytes the server already holds should not be kept twice.
True, and useless. A few hundred kilobytes against a file measured in
megabytes is a trade worth making.

A `null` answer is not an error — it means nobody has read this file yet.
Documents extracted before this existed are sent up the next time they
are opened.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
marcandClaude Opus 5 committed 2026-08-11 19:26:07 +02:00
1 parent 228552d453
commit 8e38404a69
8 files changed
+268 -23

No files matched your search

+116 -11
View File
@@ -5,17 +5,26 @@
* same index that holds the notes — so one search box finds a sentence whether
* it was typed or imported.
*
* **The text is not sent to the server.** It is derivable from a file the server
* already has, so storing it again would cost space and a second thing to
* encrypt for no information gained. The price is that a PDF becomes searchable
* on a device once that device has opened it. That is the same shape as the
* limitation notes already have, and the honest trade for not duplicating data.
* **The reading happens on a device, never on the server.** The server has no
* PDF library and no way to open one: the bytes arrive encrypted and are stored
* that way. The device that imports the file does the work once, and the result
* is stored beside the file — encrypted at rest like everything else — so every
* other device can search a document it has never opened.
*
* It used to be kept only in the browser that had opened the file, on the
* reasoning that text derivable from bytes the server already holds should not
* be stored twice. That was true and useless: a PDF imported on the desktop was
* invisible to a search on the phone. Storing it is what makes an archive of
* scanned invoices findable from anywhere, and it costs a few hundred kilobytes
* against a file that is measured in megabytes.
*/
import * as db from "../../offlineStore";
import * as sync from "../../syncEngine";
import * as searchIndex from "../../search";
import type { NoteMeta } from "../../types";
import type { PDFDocumentProxy } from "./pdfjs";
import { extractPageText } from "./pdfjs";
import { extractPageText, openPdf } from "./pdfjs";
/** Cache key. Keyed by attachment, not note: the same file in two notes is extracted once. */
const keyFor = (attachmentId: string) => `pdftext:${attachmentId}`;
@@ -34,6 +43,92 @@ export async function cachedPdfText(attachmentId: string): Promise<PdfText | nul
return (await db.getMeta<PdfText>(keyFor(attachmentId))) ?? null;
}
const textOf = (pages: string[]): PdfText => ({
pages,
full: pages.join("\n").slice(0, MAX_TEXT_CHARS),
});
const cache = async (attachmentId: string, text: PdfText): Promise<void> => {
try {
await db.setMeta(keyFor(attachmentId), text);
} catch {
/* a full quota costs the cache, not the search */
}
};
/** Stores the text with the file if nothing is stored there yet. */
async function ensureStored(attachmentId: string, pages: string[]): Promise<void> {
try {
if (await sync.fetchPdfText(attachmentId)) return;
await sync.storePdfText(attachmentId, pages);
} catch {
/* offline, or the file is gone — either way, nothing to do about it here */
}
}
/**
* The text of a file, from wherever it can be had cheapest.
*
* Locally first, then from the server — which is where the device that imported
* the file left it. Null means nobody has read this document yet, and whoever
* has the bytes should.
*/
export async function loadPdfText(attachmentId: string): Promise<PdfText | null> {
const local = await cachedPdfText(attachmentId);
if (local) return local;
try {
const pages = await sync.fetchPdfText(attachmentId);
if (!pages) return null;
const text = textOf(pages);
await cache(attachmentId, text);
return text;
} catch {
// Offline, or the file is gone. Either way there is nothing to index and
// nothing worth interrupting the user about.
return null;
}
}
/**
* Reads a freshly imported PDF, so it is searchable everywhere immediately.
*
* Done on the device that has the file open anyway, rather than left until some
* device opens the document — which for an invoice filed and never looked at
* again may be never. Failure is silent on purpose: the import succeeded, and
* the text can still be extracted the next time the file is opened.
*/
export async function extractOnImport(file: Blob, attachmentId: string): Promise<void> {
try {
const { doc } = await openPdf(new Uint8Array(await file.arrayBuffer()));
await extractAndCache(doc, attachmentId);
} catch (err) {
console.warn(`[pdf] could not read the text of ${attachmentId} on import:`, err);
}
}
/**
* Fills the search index with the text of every PDF the account holds.
*
* Runs beside the note backfill after login. A document whose text nobody has
* extracted yet is left alone: this device may not have its bytes, and
* downloading every PDF to index it is not a thing to do in the background.
*/
export async function backfillPdfText(
notes: NoteMeta[],
signal?: { aborted: boolean }
): Promise<number> {
let indexed = 0;
for (const note of notes) {
if (signal?.aborted) break;
if (!note.pdfAttachmentId) continue;
const text = await loadPdfText(note.pdfAttachmentId);
if (!text) continue;
indexPdfText(note.id, note.title || "", text);
indexed++;
}
return indexed;
}
/**
* Extracts every page's text and remembers it.
*
@@ -47,7 +142,12 @@ export async function extractAndCache(
signal?: { aborted: boolean }
): Promise<PdfText | null> {
const existing = await cachedPdfText(attachmentId);
if (existing) return existing;
if (existing) {
// Extracted here before the text was ever stored with the file. Sending it
// now is what makes those documents findable on the other devices too.
void ensureStored(attachmentId, existing.pages);
return existing;
}
const pages: string[] = [];
let total = 0;
@@ -66,11 +166,16 @@ export async function extractAndCache(
await new Promise((r) => setTimeout(r, 0));
}
const result: PdfText = { pages, full: pages.join("\n").slice(0, MAX_TEXT_CHARS) };
const result = textOf(pages);
await cache(attachmentId, result);
// Left with the file, so the next device does not have to read it again — and
// can find the document without ever opening it. A failure here is not worth
// reporting: the text is cached locally, and the next device to open the file
// will store it.
try {
await db.setMeta(keyFor(attachmentId), result);
} catch {
/* a full quota costs the cache, not the search */
await sync.storePdfText(attachmentId, pages);
} catch (err) {
console.warn(`[pdf] could not store the text of ${attachmentId}:`, err);
}
return result;
}
+7
View File
@@ -4,6 +4,7 @@ import * as sync from "../syncEngine";
import * as searchIndex from "../search";
import { sameStrokeDoc } from "../features/drawing/strokes.js";
import { reindexEntries } from "../features/notes/noteSort";
import { extractOnImport } from "../features/pdf/pdfText";
import type { Backlink, NoteContent, NoteMeta, Notebook, StrokeDoc, User } from "../types";
/** How often the whole account is reconciled with the server. */
@@ -203,6 +204,12 @@ export function useNoteData(user: User | null, activeNote: string | null): NoteD
const title = file.name.replace(/\.pdf$/i, "") || "Dokument";
const note = await sync.createPdfNote(folderId, title, attachment.id);
setNotes((p) => [note, ...p]);
/* Read the document here, now, on the machine that already has it open.
*
* Waiting until someone opens it means a filed invoice may never be read at
* all, and until it is, no device can find it. Not awaited: the note exists
* and should appear at once — the text catches up. */
void extractOnImport(file, attachment.id);
return note;
}, []);
+7
View File
@@ -2,6 +2,7 @@ import { useEffect, useMemo, useRef, useState } from "react";
import * as sync from "../syncEngine";
import * as searchIndex from "../search";
import { backfillPdfText } from "../features/pdf/pdfText";
import type { NoteMeta, SearchHit, User } from "../types";
const DEBOUNCE_MS = 120;
@@ -52,6 +53,12 @@ export function useSearchIndex(user: User | null, notes: NoteMeta[], query: stri
},
});
if (!cancelled) setVersion((v) => v + 1);
/* PDFs last: their text is stored with the file rather than derived from a
* note body, so it has to be fetched separately — and a document nobody
* has read yet is simply skipped. Searching a note is more urgent than
* searching a scan, so this waits until the notes are in. */
const indexed = await backfillPdfText(notesRef.current, signal);
if (indexed && !cancelled) setVersion((v) => v + 1);
running.current = false;
})();
+16
View File
@@ -1127,6 +1127,22 @@ export async function loadPdfBytes(attachmentId: string): Promise<Uint8Array> {
return new Uint8Array(await blob.arrayBuffer());
}
/* ─── The text inside a PDF ───
*
* Read on a device, stored with the file, handed to every other device. The
* server never opens a PDF; it keeps this the way it keeps a note body —
* encrypted, and unread by it. */
/** The stored text of a PDF, page by page, or null if nobody has read it yet. */
export async function fetchPdfText(attachmentId: string): Promise<string[] | null> {
const r = await api.get<{ pages: string[] | null }>(`/attachments/${attachmentId}/text`);
return r.pages ?? null;
}
export async function storePdfText(attachmentId: string, pages: string[]): Promise<void> {
await api.put(`/attachments/${attachmentId}/text`, { pages });
}
/** Creates a note that holds a PDF. */
export async function createPdfNote(
folderId: string | null,