fix(pdf): flag scanned PDFs in pdf-to-text and serve text as UTF-8 (#603)

When a PDF has no text layer (scanned or image-only), pdf-to-text now
returns a 422 that points at the OCR tool instead of a silent empty file,
and text downloads carry charset=utf-8 so UTF-8 Arabic renders correctly
when the .txt is viewed inline.

Fixes #589
This commit is contained in:
SnapOtter
2026-07-21 16:26:43 +08:00
committed by GitHub
parent 43334324c4
commit 7d37f6e6f5
8 changed files with 109 additions and 19 deletions
@@ -31,7 +31,7 @@ vi.mock("../../../apps/api/src/config.js", () => ({
vi.mock("@snapotter/doc-engine", () => ({
htmlToPdfPy: vi.fn(),
pdfFlattenPy: vi.fn(),
pdfTextPy: vi.fn(async () => ({ chars: 12 })),
pdfTextPy: vi.fn(async () => ({ chars: 12, hasText: true })),
qpdfAvailable: vi.fn(() => false),
qpdfCheck: vi.fn(),
qpdfPageCount: vi.fn(),
@@ -105,6 +105,19 @@ describe("document route processors", () => {
});
});
it("rejects a PDF with no text layer and points the user to OCR", async () => {
// A scanned/image-only PDF yields hasText=false; the route must reject with
// an OCR handoff instead of returning a silent empty .txt (#589).
vi.mocked(pdfTextPy).mockResolvedValueOnce({ chars: 0, hasText: false });
registerPdfToText(createMockApp());
const config = getToolConfig("pdf-to-text");
await withScratch(async (scratchDir) => {
const ctx = createCtx(scratchDir, "scanned.pdf");
await expect(config?.processV2?.(ctx)).rejects.toThrow(/text layer/i);
});
});
it("flattens PDFs through pdfFlattenPy", async () => {
registerFlattenPdf(createMockApp());
const config = getToolConfig("flatten-pdf");