Lua Requires Chilkat v11.7.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- 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
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- "audit" takes the same options as "optimize", so an empty JSON means the default options.
local json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
success = pdf:optimize("audit", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("fileSize: " .. jsonOut:int_of("fileSize"))
print("pdfVersion: " .. jsonOut:string_of("pdfVersion"))
print("pdfa: " .. jsonOut:string_of("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.
print("images: " .. jsonOut:int_of("usage.images.bytes") .. " in " .. jsonOut:int_of("usage.images.count") .. " objects")
print("fonts: " .. jsonOut:int_of("usage.fonts.bytes") .. " in " .. jsonOut:int_of("usage.fonts.count") .. " objects")
print("pageContent: " .. jsonOut:int_of("usage.pageContent.bytes"))
print("formXObjects: " .. jsonOut:int_of("usage.formXObjects.bytes"))
print("annotations: " .. jsonOut:int_of("usage.annotations.bytes"))
print("structureTree: " .. jsonOut:int_of("usage.structureTree.bytes"))
print("metadata: " .. jsonOut:int_of("usage.metadata.bytes"))
print("embeddedFiles: " .. jsonOut:int_of("usage.embeddedFiles.bytes"))
print("oldRevisions: " .. jsonOut:int_of("usage.oldRevisions.bytes"))
print("overhead: " .. jsonOut:int_of("usage.overhead.bytes"))
-- What each option is expected to save. An option that would save nothing is left out.
print("estimated total: " .. jsonOut:int_of("estimatedSavings.total"))
local est = chilkat.JsonObject.new()
success = jsonOut:object_of2("estimatedSavings", est)
if not success then
print(jsonOut.last_error_text)
return
end
local i = 0
local numEst = est.size
while i < numEst do
local optionName = est:name_at(i)
print(" " .. optionName .. ": " .. est:int_of(optionName))
i = i + 1
end