Monday, November 06, 2006

How to create a PowerPoint .ppt file using COM and PHP?

If you need to create a presentation using some dynamic data, here is how to create a PowerPoint .ppt file. You can create as many slides as you want and then add elements inside by using "Slides[number_of_slide]":
$powerpnt = new COM("powerpoint.application");
//Creating a new presentation
$pres=$powerpnt->gt;Presentations->gt;Add();
//Adds the first slide. "12" means blank slide
$pres->gt;Slides->gt;Add(1,12);
//Adds another slide. "10" means a slide with a clipart and text
$pres->gt;Slides->gt;Add(2,10);
//Adds a textbox (1=horizontal, 20=left margin, 50=top margin, 300=width, 40=height)
$pres->gt;Slides[1]->gt;Shapes->gt;AddTextbox(1,20,50,300,40);
//Adds a 16-point star (94=16 point star, 100=left margin, 200=top margin, 300=width, 300=height)
$pres->gt;Slides[1]->gt;Shapes->gt;AddShape(94,100,200,300,300);
//Save the document as PPT file
$powerpnt->gt;Presentations[1]->gt;SaveAs("D:\byeworld.ppt");
//And of course, quit Power Point
$powerpnt->gt;quit();
//Give the user a download link
echo 'gt;Download file as .pptgt;';
?>gt;
Note: To understand the meaning of each function, just open the Object Browser. It's on the place in Microsoft PowerPoint (Alt+F11) to open Visual Basic Editor, F2 to open Object Browser).

No comments: