PhpOffice\PhpPresentation\Writer\ODPresentation::save PHP Method

save() public method

Save PhpPresentation to file
public save ( string $pFilename )
$pFilename string
    public function save($pFilename)
    {
        if (empty($pFilename)) {
            throw new \Exception("Filename is empty");
        }
        // If $pFilename is php://output or php://stdout, make it a temporary file...
        $originalFilename = $pFilename;
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
            $pFilename = @tempnam('./', 'phppttmp');
            if ($pFilename == '') {
                $pFilename = $originalFilename;
            }
        }
        // Initialize HashTable
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
        // Initialize Zip
        $oZip = $this->getZipAdapter();
        $oZip->open($pFilename);
        // Variables
        $oPresentation = $this->getPhpPresentation();
        $arrayChart = array();
        $arrayFiles = array();
        $oDir = new DirectoryIterator(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ODPresentation');
        foreach ($oDir as $oFile) {
            if (!$oFile->isFile()) {
                continue;
            }
            $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
            $o = new \ReflectionClass($class);
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\AbstractDecoratorWriter')) {
                continue;
            }
            $arrayFiles[$oFile->getBasename('.php')] = $o;
        }
        ksort($arrayFiles);
        foreach ($arrayFiles as $o) {
            $oService = $o->newInstance();
            $oService->setZip($oZip);
            $oService->setPresentation($oPresentation);
            $oService->setDrawingHashTable($this->getDrawingHashTable());
            $oService->setArrayChart($arrayChart);
            $oZip = $oService->render();
            $arrayChart = $oService->getArrayChart();
            unset($oService);
        }
        // Close file
        $oZip->close();
        // If a temporary file was used, copy it to the correct file stream
        if ($originalFilename != $pFilename) {
            if (copy($pFilename, $originalFilename) === false) {
                throw new \Exception("Could not copy temporary zip file {$pFilename} to {$originalFilename}.");
            }
            if (@unlink($pFilename) === false) {
                throw new \Exception('The file ' . $pFilename . ' could not be removed.');
            }
        }
    }

Usage Example

 /**
  * Test get PhpPresentation exception
  *
  * @expectedException Exception
  * @expectedExceptionMessage Filename is empty
  */
 public function testSaveEmpty()
 {
     $object = new ODPresentation();
     $object->save('');
 }