Search code examples
repairphppresentation

PHPPresentation file with 2 slides requires repair


I'm working on a script to create a presentation with multiple slides. When I add the first slide the script works fine.

$colorBlack = new Color('FF000000');


$objPHPPresentation = new PhpPresentation();

$objPHPPresentation->getDocumentProperties()->setCreator('PHPOffice')
    ->setLastModifiedBy('PHPPresentation Team')
    ->setTitle('Sample 01 Title')
    ->setSubject('Sample 01 Subject')
    ->setDescription('Sample 01 Description')
    ->setKeywords('office 2007 openxml libreoffice odt php')
    ->setCategory('Sample Category');

$objPHPPresentation->removeSlideByIndex(0);

$currentSlide = createTemplatedSlide($objPHPPresentation); 

$shape = $currentSlide->createRichTextShape(); $shape->setHeight(200);    
$shape->setWidth(600); $shape->setOffsetX(10); $shape->setOffsetY(400); 
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);

$textRun = $shape->createTextRun('slide 1 text 1'); 
$textRun->getFont()->setBold(true); 
$textRun->getFont()->setSize(28); 
$textRun->getFont()->setColor($colorBlack); $shape->createBreak();

$textRun = $shape->createTextRun('slide 1 text 2'); 
$textRun->getFont()->setBold(true); 
$textRun->getFont()->setSize(60); 
$textRun->getFont()->setColor($colorBlack);

So the above code works fine. Then when I add code for a second slide, I get an error when opening with PowerPoint that indicates there is a problem with the content and the file needs repaired.

Here's the code I add for the second slide. It's basically a copy of the previous slide.

$currentSlide = createTemplatedSlide($objPHPPresentation); 

$shape = $currentSlide->createRichTextShape(); 
$shape->setHeight(200); 
$shape->setWidth(600); 
$shape->setOffsetX(10); 
$shape->setOffsetY(400); 
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);

$textRun = $shape->createTextRun('second slide text 1'); 
$textRun->getFont()->setBold(true); 
$textRun->getFont()->setSize(28); 
$textRun->getFont()->setColor($colorBlack); $shape->createBreak();

$textRun = $shape->createTextRun('second slide text 2'); 
$textRun->getFont()->setBold(true); 
$textRun->getFont()->setSize(60); 
$textRun->getFont()->setColor($colorBlack);

Solution

  • I got this on another forum. Copy the layout.

    $objPHPPresentation = new PhpPresentation();
    $oMasterSlide = $objPHPPresentation->getAllMasterSlides()[0];
    $oSlideLayout = $oMasterSlide->getAllSlideLayouts()[0];
    

    Then copy the layout to each new slide:

    $currentSlide->setSlideLayout($oSlideLayout);