Files
HealthLog/src/lib/documents/pdfjs.ts
T
Marc-André Bombeck c47f61f02a feat(documents): read a PDF's text layer page by page without rendering
The reader child gains a `page-texts` request: the first pages' text
layers, each read the way a rendered page's is (cut and marked past the
per-page cap), plus the document's page count. Nothing is rendered, so it
works where the page renderer cannot run.

`readPdfTextLayer` judges what that read is worth: a page counts only with
at least 40 characters of its own besides white space, so a scanner's
stamped page number or fax header does not pass for a page of text; a PDF
with no such page is a scan and is reported as one.
2026-11-03 22:07:11 +01:00

95 lines
3.4 KiB
TypeScript

/**
* Reading a PDF's text layer, the way the server does it: in a child process
* under a memory ceiling and a time limit (`./pdf-isolation.ts`), through the
* one pdf.js loader (`./pdf-engine.mjs`). Rendering goes the same way from
* `./rasterize-pdf.ts`.
*
* This module has no logging of its own, so the end-to-end suite can read
* report text with it; the callers say what failed.
*/
import { runPdfChild, type PdfPageTexts } from "./pdf-isolation";
export { joinTextRuns } from "./pdf-engine.mjs";
/**
* How long one text read may take. The page walk is capped by the caller
* (200 pages for the content index); a document still inside this after that
* many pages is one no reader should wait on.
*/
export const PDF_TEXT_TIMEOUT_MS = 60_000;
/** A document that crossed the memory ceiling or the time limit. */
export class PdfResourceLimitError extends Error {
override name = "PdfResourceLimitError";
constructor(readonly limit: "memory" | "time") {
super(`the PDF crossed the reader's ${limit} limit`);
}
}
/**
* The reader could not take the document now: it was busy past the wait this
* work may spend, or its process ended for a reason that says nothing about
* the document. Not a verdict on the file; the work is tried again later.
*/
export class PdfReaderUnavailableError extends Error {
override name = "PdfReaderUnavailableError";
constructor(readonly kind: "busy" | "interrupted") {
super(
kind === "busy"
? "the document reader is busy"
: "the document reader stopped before it answered",
);
}
}
/**
* The text layer of the first `maxPages` pages, one page after another.
* Throws what pdf.js threw (a malformed file, a password, a page that will
* not parse) under its own name, `PdfResourceLimitError` when the document
* crossed a bound, or `PdfReaderUnavailableError` when the reader could not
* take it now.
*/
export async function readPdfText(
data: Uint8Array,
maxPages = Number.POSITIVE_INFINITY,
): Promise<string> {
const result = await runPdfChild(
// Infinity does not cross the channel as a page count.
{ op: "text", data, maxPages: Math.min(maxPages, Number.MAX_SAFE_INTEGER) },
{ timeoutMs: PDF_TEXT_TIMEOUT_MS },
);
if (result.ok) return result.value;
if (result.reason === "limit") throw new PdfResourceLimitError(result.limit);
if (result.reason === "busy" || result.reason === "interrupted") {
throw new PdfReaderUnavailableError(result.reason);
}
const err = new Error(result.message);
err.name = result.name;
throw err;
}
/**
* The text layer of the first `maxPages` pages, page by page, with the
* document's page count. Each page is read as a rendered page's text layer is
* (`pageText`: cut and marked past `RASTER_PAGE_TEXT_MAX`, empty for a page
* without one), and nothing is rendered, so it works where the page renderer
* cannot run. Throws as `readPdfText` does.
*/
export async function readPdfPageTexts(
data: Uint8Array,
maxPages: number,
): Promise<PdfPageTexts> {
const result = await runPdfChild(
{ op: "page-texts", data, maxPages },
{ timeoutMs: PDF_TEXT_TIMEOUT_MS },
);
if (result.ok) return result.value;
if (result.reason === "limit") throw new PdfResourceLimitError(result.limit);
if (result.reason === "busy" || result.reason === "interrupted") {
throw new PdfReaderUnavailableError(result.reason);
}
const err = new Error(result.message);
err.name = result.name;
throw err;
}