0

I am using this below code to load a pptx file & saving into a new pptx. My code is simple. But style is broken in new pptx file. can anyone kindly help ?

<?php 

require_once 'vendor/autoload.php';

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

function createPPT($template){
   $pptLayout = new \PhpOffice\PhpPresentation\DocumentLayout();
   $pptLayout->setDocumentLayout($pptLayout::LAYOUT_SCREEN_16X10);
   $pptTMPL = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
    //$ppt = $pptTMPL->load("./ppt-template.pptx");
   $ppt = $pptTMPL->load("./$template");
   $ppt->setLayout($pptLayout);

   $oWriterPPTX = IOFactory::createWriter($ppt, 'PowerPoint2007');
   $oWriterPPTX->save(__DIR__ . "/sample2.pptx");

}

createPPT("a.pptx", "PowerPoint2007");
Rockers Niloy
  • 101
  • 2
  • 12

1 Answers1

1

Whenever we are working working with MS Office, import / export from php, we need to include a CSS file in the related code. More then 10 time we also faced this kind of problem. So we need to send the style file URL, to the related / called file. Check the modified code below :-

function createPPT($template)
  {

       $custom_css = "<your_url>/stye.css";

       $pptLayout = new \PhpOffice\PhpPresentation\DocumentLayout();
       $pptLayout->setDocumentLayout($pptLayout::LAYOUT_SCREEN_16X10);
       $pptTMPL = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
        //$ppt = $pptTMPL->load("./ppt-template.pptx");
       $ppt = $pptTMPL->load("./$template" , $custom_css);
       $ppt->setLayout($pptLayout);
    
       $oWriterPPTX = IOFactory::createWriter($ppt, 'PowerPoint2007');
       $oWriterPPTX->save(__DIR__ . "/sample2.pptx");
    
    }

Try this. I hope this will support your project.

ARVIND IT
  • 156
  • 1
  • 9
  • 1
    is that possible to get css file of an existing pptx file. – Rockers Niloy Aug 31 '20 at 05:52
  • some times it wont work. As per my work experience the file will not mention the included style file, at time of export. Because we instruct that file with our PHP function. Beside this it will depend on your function structure. – ARVIND IT Aug 31 '20 at 06:22