PHP ActiveX Requires Chilkat v11.7.0+
PHP ActiveX
PDF Object Streams: generate, preserve or disable
See more PDF Optimization Examples
Demonstrates the objectStreams option. An object stream packs many small objects (dictionaries, not stream data) into one compressed stream, which is usually a worthwhile saving in a document with many pages or tags. "generate" (the default) packs them and writes a cross-reference stream; "preserve" keeps the file as it is -- packed if it already was, not packed if it was not; "disable" writes every object on its own with a classic cross-reference table, which is what a PDF 1.4 reader needs. Object streams require PDF 1.5 and are skipped for PDF/A-1.Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// 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
$fac = new COM("Chilkat.FileAccess");
$json = new COM("Chilkat.JsonObject");
$jsonOut = new COM("Chilkat.JsonObject");
// generate: pack the objects (the default).
$pdf1 = new COM("Chilkat.Pdf");
$success = $pdf1->LoadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
if ($success == 0) {
print $pdf1->LastErrorText . "\n";
exit;
}
$json->UpdateString('objectStreams','generate');
$success = $pdf1->Optimize('optimize',$json,$jsonOut);
if ($success == 0) {
print $pdf1->LastErrorText . "\n";
exit;
}
$success = $pdf1->WriteFile('qa_output/grand_canyon_objstm_generate.pdf');
if ($success == 0) {
print $pdf1->LastErrorText . "\n";
exit;
}
print 'generate: ' . $fac->FileSize('qa_output/grand_canyon_objstm_generate.pdf') . ' bytes, objects packed: ' . $jsonOut->IntOf('actions.objectStreams.count') . "\n";
// disable: every object on its own, with a classic cross-reference table.
$pdf2 = new COM("Chilkat.Pdf");
$success = $pdf2->LoadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
if ($success == 0) {
print $pdf2->LastErrorText . "\n";
exit;
}
$json->UpdateString('objectStreams','disable');
$success = $pdf2->Optimize('optimize',$json,$jsonOut);
if ($success == 0) {
print $pdf2->LastErrorText . "\n";
exit;
}
$success = $pdf2->WriteFile('qa_output/grand_canyon_objstm_disable.pdf');
if ($success == 0) {
print $pdf2->LastErrorText . "\n";
exit;
}
print 'disable: ' . $fac->FileSize('qa_output/grand_canyon_objstm_disable.pdf') . ' bytes' . "\n";
// preserve: leave the file's own structure alone. Use it when the output must stay readable by
// whatever reads the input today.
$pdf3 = new COM("Chilkat.Pdf");
$success = $pdf3->LoadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
if ($success == 0) {
print $pdf3->LastErrorText . "\n";
exit;
}
$json->UpdateString('objectStreams','preserve');
$success = $pdf3->Optimize('optimize',$json,$jsonOut);
if ($success == 0) {
print $pdf3->LastErrorText . "\n";
exit;
}
$success = $pdf3->WriteFile('qa_output/grand_canyon_objstm_preserve.pdf');
if ($success == 0) {
print $pdf3->LastErrorText . "\n";
exit;
}
print 'preserve: ' . $fac->FileSize('qa_output/grand_canyon_objstm_preserve.pdf') . ' bytes' . "\n";
?>