Style & layout is not copied while creating new pptx from pptx in PHPPresentation

682 views Asked by At

I want to split slides of one pptx file into seperated pptx files, containing one slide each. The content/text is copied but the layout & styling is not copied. Here is the code.

Can anyone please help ?

<?php 

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory; 
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Slide\SlideLayout;

 $objReader = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
 $objPHPPowerPoint = $objReader->load('a.pptx');

 $totalSlides = $objPHPPowerPoint->getSlideCount();
 $oMasterSlide = $objPHPPowerPoint->getAllMasterSlides()[0];

 $documentProperties =  $objPHPPowerPoint->getDocumentProperties();

 for ( $count = 0; $count < $totalSlides; $count++ ) {

     $objPHPPresentation = new PhpPresentation();
     $slide = $objPHPPowerPoint->getSlide(  $count );
     $background = $slide->getBackground();

     $newSlide = $objPHPPresentation->addSlide( $slide );
     $newSlide->setBackground ( $background );

     $objPHPPresentation->setAllMasterSlides(  $oMasterSlide );
     $objPHPPresentation->removeSlideByIndex(0);

     $oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($objPHPPresentation, 'PowerPoint2007');
     $oWriterPPTX->save($count.'.pptx');

}
2

There are 2 answers

4
mickadoo On

I don't think it's an issue with your code - more an issue with the underlying libraries - as mentioned here: PhpPresentation imagecreatefromstring(): Data is not in a recognized format - PHP7.2

It ran a test to see if it was something I could replicate - and I was able to. The key difference in my test was in one presentation I had a simple background, and in the other it was a gradient.

This slide caused problems: this caused problems

But this one was copied over fine: this worked fine

With the more complex background I got errors like:

PHP Warning: imagecreatefromstring(): Data is not in a recognized format

My code is even less complicated than yours, I just clone the original slideshow and remove all except a single slide before saving it:

for ( $count = 0; $count < $totalSlides; $count++ ) {
    $copyVersion = clone $objPHPPowerPoint;
    foreach ($copyVersion->getAllSlides() as $index => $slide) {
        if ($index !== $count) {
            $copyVersion->removeSlideByIndex($index);
        }
    }

    $oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($copyVersion, 'PowerPoint2007');
    $oWriterPPTX->save($count.'.pptx');
}

Sorry if this doesn't exactly solve your problem, but hopefully it can help identify why it's happening. The other answer I linked to has more information about finding unsupported images types in your slides.

0
Andrey Potapov On

You can try using Aspose.Slides Cloud SDK for PHP to split a presentation into separate slides and save them to many formats. You can evaluate this REST-based API making 150 free API calls per month for API learning and presentation processing. The following code example shows you how to split a presentation and save slides to PPTX format using Aspose.Slides Cloud:

use Aspose\Slides\Cloud\Sdk\Api\Configuration;
use Aspose\Slides\Cloud\Sdk\Api\SlidesApi;
use Aspose\Slides\Cloud\Sdk\Model;

$configuration = new Configuration();
$configuration->setAppSid("my_client_id");
$configuration->setAppKey("my_client_key");

$slidesApi = new SlidesApi(null, $configuration);

$filePath = "example.pptx";

// Upload the file to the default storage.
$fileStream = fopen($filePath, 'r');
$slidesApi->uploadFile($filePath, $fileStream);

// Split the file and save the slides in PPTX format in the same folder.
$response = $slidesApi->split($filePath, null, Model\SlideExportFormat::PPTX);

// Download files of the slides.
foreach($response->getSlides() as $slide) {
    $slideFilePath = pathinfo($slide->getHref())["basename"];
    $slideFile = $slidesApi->downloadFile($slideFilePath);

    echo $slideFile->getRealPath(), "\r\n";
}

Sometimes it is necessary to split a presentation without using any code. In this case, you can use Online PowerPoint Splitter.

I work as a Support Developer at Aspose.