new COM("PowerPoint.Application") Doesn't work

I'm trying to convert slides on a ppt file to a PNG files, but something wrong with my code.

in a local test the script stuck at this line :

$ppApp = new COM("PowerPoint.Application") or die ("Unable to instantiate PowerPoint");

then show "Maximum execution time of 30 seconds exceeded";

And when i go on my live server, the script work for the first time, but then it's show up an error

"Fatal error: Uncaught exception 'Exception' with message 'Could not close ppt file 1251.pptx.'

I'm on a Windows server, and i think something wrong with the way i'm using ppt process.

Here is the Code :

$ppApp = new COM("PowerPoint.Application") or die ("Unable to instantiate PowerPoint");
    $ppApp->Visible = True;
    $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"])));
    $ppName = $jobQuery['id'].".pptx";
    $FileName = "MyPP";

    //*** Open Document ***//
    $ppApp->Presentations->Open(realpath($ppName));

    //*** Save Document ***//
    $p_opned =  $ppApp->ActivePresentation->SaveAs($strPath."/".$jobQuery['id']."_".$FileName,18);
    //$ppApp->ActivePresentation->SaveAs(realpath($FileName),17);
    $ppApp->Quit();
    $ppApp = NULL;
    unset($ppApp); 

Thank you for your help.

Answers


I think your problem may be with your saving location or simply your file was not found, are you sure that the location you're trying to save to actually exists as from your code I'm not so sure?

$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"])));

Should be:

$strPath = realpath(dirname(__FILE__));

Are you sure that this file actually exists in the location:

$ppName = $jobQuery['id'].".pptx";

Check it with:

file_exists($ppName)

Look here for a similar question: Fatal Error in convert PPT to PNG in PHP

EDIT:

Try the below, I think your $p_opned was keeping the reference open to your $ppApp object and hence your instance wasn't properly removed.

//*** Save Document ***//
$ppApp->ActivePresentation->SaveAs($strPath."/".$jobQuery['id']."_".$FileName,18);

EDIT 2:

As requested try wrapping the line you think is causing the exception (so assuming its this line):

try {
  //*** Save Document ***//
  $ppApp->ActivePresentation->SaveAs($strPath."/".$jobQuery['id']."_".$FileName,18);
} catch (Exception $e) {
    echo 'Exception message: ',  $e->getMessage(), "\n";
}

Although looking again at your error message it refers to your original file you tried to open (1251.pptx) as you're trying to save the file with a different post fix (which it doesn't mention in the code).

You could try wrapping the Exception try / catch around the 'Open' script line if the above doesn't work, or perhaps you could try adding before you Quit:

$ppApp->ActivePresentation->Close();

If your error is because Powerpoint is still hanging around after the first instance call it must be because you still have a reference alive somewhere, so try checking ALL your code. If you still get an error you should include the FULL error message in your question and remove the Max execution error message from your question is this not actually a problem (I assume you don't still get the "Maximum execution time of 30 seconds exceeded" message anymore?).

Lastly you could always try a PowerPoint library:

http://phppowerpoint.codeplex.com/


Need Your Help

SMTP JavaMail : Getting SMTPTransport Not Connected error with Threads

java multithreading email smtp javamail

I'm trying to work with the JavaMail API. I'm trying to send multiple emails using the same transport object to avoid re-authentication.

Capture screenshot of current web site in Universal Apps

c# .net windows windows-10 win-universal-app

Here is how I can capture screen shot of current webpage, but sometimes it is capturing only visible area of webview (what user actually see).