PDF Compress — Zig source
Shrink PDF file sizes in your browser — lossless optimization plus a JPEG quality slider for embedded images. Nothing uploads.
This is the Zig implementation — the same logic the interactive tool runs, in a shareable, citable form.
//! pdf-compress — two-pass, fully client-side PDF compression. Zig port
//! (canonical TS: src/lib/pdf-compress.ts; Go twin: cli/pdf-compress).
//! Honest scope note: Zig 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 compress().
const std = @import("std");
/// Load failure kind: "encrypted" (password-protected) or "invalid"
/// (unparseable) — the Zig twin of the TS PdfToolError.code.
pub const PdfToolError = struct {
code: []const u8,
message: []const u8,
};
/// ascii case-insensitive substring: the /encrypt|password/i heuristic.
fn containsIgnoreCase(hay: []const u8, needle: []const u8) bool {
if (needle.len == 0) return true;
if (hay.len < needle.len) return false;
var i: usize = 0;
while (i + needle.len <= hay.len) : (i += 1) {
var j: usize = 0;
while (j < needle.len and std.ascii.toLower(hay[i + j]) == std.ascii.toLower(needle[j])) j += 1;
if (j == needle.len) return true;
}
return false;
}
/// Pure classifier — encrypted vs invalid, from the raw error message.
/// Mirrors classifyLoadError() in src/lib/pdf-errors.ts; null (the TS
/// undefined) classifies as invalid.
pub fn classifyLoadError(msg: ?[]const u8) []const u8 {
if (msg) |m| {
if (containsIgnoreCase(m, "encrypt") or containsIgnoreCase(m, "password"))
return "encrypted";
}
return "invalid";
}
/// Map a thrown load/parse error to a user-facing PdfToolError.
pub fn mapError(msg: ?[]const u8) PdfToolError {
const kind = classifyLoadError(msg);
return .{
.code = kind,
.message = if (std.mem.eql(u8, kind, "encrypted"))
"This PDF is password-protected. Unlock it first."
else
"Not a readable PDF file.",
};
}
/// Per-image outcome: before/after stream sizes, or why the original
/// stayed (after == null, skipped = "grew" | "not-jpeg" | "decode-failed").
pub const ImageAccounting = struct {
index: usize,
before: usize,
after: ?usize = null,
skipped: ?[]const u8 = null,
};
/// 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.
pub fn chooseImageAction(filter: []const u8) []const u8 {
return if (std.mem.eql(u8, filter, "DCTDecode")) "recompress" else "skip";
}
/// Raw-buffer load guard: the self-contained stand-in for PDFDocument.load
/// + mapError. Not a %PDF header -> "invalid"; an /Encrypt entry anywhere
/// in the document -> "encrypted"; otherwise "ok".
pub fn classifyBytes(data: []const u8) []const u8 {
if (data.len < 5 or !std.mem.eql(u8, data[0..5], "%PDF-")) return "invalid";
if (std.mem.indexOf(u8, data, "/Encrypt") != null) 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.
pub const compress_scope_note =
"pass 1/2 need a parser + JPEG re-encoder; helpers above are the verbatim port";
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 →