/** * Object destructor */ public function __destruct() { $this->_stringParser->cleanUp(); }
/** * Object constructor * * Note: PHP duplicates string, which is sent by value, only of it's updated. * Thus we don't need to care about overhead * * @param mixed $source * @param \Zend\Pdf\ObjectFactory $factory * @param boolean $load * @throws \Zend\Exception */ public function __construct($source, Pdf\ObjectFactory $factory, $load) { if ($load) { if (($pdfFile = @fopen($source, 'rb')) === false) { throw new Pdf\Exception("Can not open '{$source}' file for reading."); } $byteCount = filesize($source); $data = fread($pdfFile, $byteCount); $byteCount -= strlen($data); while ($byteCount > 0 && ($nextBlock = fread($pdfFile, $byteCount)) != false) { $data .= $nextBlock; $byteCount -= strlen($nextBlock); } fclose($pdfFile); $this->_stringParser = new DataParser($data, $factory); } else { $this->_stringParser = new DataParser($source, $factory); } $pdfVersionComment = $this->_stringParser->readComment(); if (substr($pdfVersionComment, 0, 5) != '%PDF-') { throw new Pdf\Exception('File is not a PDF.'); } $pdfVersion = substr($pdfVersionComment, 5); if (version_compare($pdfVersion, '0.9', '<') || version_compare($pdfVersion, '1.61', '>=')) { /** * @todo * To support PDF versions 1.5 (Acrobat 6) and PDF version 1.7 (Acrobat 7) * Stream compression filter must be implemented (for compressed object streams). * Cross reference streams must be implemented */ throw new Pdf\Exception(sprintf('Unsupported PDF version. Zend_PDF supports PDF 1.0-1.4. Current version - \'%f\'', $pdfVersion)); } $this->_pdfVersion = $pdfVersion; $this->_stringParser->offset = strrpos($this->_stringParser->data, '%%EOF'); if ($this->_stringParser->offset === false || strlen($this->_stringParser->data) - $this->_stringParser->offset > 7) { throw new Pdf\Exception('PDF file syntax error. End-of-fle marker expected at the end of file.'); } $this->_stringParser->offset--; /** * Go to end of cross-reference table offset */ while (DataParser::isWhiteSpace(ord($this->_stringParser->data[$this->_stringParser->offset])) && $this->_stringParser->offset > 0) { $this->_stringParser->offset--; } /** * Go to the start of cross-reference table offset */ while (!DataParser::isWhiteSpace(ord($this->_stringParser->data[$this->_stringParser->offset])) && $this->_stringParser->offset > 0) { $this->_stringParser->offset--; } /** * Go to the end of 'startxref' keyword */ while (DataParser::isWhiteSpace(ord($this->_stringParser->data[$this->_stringParser->offset])) && $this->_stringParser->offset > 0) { $this->_stringParser->offset--; } /** * Go to the white space (eol marker) before 'startxref' keyword */ $this->_stringParser->offset -= 9; $nextLexeme = $this->_stringParser->readLexeme(); if ($nextLexeme != 'startxref') { throw new Pdf\Exception(sprintf('PDF file syntax error. \'startxref\' keyword expected. Offset - 0x%X.', $this->_stringParser->offset - strlen($nextLexeme))); } $startXref = $this->_stringParser->readLexeme(); if (!ctype_digit($startXref)) { throw new Pdf\Exception(sprintf('PDF file syntax error. Cross-reference table offset must contain only digits. Offset - 0x%X.', $this->_stringParser->offset - strlen($nextLexeme))); } $this->_trailer = $this->_loadXRefTable($startXref); $factory->setObjectCount($this->_trailer->Size->value); }