PDF Compress — C++ source
Shrink PDF file sizes in your browser — lossless optimization plus a JPEG quality slider for embedded images. Nothing uploads.
This is the C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.
// pdf-compress — two-pass, fully client-side PDF compression. C++ port
// (canonical TS: src/lib/pdf-compress.ts; Go twin: cli/pdf-compress).
// Honest scope note: C++ has no idiomatic PDF library to call, so like the
// self-contained Go CLI this port ships the pure decision and error
// classification helpers verbatim plus a raw-buffer load guard that refuses
// garbage and /Encrypt-carrying inputs exactly like the TS load path. The
// full object-graph walk (Resources -> XObject -> /Image with /Filter
// /DCTDecode, re-encode at identical pixel dimensions) stays a documented
// extension point behind compressPdf().
#include <optional>
#include <string>
namespace pdfcompress {
// Load failure kind: "encrypted" (password-protected) or "invalid"
// (unparseable) — the C++ twin of the TS PdfToolError.code.
struct PdfToolError {
std::string code;
std::string message;
};
// ascii case-insensitive substring: the /encrypt|password/i heuristic.
static bool containsIgnoreCase(const std::string &hay, const std::string &needle) {
if (needle.empty()) return true;
auto lower = [](char c) {
return c >= 'A' && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c;
};
for (size_t i = 0; i + needle.size() <= hay.size(); i++) {
size_t j = 0;
while (j < needle.size() && lower(hay[i + j]) == lower(needle[j])) j++;
if (j == needle.size()) return true;
}
return false;
}
/** Pure classifier — encrypted vs invalid, from the raw error message.
* Mirrors classifyLoadError() in src/lib/pdf-errors.ts; nullopt (the
* TS `undefined`) classifies as invalid. */
std::string classifyLoadError(const std::optional<std::string> &msg) {
if (msg && (containsIgnoreCase(*msg, "encrypt") || containsIgnoreCase(*msg, "password")))
return "encrypted";
return "invalid";
}
/** Map a thrown load/parse error to a user-facing PdfToolError. */
PdfToolError mapError(const std::optional<std::string> &msg) {
const std::string kind = classifyLoadError(msg);
return {kind, kind == "encrypted" ? "This PDF is password-protected. Unlock it first."
: "Not a readable PDF file."};
}
/** Pure decision: only baseline single-stream JPEGs (/Filter /DCTDecode)
* are recompressible — anything else (Flate, multi-filter chains like
* DCTDecode,FlateDecode, no filter) stays byte-for-byte intact. Ported
* verbatim from chooseImageAction() in src/lib/pdf-compress.ts. */
std::string chooseImageAction(const std::string &filter) {
return filter == "DCTDecode" ? "recompress" : "skip";
}
/** Per-image outcome: before/after stream sizes, or why the original
* stayed (after == nullopt, skipped = "grew" | "not-jpeg" |
* "decode-failed"). */
struct ImageAccounting {
size_t index;
size_t before;
std::optional<size_t> after;
std::optional<std::string> skipped;
};
/** Raw-buffer load guard: the self-contained stand-in for
* PDFDocument.load + mapError. Not a %PDF header -> invalid; an /Encrypt
* entry anywhere -> encrypted; otherwise ok (empty string). */
std::string classifyBytes(const std::string &data) {
if (data.size() < 5 || data.compare(0, 5, "%PDF-") != 0) return "invalid";
if (data.find("/Encrypt") != std::string::npos) return "encrypted";
return "ok";
}
/** Pass 1 (lossless floor) + pass 2 (JPEG walk) are display-level here:
* a real port needs a PDF object-graph parser and a JPEG re-encoder, the
* same honest scope cut the Go CLI twin makes. classifyBytes above is the
* load half; chooseImageAction is the per-XObject decision half. */
inline const char *compressPdfScopeNote() {
return "pass 1/2 need a parser + JPEG re-encoder; helpers above are the verbatim port";
}
} // namespace pdfcompress
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →