public function __toString()
{
$matches = array();
// Set the content stream.
$stream = null !== $this->stream ? "stream" . $this->stream . "endstream\n" : '';
// Set up the Length definition.
if (strpos($this->def, '/Length ') !== false && strpos($this->def, '/Length1') === false && strpos($this->def, '/Image') === false) {
preg_match('/\\/Length\\s\\d*/', $this->def, $matches);
if (isset($matches[0])) {
$len = $matches[0];
$len = str_replace('/Length', '', $len);
$len = str_replace(' ', '', $len);
$this->def = str_replace($len, '[{byte_length}]', $this->def);
}
} else {
if (strpos($this->def, '/Length') === false) {
$this->def .= "<</Length [{byte_length}]>>\n";
}
}
// Calculate the byte length of the content stream and swap out the placeholders.
$byteLength = $this->compress && function_exists('gzcompress') && strpos($this->def, ' /Image') === false && strpos($this->def, '/FlateDecode') === false ? $this->calcByteLength($this->stream) . " /Filter /FlateDecode" : $this->calcByteLength($this->stream);
$data = str_replace('[{obj_index}]', $this->index, $this->data);
$data = str_replace('[{obj_stream}]', $stream, $data);
$data = str_replace('[{obj_def}]', $this->def, $data);
$data = str_replace('[{byte_length}]', $byteLength, $data);
// Clear Length definition if it is zero.
if (strpos($data, '<</Length 0>>') !== false) {
$data = str_replace('<</Length 0>>', '', $data);
}
return $data;
}