fix(compress-pdf): land close to the target size, honestly (#522)

Target-size compression had only a coarse DPI lever, so it undershot badly (a 350KB target could land at 216KB) and silently missed unreachable targets. Adds JPEG quality as a second lever (forced re-encode so it bites on JPEG scans), folds both into one monotonic quality axis that target-size binary-searches, reports targetMet honestly in the panel across 21 locales, and flips the tool to async for the extra passes. Quality-mode output sizes shift intentionally (slider now drives JPEG quality at full resolution in its top half).
This commit is contained in:
SnapOtter
2026-07-16 15:08:21 +08:00
committed by GitHub
parent f858c4cea0
commit 7d938af1f9
30 changed files with 344 additions and 65 deletions
@@ -5,6 +5,7 @@ import { join } from "node:path";
import {
gsAvailable,
gsCompressPdf,
gsCompressPdfTuned,
gsGrayscalePdf,
gsPdfaConvert,
qpdfAvailable,
@@ -168,6 +169,23 @@ describe.skipIf(!gsAvailable())("doc-engine ghostscript compress (requires gs)",
}
});
it("gsCompressPdfTuned: lower quality (higher QFactor) yields a smaller file at fixed DPI", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {
const best = join(dir, "tuned-best.pdf");
const worst = join(dir, "tuned-worst.pdf");
// Image-heavy scan: QFactor only bites because the primitive forces re-encode.
await gsCompressPdfTuned(fixtures.document.pdfScanned, best, 150, 0.1);
await gsCompressPdfTuned(fixtures.document.pdfScanned, worst, 150, 2.0);
const bestBytes = await readFile(best);
const worstBytes = await readFile(worst);
expect(worstBytes.subarray(0, 5).toString()).toBe("%PDF-");
expect(worstBytes.length).toBeLessThan(bestBytes.length);
} finally {
rmSync(dir, { recursive: true, force: true });
}
}, 60_000);
it("converts to grayscale (valid pdf out)", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdf-ops-"));
try {