title: Merging/Creating powerpoint slides in PHP using COM and PHPPowerpoint
body: <p>My requirement is to split ppt/pptx files and then merge particular slides.</p>
  
  <p>I have successfully split ppt/pptx files into seperate slides using PHP COM. Now I want to join/merge the slides using some PHP Library.</p>
  
  <p>However, there doesn't seem to be one except PHPPowerpoint.</p>
  
  <p>I can use PHPPowerpoint to create slides and add text nodes/images using it but it cannot read existing .ppt/pptx files and create a merged output.</p>
  
  <p>Is there another way? I will appreciate any help at all.</p>
  
  <p>EDIT:
  I was able to merge slides but not Properly.. the background color/sort order is still missing. Please help as there are no references on the web on this except for this link - </p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/office/ff746640%28v=office.15%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/office/ff746640%28v=office.15%29.aspx</a></p>
  
  <p>Here is the code - </p>
  
  <pre><code>        //SET Max exec time
          ini_set("max_execution_time",-1);
  
  
          $directory = 'c:/ppt/slides/';
          $files_list = array_diff(scandir($directory,SCANDIR_SORT_DESCENDING), array('..', '.')); //Get list of all files from the sub slides folder
          //var_dump($files_list); die;
          $ppt_new = new COM("powerpoint.application") or die("Unable to instantiate Powerpoint 2");
          $ppt_new-&gt;Presentations-&gt;Add(true); //Create the new(merged) ppt
          $dirpath = "C:/ppt/slides/";        
          $ppt_new-&gt;Presentations[1]-&gt;Slides-&gt;Add( 1, 1 );
          foreach($files_list as $file) { //Loop through all slides to merge those
  
          $powerpnt = new COM("powerpoint.application") or die("Unable to instantiate Powerpoint");
                  echo "Adding slide...";
                  echo $file_path1 = realpath($dirpath.$file);
  
                  $pres = $powerpnt-&gt;Presentations-&gt;Open($file_path1, false, false, false) or die("Unable to open the slide");
                  echo $count = (int)$pres-&gt;Slides-&gt;Count."&lt;--SLIDES COUNT&lt;br&gt;";
                  $i=1;
                  foreach($pres-&gt;Slides as $slide)
  
                  {   
                  try {
                      $pptLayout = $slide-&gt;CustomLayout;
  //                  var_dump($pptLayout); die;
  //                  $ppt_new-&gt;Presentations[1]-&gt;Slides[$i]-&gt;FollowMasterBackground  = false;
  //                  $ppt_new-&gt;Presentations[1]-&gt;Slides[$i]-&gt;Background = $slide-&gt;Background;
                      //$ppt_new-&gt;Presentations[1]-&gt;Slides-&gt;Layout = $pptLayout;
  
                      $ppt_new-&gt;Presentations[1]-&gt;Slides-&gt;InsertFromFile($file_path1, $i, $i, $i);
                      //          $ppt_new-&gt;Presentations[1]-&gt;SaveAs("merged11.ppt");
                      //          $ppt_new-&gt;Export("created.ppt", "ppt");
                      $i++;
                      }
                          catch(Exception $e)
                          {
                              echo 'Caught exception: ',  $e-&gt;getMessage(), "\n";
                          } 
                  }
  
          }
  
          //Save the merged presentation
          $powerpnt-&gt;quit();
          $ppt_new-&gt;Presentations[1]-&gt;SaveAs("c:\ppt\merged121.ppt");
          $ppt_new-&gt;Presentations[1]-&gt;Close(); 
          $ppt_new-&gt;quit();
  
          echo "Done!";
  </code></pre>
  
  <p>Can anyone please run the code and find why the background is not coming in the merged slide?
  Thanks already.</p>