Heim > Artikel > Backend-Entwicklung > 施用PHP创建 PowerPoint2007 文档
使用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 一样. 处理速度也非常快. 本次只做了简单才测试. 更多高级功能未花时间去玩. 帖一下测试代码吧.
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li><span><span class="comment">/**</span> </span></li> <li class="alt"><span><span class="comment"> * PHP 生成 PowerPoint 2007 示例脚本.</span> </span></li> <li><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * 本程序需要 PHP 5.2 以上版本, 需要 php_zip 和 php_xml 扩展支持.</span> </span></li> <li><span><span class="comment"> * 通常WIN下程序只要打开 php_zip 扩展即可, php_xml 扩展内置支持.</span> </span></li> <li class="alt"><span><span class="comment"> * Linux 下需要根据编译条件具体调整.</span> </span></li> <li><span><span class="comment"> *</span> </span></li> <li class="alt"><span><span class="comment"> * @author: Guya</span> </span></li> <li><span><span class="comment"> * @since: 2009-4-30</span> </span></li> <li class="alt"><span><span class="comment"> */</span><span> </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//目录分割符号</span><span> </span></span></li> <li><span>define(<span class="string">'DS'</span><span>, DIRECTORY_SEPARATOR); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//定义根目录</span><span> </span></span></li> <li class="alt"><span>define(<span class="string">'ROOT'</span><span>, dirname(</span><span class="keyword">__FILE__</span><span>) . DS); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//修改include路径, PHPPowerPoint 包放在当前目录的 libs 目录下.</span><span> </span></span></li> <li><span>set_include_path(get_include_path() . PATH_SEPARATOR . ROOT . <span class="string">'libs'</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//不限制脚本运行时间限制.</span><span> </span></span></li> <li class="alt"><span>set_time_limit(0); </span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//简单设置自动载入函数.</span><span> </span></span></li> <li><span><span class="keyword">function</span><span> __autoload(</span><span class="vars">$className</span><span>) { </span></span></li> <li class="alt"><span>????<span class="keyword">include_once</span><span>(</span><span class="func">str_replace</span><span>(</span><span class="string">"_"</span><span>, DS, </span><span class="vars">$className</span><span>) . </span><span class="string">".php"</span><span>);??</span></span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//新建立一个 PHPPowerPoint 对象.</span><span> </span></span></li> <li class="alt"><span><span class="vars">$ppp</span><span> = </span><span class="keyword">new</span><span> PHPPowerPoint(); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//获取当前使用的一页幻灯片</span><span> </span></span></li> <li><span><span class="vars">$activeSlide</span><span> = </span><span class="vars">$ppp</span><span>->getActiveSlide(); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//添加一个图片到幻灯片.</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span> = </span><span class="vars">$activeSlide</span><span>->createDrawingShape(); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置图片名称.</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->setName(</span><span class="string">'MmClub.net Logo'</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置图片的描述信息.</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->setDescription(</span><span class="string">'MmClub.net Logo'</span><span>); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//图片实际路径</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->setPath(ROOT . </span><span class="string">'mmclub.net.jpg'</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//图片高度</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->setHeight(103); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置图片宽度</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->setWidth(339); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置图片相对于左上角X位置, 单位像素</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->setOffsetX(10); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置图片相对于左上角Y位置, 单位像素</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->setOffsetY(10); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置图显示状态</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->getShadow()->setVisible(true); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="vars">$shape</span><span>->getShadow()->setDirection(45); </span></span></li> <li><span><span class="vars">$shape</span><span>->getShadow()->setDistance(10); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置一个文本框</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span> = </span><span class="vars">$activeSlide</span><span>->createRichTextShape(); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置文本框高度, 单位像素</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->setHeight(150); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置文本框宽度, 单位像素</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->setWidth(600); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置文本框相对于左上角X位置, 单位像素</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->setOffsetX(150); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置文本框相对于左上角Y位置, 单位像素</span><span> </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->setOffsetY(200); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置文本布局位置为水平居中, 垂直居中.</span><span> </span></span></li> <li><span><span class="vars">$shape</span><span>->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER ); </span></span></li> <li class="alt"><span><span class="vars">$shape</span><span>->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置文本框文本内容. 在中文环境下测试没中文问题. 如果在 e 文环境. 注意要指定支持中文的字体. 否则可能出乱码了.</span><span> </span></span></li> <li><span><span class="vars">$textRun</span><span> = </span><span class="vars">$shape</span><span>->createTextRun(</span><span class="string">'欢迎使用 PHPPowerPoint2007'</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//使用字体加粗</span><span> </span></span></li> <li class="alt"><span><span class="vars">$textRun</span><span>->getFont()->setBold(true); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//设置字体尺寸为 38, 这里注意一下文字的大小设置. 前面的文本框的大小是固定的. 如果文字超出的容器会被出容器被排到下面</span><span> </span></span></li> <li><span><span class="vars">$textRun</span><span>->getFont()->setSize(38); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//设置文字颜色, 这里是ARGB模式 , 16进制模式, 前面2位为透明度, 后面为RGB值. 这里设置为 blue蓝色</span><span> </span></span></li> <li class="alt"><span><span class="vars">$textRun</span><span>->getFont()->setColor( </span><span class="keyword">new</span><span> PHPPowerPoint_Style_Color( </span><span class="string">'FFFF0000'</span><span> ) ); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//下面再设置几个文本框</span><span> </span></span></li> <li><span><span class="vars">$shape0</span><span> = </span><span class="vars">$activeSlide</span><span>->createRichTextShape(); </span></span></li> <li class="alt"><span><span class="vars">$shape0</span><span>->setHeight(50); </span></span></li> <li><span><span class="vars">$shape0</span><span>->setWidth(400); </span></span></li> <li class="alt"><span><span class="vars">$shape0</span><span>->setOffsetX(250); </span></span></li> <li><span><span class="vars">$shape0</span><span>->setOffsetY(400); </span></span></li> <li class="alt"><span><span class="vars">$shape0</span><span>->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER ); </span></span></li> <li><span><span class="vars">$shape0</span><span>->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); </span></span></li> <li class="alt"><span><span class="vars">$textRun0</span><span> = </span><span class="vars">$shape0</span><span>->createTextRun(</span><span class="string">'http://www.mmclub.net'</span><span>); </span></span></li> <li><span><span class="vars">$textRun0</span><span>->getFont()->setSize(26); </span></span></li> <li class="alt"><span><span class="vars">$textRun0</span><span>->getFont()->setColor( </span><span class="keyword">new</span><span> PHPPowerPoint_Style_Color( </span><span class="string">'FF0000FF'</span><span> ) ); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="vars">$shape1</span><span> = </span><span class="vars">$activeSlide</span><span>->createRichTextShape(); </span></span></li> <li><span><span class="vars">$shape1</span><span>->setHeight(30); </span></span></li> <li class="alt"><span><span class="vars">$shape1</span><span>->setWidth(200); </span></span></li> <li><span><span class="vars">$shape1</span><span>->setOffsetX(700); </span></span></li> <li class="alt"><span><span class="vars">$shape1</span><span>->setOffsetY(500); </span></span></li> <li><span><span class="vars">$shape1</span><span>->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); </span></span></li> <li class="alt"><span><span class="vars">$shape1</span><span>->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); </span></span></li> <li><span><span class="vars">$textRun1</span><span> = </span><span class="vars">$shape1</span><span>->createTextRun(</span><span class="string">'Author: Guya'</span><span>); </span></span></li> <li class="alt"><span><span class="vars">$textRun1</span><span>->getFont()->setSize(14); </span></span></li> <li><span><span class="vars">$textRun1</span><span>->getFont()->setColor( </span><span class="keyword">new</span><span> PHPPowerPoint_Style_Color( </span><span class="string">'FF000000'</span><span> ) ); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="vars">$shape2</span><span> = </span><span class="vars">$activeSlide</span><span>->createRichTextShape(); </span></span></li> <li class="alt"><span><span class="vars">$shape2</span><span>->setHeight(30); </span></span></li> <li><span><span class="vars">$shape2</span><span>->setWidth(200); </span></span></li> <li class="alt"><span><span class="vars">$shape2</span><span>->setOffsetX(700); </span></span></li> <li><span><span class="vars">$shape2</span><span>->setOffsetY(540); </span></span></li> <li class="alt"><span><span class="vars">$shape2</span><span>->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); </span></span></li> <li><span><span class="vars">$shape2</span><span>->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); </span></span></li> <li class="alt"><span><span class="vars">$textRun2</span><span> = </span><span class="vars">$shape2</span><span>->createTextRun(</span><span class="string">'Date: 2009-4-30'</span><span>); </span></span></li> <li><span><span class="vars">$textRun2</span><span>->getFont()->setSize(14); </span></span></li> <li class="alt"><span><span class="vars">$textRun2</span><span>->getFont()->setColor( </span><span class="keyword">new</span><span> PHPPowerPoint_Style_Color( </span><span class="string">'FF000000'</span><span> ) ); </span></span></li> <li><span> </span></li> <li class="alt"><span><span class="comment">//保存PPTX 文件, 使用 2007 格式</span><span> </span></span></li> <li><span><span class="vars">$objWriter</span><span> = PHPPowerPoint_IOFactory::createWriter(</span><span class="vars">$ppp</span><span>, </span><span class="string">'PowerPoint2007'</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span><span class="comment">//保存文件</span><span> </span></span></li> <li class="alt"><span><span class="vars">$objWriter</span><span>->save(ROOT . </span><span class="string">'myPhpPpt.pptx'</span><span>); </span></span></li> <li><span><span class="func">echo</span><span> </span><span class="string">'ppt create success!'</span><span>; </span></span></li> <li class="alt"><span>?> </span></li> </ol>
这个东西的应用前景的话. 在WEB的某些场合还是很有用的. 需要的朋友可以多花点时间去研究了.
本文来源:http://blog.mmclub.net/index/view/article_id/89
网上几乎很少有相关资料,找了好久,拿上来跟大家分享,同时感谢作者,。