使用php创建 powerpoint2007 文档
今天早上从订阅的 zend devzone 看到篇很有意思的文章. creating powerpoint 2007 files using php. 试了一下. 果然很又意思, 分享给大家吧.
程序需要 php 5.2 以上环境, 另外需要 php_zip 和 php_xml 扩展模块支持. 另外需要下载 phppowerpoint 类库. 官方网站地址: http://phppowerpoint.codeplex.com/ 目前稳定版本是 0.1.0
这里已经下载好了. 各位可以直接同示例代码一起在本站下载. 我已打好包. 点击这里下载示例包. 另外一个官方发原始包带了api文档还有官方的示例程序的也放出来 官方包下载.
说一下感觉吧. 这个类库还可以. 编码很规范. 完全php5的风格. 我喜欢的类型. 和 zend framework 一样. 处理速度也非常快. 本次只做了简单才测试. 更多高级功能未花时间去玩. 帖一下测试代码吧.
getactiveslide(); //添加一个图片到幻灯片. $shape = $activeslide->createdrawingshape(); //设置图片名称. $shape->setname('mmclub.net logo'); //设置图片的描述信息. $shape->setdescription('mmclub.net logo'); //图片实际路径 $shape->setpath(root . 'mmclub.net.jpg'); //图片高度 $shape->setheight(103); //设置图片宽度 $shape->setwidth(339); //设置图片相对于左上角x位置, 单位像素 $shape->setoffsetx(10); //设置图片相对于左上角y位置, 单位像素 $shape->setoffsety(10); //设置图显示状态 $shape->getshadow()->setvisible(true); $shape->getshadow()->setdirection(45); $shape->getshadow()->setdistance(10); //设置一个文本框 $shape = $activeslide->createrichtextshape(); //设置文本框高度, 单位像素 $shape->setheight(150); //设置文本框宽度, 单位像素 $shape->setwidth(600); //设置文本框相对于左上角x位置, 单位像素 $shape->setoffsetx(150); //设置文本框相对于左上角y位置, 单位像素 $shape->setoffsety(200); //设置文本布局位置为水平居中, 垂直居中. $shape->getalignment()->sethorizontal( phppowerpoint_style_alignment::horizontal_center ); $shape->getalignment()->setvertical( phppowerpoint_style_alignment::vertical_center ); //设置文本框文本内容. 在中文环境下测试没中文问题. 如果在 e 文环境. 注意要指定支持中文的字体. 否则可能出乱码了. $textrun = $shape->createtextrun('欢迎使用 phppowerpoint2007'); //使用字体加粗 $textrun->getfont()->setbold(true); //设置字体尺寸为 38, 这里注意一下文字的大小设置. 前面的文本框的大小是固定的. 如果文字超出的容器会被出容器被排到下面 $textrun->getfont()->setsize(38); //设置文字颜色, 这里是argb模式 , 16进制模式, 前面2位为透明度, 后面为rgb值. 这里设置为 blue蓝色 $textrun->getfont()->setcolor( new phppowerpoint_style_color( 'ffff0000' ) ); //下面再设置几个文本框 $shape0 = $activeslide->createrichtextshape(); $shape0->setheight(50); $shape0->setwidth(400); $shape0->setoffsetx(250); $shape0->setoffsety(400); $shape0->getalignment()->sethorizontal( phppowerpoint_style_alignment::horizontal_center ); $shape0->getalignment()->setvertical( phppowerpoint_style_alignment::vertical_center ); $textrun0 = $shape0->createtextrun('http://www.mmclub.net'); $textrun0->getfont()->setsize(26); $textrun0->getfont()->setcolor( new phppowerpoint_style_color( 'ff0000ff' ) ); $shape1 = $activeslide->createrichtextshape(); $shape1->setheight(30); $shape1->setwidth(200); $shape1->setoffsetx(700); $shape1->setoffsety(500); $shape1->getalignment()->sethorizontal( phppowerpoint_style_alignment::horizontal_left ); $shape1->getalignment()->setvertical( phppowerpoint_style_alignment::vertical_center ); $textrun1 = $shape1->createtextrun('author: guya'); $textrun1->getfont()->setsize(14); $textrun1->getfont()->setcolor( new phppowerpoint_style_color( 'ff000000' ) ); $shape2 = $activeslide->createrichtextshape(); $shape2->setheight(30); $shape2->setwidth(200); $shape2->setoffsetx(700); $shape2->setoffsety(540); $shape2->getalignment()->sethorizontal( phppowerpoint_style_alignment::horizontal_left ); $shape2->getalignment()->setvertical( phppowerpoint_style_alignment::vertical_center ); $textrun2 = $shape2->createtextrun('date: 2009-4-30'); $textrun2->getfont()->setsize(14); $textrun2->getfont()->setcolor( new phppowerpoint_style_color( 'ff000000' ) ); //保存pptx 文件, 使用 2007 格式 $objwriter = phppowerpoint_iofactory::createwriter($ppp, 'powerpoint2007'); //保存文件 $objwriter->save(root . 'myphpppt.pptx'); echo 'ppt create success!'; ?>
这个东西的应用前景的话. 在web的某些场合还是很有用的. 需要的朋友可以多花点时间去研究了.
本文来源:http://blog.mmclub.net/index/view/article_id/89
网上几乎很少有相关资料,找了好久,拿上来跟大家分享,同时感谢作者,。