Compress a PDF under a target size
You have a 30 MB PDF and need to email it (Gmail 25 MB limit). The compressor returns the file size of the output — call it in a loop, lowering quality until the threshold is met, capped at 4 attempts so we don't spin forever.
import { JohnsEssentials } from "@johns-essentials/sdk";
const je = new JohnsEssentials({ apiKey: process.env.JE_API_KEY });
const target = 25 * 1024 * 1024; // 25 MB
const qualities = ["high", "medium", "low", "screen"];
let out;
for (const quality of qualities) {
out = await je.tools.pdf.compress({
file: await readFile("./report.pdf"),
quality,
});
if (out.size <= target) break;
}
console.log(`Settled at ${out.quality}: ${out.size} bytes`);