PHP读取PPT文件的方法
本文实例讲述了PHP读取PPT文件的方法。分享给大家供大家参考,具体如下:
最近做一个和FLASH有关的东西,其中就要用到在网站上看PPT就像百度,豆丁网那样可以直接在网站上读,在网上搜了半天没搜到,都是些什么安装个软件什么的,PHP网站放到空间上,谁能让你在哪装软件呢?不是在瞎扯么?不过还好,最后在国外一个网站上搜到了一个解决思路,就是一个PHP操作PPT的类,当然这个网站还提供了操作OFFICES软件的其他类,不过是2007版的OFFICES,现把网址贴出来奉献给大家:http://phppowerpoint.codeplex.com/。也可点击此处本站下载。
这个类可以实现PPT2007格式的读写/生成
下面也贴出当时看到的一个例子:
本例子使用phppowerpoint类,使用该类注意两点,一个是了解参数含义,二注意字符集
<?php header("content-type:text/html;charset=utf-8");//字体设置防止乱码 error_reporting(E_ALL); /**Includepath**/ set_include_path(get_include_path().PATH_SEPARATOR.'../Classes/'); /**PHPPowerPoint*/ include'PHPPowerPoint.php'; /**PHPPowerPoint_IOFactory*/ include'PHPPowerPoint/IOFactory.php'; //CreatenewPHPPowerPointobject //echodate('H:i:s')."CreatenewPHPPowerPointobject\n"; $objPHPPowerPoint=newPHPPowerPoint(); $objPHPPowerPoint->getProperties()->setCreator("MaartenBalliauw"); $objPHPPowerPoint->getProperties()->setLastModifiedBy("MaartenBalliauw"); $objPHPPowerPoint->getProperties()->setTitle("Office2007PPTXTestDocument"); $objPHPPowerPoint->getProperties()->setSubject("Office2007PPTXTestDocument"); $objPHPPowerPoint->getProperties()->setDescription("TestdocumentforOffice2007PPTX,generatedusingPHPclasses."); $objPHPPowerPoint->getProperties()->setKeywords("office2007openxmlphp"); $objPHPPowerPoint->getProperties()->setCategory("Testresultfile"); //Removefirstslide //echodate('H:i:s')."Removefirstslide\n"; $objPHPPowerPoint->removeSlideByIndex(0); //Createtemplatedslide //echodate('H:i:s')."Createtemplatedslide\n"; /*$currentSlide=createTemplatedSlide($objPHPPowerPoint);//localfunction //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(200); $shape->setWidth(600); $shape->setOffsetX(10); $shape->setOffsetY(400); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('Introductionto'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('PHPPowerPoint'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(60); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createtemplatedslide echodate('H:i:s')."Createtemplatedslide\n"; $currentSlide=createTemplatedSlide($objPHPPowerPoint);//localfunction //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(10); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('WhatisPHPPowerPoint?'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(100); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('-Aclasslibrary'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-WritteninPHP'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-Representingapresentation'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-Supportswritingtodifferentfileformats'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createtemplatedslide echodate('H:i:s')."Createtemplatedslide\n"; $currentSlide=createTemplatedSlide($objPHPPowerPoint);//localfunction //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(10); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('What\'sthepoint?'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(100); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('-Generateslidedecks'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-Representbusinessdata'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-Showafamilyslideshow'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-...'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-Exportthesetodifferentformats'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-PowerPoint2007'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-Serialized'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('-...(moretocome)...'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createtemplatedslide echodate('H:i:s')."Createtemplatedslide\n"; $currentSlide=createTemplatedSlide($objPHPPowerPoint);//localfunction //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(10); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('Needmoreinfo?'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createashape(text) echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(100); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $textRun=$shape->createTextRun('ChecktheprojectsiteonCodePlex:'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); $shape->createBreak(); $textRun=$shape->createTextRun('); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('FFFFFFFF')); //Createtemplatedslide echodate('H:i:s')."Createtemplatedslide\n";*/ //test //从数据库调取数据进行for循环 $row=array('titlepic'=>array('./images/love.gif','./images/love1.gif','./images/love2.gif','./images/love3.gif'),'xsprice'=>array("55","33","22","333"),'cjid'=>array('100','222','333','3333'),'lpid'=>array('111','222','333','444'),'price'=>array('111','433','243','3245')); for($i=0;$i<4;$i++) { $currentSlide=createTemplatedSlide1($objPHPPowerPoint,$row["titlepic"][$i]);//localfunction //Createashape(text) //echodate('H:i:s')."Createashape(richtext)\n"; $shape=$currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); //调整字体的高度宽度 $shape->setOffsetX(20); $shape->setOffsetY(400); //$shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); $row["price"]=iconv("utf-8","gb2312",$row["price"][$i]); $textRun=$shape->createTextRun('礼品网价格:'.$row["xsprice"][$i]); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('#000000')); $shape=$currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(20); $shape->setOffsetY(500); $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT); //添加多行内容从这开始 $textRun=$shape->createTextRun('公司编号:'.$row["cjid"][$i]); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('#000000')); $shape->createBreak(); $textRun=$shape->createTextRun('礼品网编号:'.$row["lpid"][$i]); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor(newPHPPowerPoint_Style_Color('#000000')); //test //SavePowerPoint2007file } //echodate('H:i:s')."WritetoPowerPoint2007format\n"; $objWriter=PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint,'PowerPoint2007'); $objWriter->save(str_replace('.php','.pptx',__FILE__)); header("Content-type:application/vnd.ms-powerpoint;"); header("location:02presentation.pptx"); //Echomemorypeakusage //echodate('H:i:s')."Peakmemoryusage:".(memory_get_peak_usage(true)/1024/1024)."MB\r\n"; //Echodone //echodate('H:i:s')."Donewritingfile.\r\n"; /** *Createsatemplatedslide * *@paramPHPPowerPoint$objPHPPowerPoint *@returnPHPPowerPoint_Slide */ functioncreateTemplatedSlide1(PHPPowerPoint$objPHPPowerPoint,$cs1) { //Createslide $slide=$objPHPPowerPoint->createSlide(); //Addbackgroundimage $shape=$slide->createDrawingShape(); $shape->setName('Background'); $shape->setDescription('Background'); $shape->setPath('./images/realdolmen_bg.jpg'); $shape->setWidth(950); $shape->setHeight(720); $shape->setOffsetX(0); $shape->setOffsetY(0); //Addlogo $shape=$slide->createDrawingShape(); $shape->setName('PHPPowerPointlogo'); $shape->setDescription('PHPPowerPointlogo'); $shape->setPath($cs1); $shape->setHeight(120); $shape->setOffsetX(10); $shape->setOffsetY(10); //Returnslide return$slide; }
我的问题仍然还在研究中,如果大家有更好的解决方法,请继续贴,期待更多好的分享
希望本文所述对大家PHP程序设计有所帮助。