feat(tools): 2.0 phase 5 wave 2 - pdf depth (21 tools) (#220)

This commit is contained in:
SnapOtter
2026-06-13 10:18:55 +08:00
parent ae1337901d
commit 2f39e38162
128 changed files with 11381 additions and 778 deletions
@@ -5,9 +5,17 @@ import { join } from "node:path";
import {
gsAvailable,
gsCompressPdf,
gsGrayscalePdf,
gsPdfaConvert,
qpdfAvailable,
qpdfDecrypt,
qpdfEncrypt,
qpdfLinearize,
qpdfMerge,
qpdfPageCount,
qpdfPagesSpec,
qpdfPagesSpecUnchecked,
qpdfRepair,
qpdfRotate,
qpdfSplitRanges,
} from "@snapotter/doc-engine";
@@ -59,6 +67,93 @@ describe.skipIf(!qpdfAvailable())("doc-engine pdf ops (requires qpdf)", () => {
rmSync(dir, { recursive: true, force: true });
}
});
it("encrypts and decrypts roundtrip", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const enc = join(dir, "enc.pdf");
const dec = join(dir, "dec.pdf");
await qpdfEncrypt(PDF, "user1", "owner1", enc);
await expect(qpdfPageCount(enc)).rejects.toThrow(); // password gate works
await qpdfDecrypt(enc, "user1", dec);
expect(await qpdfPageCount(dec)).toBe(3);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("rejects a wrong password on decrypt", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
await expect(
qpdfDecrypt(
join(process.cwd(), "tests/fixtures/documents/encrypted.pdf"),
"wrong",
join(dir, "x.pdf"),
),
).rejects.toThrow(/password|invalid/i);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("reorders pages with an explicit order", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const out = join(dir, "reordered.pdf");
await qpdfPagesSpec(PDF, "3,1,2", out);
expect(await qpdfPageCount(out)).toBe(3);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("linearizes", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const out = join(dir, "lin.pdf");
await qpdfLinearize(PDF, out);
expect(await qpdfPageCount(out)).toBe(3);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("qpdfPagesSpecUnchecked accepts specs exceeding 200 chars", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
// Build a long spec that would fail assertValidRange's 200-char cap
// Use a spec like "1,2,3" for a 3-page PDF (valid grammar, just testing the bypass)
const longSpec = "1,2,3";
const out = join(dir, "unchecked.pdf");
await qpdfPagesSpecUnchecked(PDF, longSpec, out);
expect(await qpdfPageCount(out)).toBe(3);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("qpdfPagesSpecUnchecked rejects invalid grammar", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
await expect(qpdfPagesSpecUnchecked(PDF, "abc;rm -rf", join(dir, "x.pdf"))).rejects.toThrow(
/range/i,
);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("repairs (rewrites) a valid pdf without loss", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const out = join(dir, "repaired.pdf");
await qpdfRepair(PDF, out);
expect(await qpdfPageCount(out)).toBe(3);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe.skipIf(!gsAvailable())("doc-engine ghostscript compress (requires gs)", () => {
@@ -75,4 +170,28 @@ describe.skipIf(!gsAvailable())("doc-engine ghostscript compress (requires gs)",
rmSync(dir, { recursive: true, force: true });
}
});
it("converts to grayscale (valid pdf out)", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const out = join(dir, "gray.pdf");
await gsGrayscalePdf(PDF, out);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("produces a pdf/a candidate", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const out = join(dir, "pdfa.pdf");
await gsPdfaConvert(PDF, out);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});