/** * @param array $options that will get processed using a OptionsResolver * <ul> * <li>'from' => (int) page number,</li> * <li>'to' => (int) page number,</li> * <li>'format' => (string) Returns document in the specified format,</li> * <li>'storageName' => (string) Name of the storage,</li> * <li>'folder' => (string) Name of the folder,</li> * <li>'zipOutput' => (bool) save the results as .zip file,</li> * </ul> * * @return string|boolean * @throws Exception|InvalidOptionsException * @see OptionsResolver */ public function splitDocument(array $options = array()) { $strURI = Product::$baseProductUri . '/words/' . $this->getFileName() . '/split?'; $resolver = new OptionsResolver(); $resolver->setDefined(array('from', 'to', 'format', 'storage', 'folder', 'zipOutput')); $options = $resolver->resolve($options); $strURI .= http_build_query($options); AsposeApp::getLogger()->info('WordsDocument splitDocument call will be made', array('call-uri' => $strURI)); $signedURI = Utils::sign($strURI); $responseStream = Utils::processCommand($signedURI, 'POST', '', ''); $json = json_decode($responseStream); if ($json->Code == 200) { // Just return the json in case of a zip result if (isset($options['zipOutput'])) { AsposeApp::getLogger()->info('zipOutput found, so return entire splitResult'); return $json->SplitResult; } AsposeApp::getLogger()->info('Separately save each of the split pages'); $dispatcher = AsposeApp::getEventDispatcher(); foreach ($json->SplitResult->Pages as $pageNumber => $splitPage) { $splitFileName = basename($splitPage->Href); //build URI to download split slides $strURI = Product::$baseProductUri . '/storage/file/' . $splitFileName; //sign URI $signedURI = Utils::Sign($strURI); $responseStream = Utils::processCommand($signedURI, "GET", "", ""); //save split slides $outputFile = AsposeApp::$outPutLocation . $splitFileName; Utils::saveFile($responseStream, $outputFile); $event = new SplitPageEvent($outputFile, $pageNumber + 1); $dispatcher->dispatch(SplitPageEvent::PAGE_IS_SPLIT, $event); } return $json->SplitResult->Pages; } AsposeApp::getLogger()->warning('Error occured, http 200 code was not found.', array('json-code' => $json->Code)); return false; }
/** * Split pages to specified format. * * @param integer $from From page number. * @param integer $to To page number. * @param string $format Returns file in the specified format. * * @return string Returns the file path. * @throws Exception */ public function splitPagesToAnyFormat($from, $to, $format) { $strURI = Product::$baseProductUri . '/pdf/' . $this->getFileName() . '/split?from=' . $from . '&to=' . $to . '&format=' . $format; $signedURI = Utils::sign($strURI); $responseStream = Utils::processCommand($signedURI, 'POST', '', ''); $json = json_decode($responseStream); $dispatcher = AsposeApp::getEventDispatcher(); $pageNumber = 1; foreach ($json->Result->Documents as $splitPage) { $splitFileName = basename($splitPage->Href); $strURI = Product::$baseProductUri . '/storage/file/' . $splitFileName; $signedURI = Utils::sign($strURI); $responseStream = Utils::processCommand($signedURI, 'GET', '', ''); $fileName = $this->getFileName() . '_' . $pageNumber . '.' . $format; $outputFile = AsposeApp::$outPutLocation . $fileName; Utils::saveFile($responseStream, $outputFile); echo $outputFile . '<br />'; $event = new SplitPageEvent($outputFile, $pageNumber); $dispatcher->dispatch(SplitPageEvent::PAGE_IS_SPLIT, $event); $pageNumber++; } }
/** * Check or the result does not contain an error message. If $result is invalid it contains the error message * @param $result * @return string */ public static function validateOutput($result, $saveFormat = '') { $result = (string) $result; $validate = array('Unknown file format.', 'Unable to read beyond the end of the stream', 'Index was out of range', 'Cannot read that as a ZipFile', 'Not a Microsoft PowerPoint 2007 presentation', 'Index was outside the bounds of the array', 'An attempt was made to move the position before the beginning of the stream', "Format '{$saveFormat}' is not supported."); $invalid = 0; foreach ($validate as $key => $value) { $pos = strpos($result, $value); if ($pos === 1 || $pos === 16) { $invalid = true; } } // Event can be used to perform extra validation on the result $dispatcher = AsposeApp::getEventDispatcher(); $event = new ValidateOutputEvent($result, $invalid); /** @var ValidateOutputEvent $dispatchedEvent */ $dispatchedEvent = $dispatcher->dispatch(ValidateOutputEvent::VALIDATE_OUTPUT, $event); // If the output is invalid it contains the error message if ($dispatchedEvent->isInvalid() === true) { return $result; } else { return ''; // FIXME returning an empty string here is really weird } }