public function finalize()
{
$this->output = null;
// Define some variables and initialize the trailer.
$numObjs = count($this->objects) + 1;
$this->trailer = "xref\n0 {$numObjs}\n0000000000 65535 f \n";
// Calculate the root object lead off.
$byteLength = $this->calcByteLength($this->objects[$this->root]);
$this->bytelength += $byteLength;
$this->trailer .= $this->formatByteLength($this->bytelength) . " 00000 n \n";
$this->output .= $this->objects[$this->root];
// Loop through the rest of the objects, calculate their size and length for the xref table and add their data to the output.
foreach ($this->objects as $obj) {
if ($obj->index != $this->root) {
if ($obj instanceof Object\Object && $this->compress && !$obj->isPalette() && !$obj->isCompressed()) {
$obj->compress();
}
$byteLength = $this->calcByteLength($obj);
$this->bytelength += $byteLength;
$this->trailer .= $this->formatByteLength($this->bytelength) . " 00000 n \n";
$this->output .= $obj;
}
}
// Finalize the trailer.
$this->trailer .= "trailer\n<</Size {$numObjs}/Root {$this->root} 0 R/Info {$this->info} 0 R>>\nstartxref\n" . ($this->bytelength + 68) . "\n%%EOF";
// Append the trailer to the final output.
$this->output .= $this->trailer;
// Write to the file.
$this->write($this->output);
return $this;
}