1

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');

}
Keyu Gan
  • 711
  • 5
  • 17
Rockers Niloy
  • 101
  • 2
  • 12
  • if this works anything like phpOffice\phpspreadsheet then its likely you need to explicitly copy styles from the original and apply it to the new slide, there are all sorts of styles that could be in effect. If you can output the content and style of the original sheet then start cross checking the docs with the right functions to copy things that may be a start. – Chris Richardson Sep 04 '20 at 03:43
  • It's far simpler and more reliable (though possibly a bit slower) to ... For each slide in the current presentation, do a Save Copy As, then open the copy and working backwards, delete each slide EXCEPT the one you want to keep. Save. Repeat for each slide in the original presentation. – Steve Rindsberg Feb 21 '22 at 16:32

2 Answers2

0

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.

mickadoo
  • 3,337
  • 1
  • 25
  • 38
  • so If I downgrade php version to 7.1, then it will work ? – Rockers Niloy Sep 08 '20 at 17:59
  • I don't think it's anything to do with PHP version, just some of the libraries not being very good at handling complex slide graphics. I could be wrong of course, maybe it's something that was recently introduced into PHPPresentation. However there are a few cases where people have had similar issues where style was lost: https://github.com/PHPOffice/PHPPresentation/issues/594 https://github.com/PHPOffice/PHPPresentation/issues/542 https://github.com/PHPOffice/PHPPresentation/issues/346 https://github.com/PHPOffice/PHPPresentation/issues/291 – mickadoo Sep 08 '20 at 18:11
  • in my case, even simple slides are not copied at all. thats why I am worried here . Here is sample pptx file https://easycaptures.com/fs/uploaded/1452/0004185820.jpg here is what I got after copy https://easycaptures.com/fs/uploaded/1452/6495259587.jpg – Rockers Niloy Sep 08 '20 at 18:23
  • Can you try with just the basic copy like `$copyVersion = clone $objPHPPowerPoint; $oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($copyVersion, 'PowerPoint2007'); $oWriterPPTX->save($count.'.pptx');`? – mickadoo Sep 08 '20 at 20:06
0

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.

Andrey Potapov
  • 29
  • 2
  • 14