public function testPresentationProperties() { $object = new PhpPresentation(); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PresentationProperties', $object->getPresentationProperties()); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->setPresentationProperties(new PresentationProperties())); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PresentationProperties', $object->getPresentationProperties()); }
public function testFeatureThumbnail() { $imagePath = PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'PhpPresentationLogo.png'; $oPhpPresentation = new PhpPresentation(); $oPhpPresentation->getPresentationProperties()->setThumbnailPath($imagePath); $oXMLDoc = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007'); $this->assertTrue($oXMLDoc->fileExists('docProps/thumbnail.jpeg')); }
public function testLastView() { $expectedElement = '/p:viewPr'; $expectedLastView = PresentationProperties::VIEW_OUTLINE; $oPhpPresentation = new PhpPresentation(); $oPhpPresentation->getPresentationProperties()->setLastView($expectedLastView); $oXMLDoc = TestHelperDOCX::getDocument($oPhpPresentation, 'PowerPoint2007'); $this->assertTrue($oXMLDoc->fileExists('ppt/viewProps.xml')); $this->assertTrue($oXMLDoc->elementExists($expectedElement, 'ppt/viewProps.xml')); $this->assertEquals($expectedLastView, $oXMLDoc->getElementAttribute($expectedElement, 'lastView', 'ppt/viewProps.xml')); }
/** * Read View Properties * @param string $sPart */ protected function loadViewProperties($sPart) { $xmlReader = new XMLReader(); if ($xmlReader->getDomFromString($sPart)) { $pathZoom = '/p:viewPr/p:slideViewPr/p:cSldViewPr/p:cViewPr/p:scale/a:sx'; if (is_object($oElement = $xmlReader->getElement($pathZoom))) { if ($oElement->hasAttribute('d') && $oElement->hasAttribute('n')) { $this->oPhpPresentation->getPresentationProperties()->setZoom($oElement->getAttribute('n') / $oElement->getAttribute('d')); } } } }
/** * Write ppt/presProps.xml to XML format * * @return string XML Output * @throws \Exception */ public function writePresProps(PhpPresentation $pPhpPresentation) { $presentationPpts = $pPhpPresentation->getPresentationProperties(); // Create XML writer $objWriter = $this->getXMLWriter(); // XML header $objWriter->startDocument('1.0', 'UTF-8', 'yes'); // p:presentationPr $objWriter->startElement('p:presentationPr'); $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main'); $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main'); // p:presentationPr > p:showPr if ($presentationPpts->isLoopContinuouslyUntilEsc()) { $objWriter->startElement('p:showPr'); $objWriter->writeAttribute('loop', '1'); $objWriter->endElement(); } // p:extLst $objWriter->startElement('p:extLst'); // p:ext $objWriter->startElement('p:ext'); $objWriter->writeAttribute('uri', '{E76CE94A-603C-4142-B9EB-6D1370010A27}'); // p14:discardImageEditData $objWriter->startElement('p14:discardImageEditData'); $objWriter->writeAttribute('xmlns:p14', 'http://schemas.microsoft.com/office/powerpoint/2010/main'); $objWriter->writeAttribute('val', '0'); $objWriter->endElement(); // > p:ext $objWriter->endElement(); // p:ext $objWriter->startElement('p:ext'); $objWriter->writeAttribute('uri', '{D31A062A-798A-4329-ABDD-BBA856620510}'); // p14:defaultImageDpi $objWriter->startElement('p14:defaultImageDpi'); $objWriter->writeAttribute('xmlns:p14', 'http://schemas.microsoft.com/office/powerpoint/2010/main'); $objWriter->writeAttribute('val', '220'); $objWriter->endElement(); // > p:ext $objWriter->endElement(); // > p:extLst $objWriter->endElement(); // > p:presentationPr $objWriter->endElement(); return $objWriter->getData(); }
<?php include_once 'Sample_Header.php'; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Style\Alignment; use PhpOffice\PhpPresentation\Style\Color; // Create new PHPPresentation object echo date('H:i:s') . ' Create new PHPPresentation object' . EOL; $objPHPPresentation = new PhpPresentation(); // Set the zoom to 200% $objPHPPresentation->getPresentationProperties()->setZoom(3); // Create slide echo date('H:i:s') . ' Create slide' . EOL; $currentSlide = $objPHPPresentation->getActiveSlide(); $currentSlide->addShape(clone $oShapeDrawing); $currentSlide->addShape(clone $oShapeRichText); // Save file echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers); if (!CLI) { include_once 'Sample_Footer.php'; }
/** * Write content file to XML format * * @param PhpPresentation $pPhpPresentation * @return string XML Output * @throws \Exception */ public function writePart(PhpPresentation $pPhpPresentation) { // Create XML writer $objWriter = $this->getXMLWriter(); // XML header $objWriter->startDocument('1.0', 'UTF-8'); // office:document-content $objWriter->startElement('office:document-content'); $objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); $objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); $objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); $objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); $objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); $objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); $objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); $objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); $objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); $objWriter->writeAttribute('xmlns:presentation', 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0'); $objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); $objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0'); $objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0'); $objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML'); $objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0'); $objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0'); $objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); $objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer'); $objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc'); $objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events'); $objWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms'); $objWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'); $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $objWriter->writeAttribute('xmlns:smil', 'urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0'); $objWriter->writeAttribute('xmlns:anim', 'urn:oasis:names:tc:opendocument:xmlns:animation:1.0'); $objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report'); $objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2'); $objWriter->writeAttribute('xmlns:rdfa', 'http://docs.oasis-open.org/opendocument/meta/rdfa#'); $objWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0'); $objWriter->writeAttribute('office:version', '1.2'); // office:automatic-styles $objWriter->startElement('office:automatic-styles'); $this->shapeId = 0; $incSlide = 0; foreach ($pPhpPresentation->getAllSlides() as $pSlide) { // Slides $this->writeStyleSlide($objWriter, $pSlide, $incSlide); // Images $shapes = $pSlide->getShapeCollection(); foreach ($shapes as $shape) { // Increment $this->shapeId ++$this->shapeId; // Check type if ($shape instanceof RichText) { $this->writeTxtStyle($objWriter, $shape); } if ($shape instanceof AbstractDrawing) { $this->writeDrawingStyle($objWriter, $shape); } if ($shape instanceof Line) { $this->writeLineStyle($objWriter, $shape); } if ($shape instanceof Table) { $this->writeTableStyle($objWriter, $shape); } if ($shape instanceof Group) { $this->writeGroupStyle($objWriter, $shape); } } $incSlide++; } // Style : Bullet if (!empty($this->arrStyleBullet)) { foreach ($this->arrStyleBullet as $key => $item) { $oStyle = $item['oStyle']; $arrLevel = explode(';', $item['level']); // style:style $objWriter->startElement('text:list-style'); $objWriter->writeAttribute('style:name', 'L_' . $key); foreach ($arrLevel as $level) { if ($level != '') { $oAlign = $item['oAlign_' . $level]; // text:list-level-style-bullet $objWriter->startElement('text:list-level-style-bullet'); $objWriter->writeAttribute('text:level', $level + 1); $objWriter->writeAttribute('text:bullet-char', $oStyle->getBulletChar()); // style:list-level-properties $objWriter->startElement('style:list-level-properties'); if ($oAlign->getIndent() < 0) { $objWriter->writeAttribute('text:space-before', CommonDrawing::pixelsToCentimeters($oAlign->getMarginLeft() - -1 * $oAlign->getIndent()) . 'cm'); $objWriter->writeAttribute('text:min-label-width', CommonDrawing::pixelsToCentimeters(-1 * $oAlign->getIndent()) . 'cm'); } else { $objWriter->writeAttribute('text:space-before', CommonDrawing::pixelsToCentimeters($oAlign->getMarginLeft() - $oAlign->getIndent()) . 'cm'); $objWriter->writeAttribute('text:min-label-width', CommonDrawing::pixelsToCentimeters($oAlign->getIndent()) . 'cm'); } $objWriter->endElement(); // style:text-properties $objWriter->startElement('style:text-properties'); $objWriter->writeAttribute('fo:font-family', $oStyle->getBulletFont()); $objWriter->writeAttribute('style:font-family-generic', 'swiss'); $objWriter->writeAttribute('style:use-window-font-color', 'true'); $objWriter->writeAttribute('fo:font-size', '100'); $objWriter->endElement(); $objWriter->endElement(); } } $objWriter->endElement(); } } // Style : Paragraph if (!empty($this->arrStyleParagraph)) { foreach ($this->arrStyleParagraph as $key => $item) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'P_' . $key); $objWriter->writeAttribute('style:family', 'paragraph'); // style:paragraph-properties $objWriter->startElement('style:paragraph-properties'); switch ($item->getAlignment()->getHorizontal()) { case Alignment::HORIZONTAL_LEFT: $objWriter->writeAttribute('fo:text-align', 'left'); break; case Alignment::HORIZONTAL_RIGHT: $objWriter->writeAttribute('fo:text-align', 'right'); break; case Alignment::HORIZONTAL_CENTER: $objWriter->writeAttribute('fo:text-align', 'center'); break; case Alignment::HORIZONTAL_JUSTIFY: $objWriter->writeAttribute('fo:text-align', 'justify'); break; case Alignment::HORIZONTAL_DISTRIBUTED: $objWriter->writeAttribute('fo:text-align', 'justify'); break; default: $objWriter->writeAttribute('fo:text-align', 'left'); break; } $objWriter->endElement(); $objWriter->endElement(); } } // Style : Text : Font if (!empty($this->arrStyleTextFont)) { foreach ($this->arrStyleTextFont as $key => $item) { // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', 'T_' . $key); $objWriter->writeAttribute('style:family', 'text'); // style:text-properties $objWriter->startElement('style:text-properties'); $objWriter->writeAttribute('fo:color', '#' . $item->getColor()->getRGB()); $objWriter->writeAttribute('fo:font-family', $item->getName()); $objWriter->writeAttribute('fo:font-size', $item->getSize() . 'pt'); // @todo : fo:font-style if ($item->isBold()) { $objWriter->writeAttribute('fo:font-weight', 'bold'); } // @todo : style:text-underline-style $objWriter->endElement(); $objWriter->endElement(); } } $objWriter->endElement(); //=============================================== // Body //=============================================== // office:body $objWriter->startElement('office:body'); // office:presentation $objWriter->startElement('office:presentation'); // Write slides $slideCount = $pPhpPresentation->getSlideCount(); $this->shapeId = 0; for ($i = 0; $i < $slideCount; ++$i) { $pSlide = $pPhpPresentation->getSlide($i); $objWriter->startElement('draw:page'); $name = $pSlide->getName(); if (!is_null($name)) { $objWriter->writeAttribute('draw:name', $name); } $objWriter->writeAttribute('draw:master-page-name', 'Standard'); $objWriter->writeAttribute('draw:style-name', 'stylePage' . $i); // Images $shapes = $pSlide->getShapeCollection(); foreach ($shapes as $shape) { // Increment $this->shapeId ++$this->shapeId; // Check type if ($shape instanceof RichText) { $this->writeShapeTxt($objWriter, $shape); } elseif ($shape instanceof Table) { $this->writeShapeTable($objWriter, $shape); } elseif ($shape instanceof Line) { $this->writeShapeLine($objWriter, $shape); } elseif ($shape instanceof Chart) { $this->writeShapeChart($objWriter, $shape); } elseif ($shape instanceof AbstractDrawing) { $this->writeShapePic($objWriter, $shape); } elseif ($shape instanceof Group) { $this->writeShapeGroup($objWriter, $shape); } } // Slide Note if ($pSlide->getNote() instanceof Note) { $this->writeSlideNote($objWriter, $pSlide->getNote()); } $objWriter->endElement(); } if ($pPhpPresentation->getPresentationProperties()->isLoopContinuouslyUntilEsc()) { $objWriter->startElement('presentation:settings'); $objWriter->writeAttribute('presentation:endless', 'true'); $objWriter->writeAttribute('presentation:pause', 'P0s'); $objWriter->writeAttribute('presentation:mouse-visible', 'false'); $objWriter->endElement(); } // > office:presentation $objWriter->endElement(); // > office:body $objWriter->endElement(); // > office:document-content $objWriter->endElement(); // Return return $objWriter->getData(); }
/** * Save PhpPresentation to file * * @param string $pFilename * @throws \Exception */ public function save($pFilename) { if (empty($pFilename)) { throw new \Exception("Filename is empty"); } if (!is_null($this->presentation)) { // 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; } } $writerPartChart = $this->getWriterPart('charts'); if (!$writerPartChart instanceof ObjectsChart) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\ObjectsChart'); } $writerPartContent = $this->getWriterPart('content'); if (!$writerPartContent instanceof Content) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\Content'); } $writerPartDrawing = $this->getWriterPart('Drawing'); if (!$writerPartDrawing instanceof Drawing) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\Drawing'); } $writerPartManifest = $this->getWriterPart('manifest'); if (!$writerPartManifest instanceof Manifest) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\Manifest'); } $writerPartMeta = $this->getWriterPart('meta'); if (!$writerPartMeta instanceof Meta) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\Meta'); } $writerPartMimetype = $this->getWriterPart('mimetype'); if (!$writerPartMimetype instanceof Mimetype) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\Mimetype'); } $writerPartStyles = $this->getWriterPart('styles'); if (!$writerPartStyles instanceof Styles) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Writer\\ODPresentation\\Styles'); } // Create drawing dictionary $this->drawingHashTable->addFromSource($writerPartDrawing->allDrawings($this->presentation)); // Create new ZIP file and open it for writing $objZip = new \ZipArchive(); // Try opening the ZIP file if ($objZip->open($pFilename, \ZIPARCHIVE::OVERWRITE) !== true) { if ($objZip->open($pFilename, \ZIPARCHIVE::CREATE) !== true) { throw new \Exception("Could not open " . $pFilename . " for writing."); } } // Add mimetype to ZIP file //@todo Not in ZIPARCHIVE::CM_STORE mode $objZip->addFromString('mimetype', $writerPartMimetype->writePart()); // Add content.xml to ZIP file $objZip->addFromString('content.xml', $writerPartContent->writePart($this->presentation)); // Add meta.xml to ZIP file $objZip->addFromString('meta.xml', $writerPartMeta->writePart($this->presentation)); // Add styles.xml to ZIP file $objZip->addFromString('styles.xml', $writerPartStyles->writePart($this->presentation)); // Add META-INF/manifest.xml $objZip->addFromString('META-INF/manifest.xml', $writerPartManifest->writePart()); // Add Thumbnail if ($this->presentation->getPresentationProperties()->getThumbnailPath()) { $pathThumbnail = $this->presentation->getPresentationProperties()->getThumbnailPath(); // Size : 128x128 pixel // PNG : 8bit, non-interlaced with full alpha transparency $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail)); if ($gdImage) { list($width, $height) = getimagesize($pathThumbnail); $gdRender = imagecreatetruecolor(128, 128); $colorBgAlpha = imagecolorallocatealpha($gdRender, 0, 0, 0, 127); imagecolortransparent($gdRender, $colorBgAlpha); imagefill($gdRender, 0, 0, $colorBgAlpha); imagecopyresampled($gdRender, $gdImage, 0, 0, 0, 0, 128, 128, $width, $height); imagetruecolortopalette($gdRender, false, 255); imagesavealpha($gdRender, true); ob_start(); imagepng($gdRender); $imageContents = ob_get_contents(); ob_end_clean(); imagedestroy($gdRender); imagedestroy($gdImage); $objZip->addFromString('Thumbnails/thumbnail.png', $imageContents); } } // Add charts foreach ($this->chartArray as $keyChart => $shapeChart) { $arrayFile = $writerPartChart->writePart($shapeChart); foreach ($arrayFile as $file => $content) { if (!empty($content)) { $objZip->addFromString('Object ' . $keyChart . '/' . $file, $content); } } } // Add media $arrMedia = array(); for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { $shape = $this->getDrawingHashTable()->getByIndex($i); if (!$shape instanceof AbstractDrawing) { throw new \Exception('The $parentWriter is not an instance of \\PhpOffice\\PhpPresentation\\Shape\\AbstractDrawing'); } if ($shape instanceof ShapeDrawing) { if (!in_array(md5($shape->getPath()), $arrMedia)) { $arrMedia[] = md5($shape->getPath()); $imagePath = $shape->getPath(); if (strpos($imagePath, 'zip://') !== false) { $imagePath = substr($imagePath, 6); $imagePathSplitted = explode('#', $imagePath); $imageZip = new \ZipArchive(); $imageZip->open($imagePathSplitted[0]); $imageContents = $imageZip->getFromName($imagePathSplitted[1]); $imageZip->close(); unset($imageZip); } else { $imageContents = file_get_contents($imagePath); } $objZip->addFromString('Pictures/' . md5($shape->getPath()) . '.' . $shape->getExtension(), $imageContents); } } elseif ($shape instanceof MemoryDrawing) { if (!in_array(str_replace(' ', '_', $shape->getIndexedFilename()), $arrMedia)) { $arrMedia[] = str_replace(' ', '_', $shape->getIndexedFilename()); ob_start(); call_user_func($shape->getRenderingFunction(), $shape->getImageResource()); $imageContents = ob_get_contents(); ob_end_clean(); $objZip->addFromString('Pictures/' . str_replace(' ', '_', $shape->getIndexedFilename()), $imageContents); } } } foreach ($this->presentation->getAllSlides() as $keySlide => $oSlide) { // Add background image slide $oBkgImage = $oSlide->getBackground(); if ($oBkgImage instanceof Image) { $objZip->addFromString('Pictures/' . $oBkgImage->getIndexedFilename($keySlide), file_get_contents($oBkgImage->getPath())); } } // Close file if ($objZip->close() === false) { throw new \Exception("Could not close zip file {$pFilename}."); } // 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 deleted.'); } } } else { throw new \Exception("PhpPresentation object unassigned."); } }
<?php include_once 'Sample_Header.php'; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Slide\Background\Color; use PhpOffice\PhpPresentation\Style\Color as StyleColor; use PhpOffice\PhpPresentation\Slide\Background\Image; // Create new PHPPresentation object echo date('H:i:s') . ' Create new PHPPresentation object' . EOL; $objPHPPresentation = new PhpPresentation(); // Set Thumbnail $objPHPPresentation->getPresentationProperties()->setThumbnailPath(__DIR__ . '\\resources\\phppowerpoint_logo.gif'); // Create slide echo date('H:i:s') . ' Create slide' . EOL; $oSlide1 = $objPHPPresentation->getActiveSlide(); $oSlide1->addShape(clone $oShapeDrawing); $oSlide1->addShape(clone $oShapeRichText); // Save file echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers); if (!CLI) { include_once 'Sample_Footer.php'; }