Python Requires Chilkat v11.7.0+
Chilkat2-Python
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 Python Downloads
import sys
import chilkat2
success = False
# 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
pdf = chilkat2.Pdf()
success = pdf.LoadFile("qa_data/pdf/grand_canyon_hiking_guide.pdf")
if (success == False):
print(pdf.LastErrorText)
sys.exit()
# "audit" takes the same options as "optimize", so an empty JSON means the default options.
json = chilkat2.JsonObject()
jsonOut = chilkat2.JsonObject()
success = pdf.Optimize("audit",json,jsonOut)
if (success == False):
print(pdf.LastErrorText)
sys.exit()
print("fileSize: " + str(jsonOut.IntOf("fileSize")))
print("pdfVersion: " + jsonOut.StringOf("pdfVersion"))
print("pdfa: " + jsonOut.StringOf("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: " + str(jsonOut.IntOf("usage.images.bytes")) + " in " + str(jsonOut.IntOf("usage.images.count")) + " objects")
print("fonts: " + str(jsonOut.IntOf("usage.fonts.bytes")) + " in " + str(jsonOut.IntOf("usage.fonts.count")) + " objects")
print("pageContent: " + str(jsonOut.IntOf("usage.pageContent.bytes")))
print("formXObjects: " + str(jsonOut.IntOf("usage.formXObjects.bytes")))
print("annotations: " + str(jsonOut.IntOf("usage.annotations.bytes")))
print("structureTree: " + str(jsonOut.IntOf("usage.structureTree.bytes")))
print("metadata: " + str(jsonOut.IntOf("usage.metadata.bytes")))
print("embeddedFiles: " + str(jsonOut.IntOf("usage.embeddedFiles.bytes")))
print("oldRevisions: " + str(jsonOut.IntOf("usage.oldRevisions.bytes")))
print("overhead: " + str(jsonOut.IntOf("usage.overhead.bytes")))
# What each option is expected to save. An option that would save nothing is left out.
print("estimated total: " + str(jsonOut.IntOf("estimatedSavings.total")))
est = chilkat2.JsonObject()
success = jsonOut.ObjectOf2("estimatedSavings",est)
if (success == False):
print(jsonOut.LastErrorText)
sys.exit()
i = 0
numEst = est.Size
while i < numEst :
optionName = est.NameAt(i)
print(" " + optionName + ": " + str(est.IntOf(optionName)))
i = i + 1