Heim  >  Artikel  >  php教程  >  PHP创建PPT文档范例解析

PHP创建PPT文档范例解析

WBOY
WBOYOriginal
2016-06-13 11:11:571132Durchsuche

PHP创建PPT文档代码实例:

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

这个PHP创建PPT文档的应用前景的话. 在WEB的某些场合还是很有用的. 需要的朋友可以多花点时间去研究了.


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn