Zig Requires Chilkat v11.7.0+
Zig
Where a PDF's Bytes Go: the Optimize Audit
See more PDF Optimization Examples
Demonstrates the "audit" operation, which measures the loaded PDF and changes nothing. The report says how large the file is, which PDF version it claims, whether it claims to be PDF/A, where its bytes go ("usage", as stored in the file, so compressed sizes), and how many bytes each lossless option would be expected to save ("estimatedSavings"). Use it to decide whether optimizing is worth the trouble, and which options matter for this file.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at grand_canyon_hiking_guide.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// "audit" takes the same options as "optimize", so an empty JSON means the default options.
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
pdf.optimize("audit", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("fileSize: {d}\n", .{json_out.intOf("fileSize")});
std.debug.print("pdfVersion: {s}\n", .{try json_out.stringOf(alloc, "pdfVersion")});
std.debug.print("pdfa: {s}\n", .{try json_out.stringOf(alloc, "pdfa")});
// Where the bytes go. Every category is present even when it is 0, and the sizes are the stored
// (compressed) sizes, so they add up to fileSize. "overhead" is what is left: the cross-reference
// tables, the object headers and the white space between objects.
std.debug.print("images: {d} in {d} objects\n", .{ json_out.intOf("usage.images.bytes"), json_out.intOf("usage.images.count") });
std.debug.print("fonts: {d} in {d} objects\n", .{ json_out.intOf("usage.fonts.bytes"), json_out.intOf("usage.fonts.count") });
std.debug.print("pageContent: {d}\n", .{json_out.intOf("usage.pageContent.bytes")});
std.debug.print("formXObjects: {d}\n", .{json_out.intOf("usage.formXObjects.bytes")});
std.debug.print("annotations: {d}\n", .{json_out.intOf("usage.annotations.bytes")});
std.debug.print("structureTree: {d}\n", .{json_out.intOf("usage.structureTree.bytes")});
std.debug.print("metadata: {d}\n", .{json_out.intOf("usage.metadata.bytes")});
std.debug.print("embeddedFiles: {d}\n", .{json_out.intOf("usage.embeddedFiles.bytes")});
std.debug.print("oldRevisions: {d}\n", .{json_out.intOf("usage.oldRevisions.bytes")});
std.debug.print("overhead: {d}\n", .{json_out.intOf("usage.overhead.bytes")});
// What each option is expected to save. An option that would save nothing is left out.
std.debug.print("estimated total: {d}\n", .{json_out.intOf("estimatedSavings.total")});
const est = try chilkat.JsonObject.init();
defer est.deinit();
json_out.objectOf2("estimatedSavings", est) catch {
std.debug.print("{s}\n", .{try json_out.getLastErrorText(alloc)});
return;
};
var i: i32 = 0;
const num_est = est.getSize();
while (i < num_est) {
const option_name = try est.nameAt(alloc, i);
std.debug.print(" {s}: {d}\n", .{ option_name, est.intOf(option_name) });
i = i + 1;
}
}