首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

施用PHP创建 PowerPoint2007 文档

2012-12-20 
使用PHP创建 PowerPoint2007 文档今天早上从订阅的 Zend DevZone 看到篇很有意思的文章. Creating PowerPo

使用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 一样. 处理速度也非常快. 本次只做了简单才测试. 更多高级功能未花时间去玩. 帖一下测试代码吧.

  1. <?php /**
  2. * PHP 生成 PowerPoint 2007 示例脚本. *
  3. * 本程序需要 PHP 5.2 以上版本, 需要 php_zip 和 php_xml 扩展支持. * 通常WIN下程序只要打开 php_zip 扩展即可, php_xml 扩展内置支持.
  4. * Linux 下需要根据编译条件具体调整. *
  5. * @author: Guya * @since: 2009-4-30
  6. */
  7. //目录分割符号 define('DS', DIRECTORY_SEPARATOR);
  8. //定义根目录
  9. define('ROOT', dirname(__FILE__) . DS);
  10. //修改include路径, PHPPowerPoint 包放在当前目录的 libs 目录下. set_include_path(get_include_path() . PATH_SEPARATOR . ROOT . 'libs');
  11. //不限制脚本运行时间限制.
  12. set_time_limit(0);
  13. //简单设置自动载入函数. function __autoload($className) {
  14. ????include_once(str_replace("_", DS, $className) . ".php");??}
  15. //新建立一个 PHPPowerPoint 对象.
  16. $ppp = new PHPPowerPoint();
  17. //获取当前使用的一页幻灯片 $activeSlide = $ppp->getActiveSlide();
  18. //添加一个图片到幻灯片.
  19. $shape = $activeSlide->createDrawingShape();
  20. //设置图片名称. $shape->setName('MmClub.net Logo');
  21. //设置图片的描述信息.
  22. $shape->setDescription('MmClub.net Logo');
  23. //图片实际路径 $shape->setPath(ROOT . 'mmclub.net.jpg');
  24. //图片高度
  25. $shape->setHeight(103);
  26. //设置图片宽度 $shape->setWidth(339);
  27. //设置图片相对于左上角X位置, 单位像素
  28. $shape->setOffsetX(10);
  29. //设置图片相对于左上角Y位置, 单位像素 $shape->setOffsetY(10);
  30. //设置图显示状态
  31. $shape->getShadow()->setVisible(true);
  32. $shape->getShadow()->setDirection(45); $shape->getShadow()->setDistance(10);
  33. //设置一个文本框
  34. $shape = $activeSlide->createRichTextShape();
  35. //设置文本框高度, 单位像素 $shape->setHeight(150);
  36. //设置文本框宽度, 单位像素
  37. $shape->setWidth(600);
  38. //设置文本框相对于左上角X位置, 单位像素 $shape->setOffsetX(150);
  39. //设置文本框相对于左上角Y位置, 单位像素
  40. $shape->setOffsetY(200);
  41. //设置文本布局位置为水平居中, 垂直居中. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER );
  42. $shape->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER );
  43. //设置文本框文本内容. 在中文环境下测试没中文问题. 如果在 e 文环境. 注意要指定支持中文的字体. 否则可能出乱码了. $textRun = $shape->createTextRun('欢迎使用 PHPPowerPoint2007');
  44. //使用字体加粗
  45. $textRun->getFont()->setBold(true);
  46. //设置字体尺寸为 38, 这里注意一下文字的大小设置. 前面的文本框的大小是固定的. 如果文字超出的容器会被出容器被排到下面 $textRun->getFont()->setSize(38);
  47. //设置文字颜色, 这里是ARGB模式 , 16进制模式, 前面2位为透明度, 后面为RGB值. 这里设置为 blue蓝色
  48. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFF0000' ) );
  49. //下面再设置几个文本框 $shape0 = $activeSlide->createRichTextShape();
  50. $shape0->setHeight(50); $shape0->setWidth(400);
  51. $shape0->setOffsetX(250); $shape0->setOffsetY(400);
  52. $shape0->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER ); $shape0->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER );
  53. $textRun0 = $shape0->createTextRun('http://www.mmclub.net'); $textRun0->getFont()->setSize(26);
  54. $textRun0->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FF0000FF' ) );
  55. $shape1 = $activeSlide->createRichTextShape(); $shape1->setHeight(30);
  56. $shape1->setWidth(200); $shape1->setOffsetX(700);
  57. $shape1->setOffsetY(500); $shape1->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  58. $shape1->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); $textRun1 = $shape1->createTextRun('Author: Guya');
  59. $textRun1->getFont()->setSize(14); $textRun1->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FF000000' ) );
  60. $shape2 = $activeSlide->createRichTextShape();
  61. $shape2->setHeight(30); $shape2->setWidth(200);
  62. $shape2->setOffsetX(700); $shape2->setOffsetY(540);
  63. $shape2->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $shape2->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER );
  64. $textRun2 = $shape2->createTextRun('Date: 2009-4-30'); $textRun2->getFont()->setSize(14);
  65. $textRun2->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FF000000' ) );
  66. //保存PPTX 文件, 使用 2007 格式 $objWriter = PHPPowerPoint_IOFactory::createWriter($ppp, 'PowerPoint2007');
  67. //保存文件
  68. $objWriter->save(ROOT . 'myPhpPpt.pptx'); echo 'ppt create success!';
  69. ?>
这个东西的应用前景的话. 在WEB的某些场合还是很有用的. 需要的朋友可以多花点时间去研究了.本文来源:http://blog.mmclub.net/index/view/article_id/89 网上几乎很少有相关资料,找了好久,拿上来跟大家分享,同时感谢作者,。

热点排行