php编辑ppt方法 PHPPowerPoint类 学习

2016年3月2日10:01:00

某一网站项目需要前台提交信息生成ppt提供下载和在线预览。

所用 PHPPowerPoint类 传送门 :http://phppowerpoint.codeplex.com/

****************************非原创分割线********************************

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

亲测可用,路径与部分文件名需自行更改。(侵权即删)

  1. createSlide()方法:新建空白ppt页  

********************************************************************************

2016年3月3日11:39:24 更新

想实现编辑已有ppt模板,到github上下了0.6.0 版本

1、github上下的0.6.0 有示例,正确与否未知(反正上述php代码可以实现新建ppt及其他操作)

2、遇到vendor/autoload.php,不存在问题,弄了好久。下了composer,安装方法网上都有,此处不赘述,只吐槽。什么开启openssl扩展,什么复制dll文件到apache,php.ini设置了还特么不行,php下搜索php.ini有两个文件,都改了才行,这个那个,头都大了。

3、项目路径下cmd 运行 composer install 会自动根据composer.json 下载lib库

4、本身也就一知半解,不明白不能把直接可用的demo提供下载方式么,好费劲。。

[html] view plain copy
  1. Installation  
  2.   
  3. It is recommended that you install the PHPPresentation library through composer. To do so, addthe following lines to your composer.json.  
  4.   
  5. {  
  6.     "require": {  
  7.        "phpoffice/phppresentation": "dev-master"  
  8.     }  
  9. }  

Github 原话,我以为是加到下载的源代码里require部分加一行,后来新建空目录,新建新composer.json,将这句话复制进去,cmd运行composer install,安装vendor成功,目录看到我眼花缭乱。

目前还未成功,一些路径问题还待查看。

to be continued ...

********************************************************************************

2016年3月3日14:16:27 更新

七调八调,成功加载本地ppt并新建空白页,贴一下samples下 sample_12_Reader_PowerPoint2007.php 源码

  1. <?php  
  2.   
  3. set_time_limit(10);  
  4.   
  5. include_once 'Sample_Header.php';  
  6.   
  7. use PhpOffice\PhpPresentation\PhpPresentation;  
  8. use PhpOffice\PhpPresentation\DocumentLayout;  
  9. use PhpOffice\PhpPresentation\IOFactory;  
  10. use PhpOffice\PhpPresentation\Style\Alignment;  
  11. use PhpOffice\PhpPresentation\Style\Color;  
  12. use PhpOffice\PhpPresentation\Slide;  
  13. use PhpOffice\PhpPresentation\AbstractShape;  
  14. use PhpOffice\PhpPresentation\Shape\MemoryDrawing;  
  15. use PhpOffice\PhpPresentation\Shape\RichText;  
  16. use PhpOffice\PhpPresentation\Shape\Group;  
  17.   
  18. $pptReader = IOFactory::createReader('PowerPoint2007');  
  19. $oPHPPresentation =new PhpPresentation();  
  20. $oPHPPresentation = $pptReader->load('resources/Sample_12.pptx');  
  21.   
  22.   
  23. $oPHPPresentation->createSlide();  
  24.   
  25. $oWriterPPTX = IOFactory::createWriter($oPHPPresentation'PowerPoint2007');  
  26. $oWriterPPTX->save(__DIR__ . "/sample.pptx");  
  27.   
  28. if (!CLI) {  
  29.     include_once 'Sample_Footer.php';  
  30. }  
原来的样例是解析ppt文件树状结构,我这改成加载、新建、保存。


OVER

********************************************************************************

2016年5月9日16:14:55 更新

后来还是用了0.1.0版本,这是后话。

windows环境迁移linux下,报错。

具体原因未知,可能是

set_include_path(get_include_path() . PATH_SEPARATOR . 'pptphp/Classes/');

没发挥作用。

后来纠结了下,dirname ( __FILE__ )、require_once、include_once、include、require 等。

改成如下:

  1. /** PHPPowerPoint */  
  2. include  PPT_PATH.'PHPPowerpoint.php';  
  3.   
  4. /** PHPPowerPoint_IOFactory */  
  5. include PPT_PATH.'PHPPowerPoint/IOFactory.php';  
PPT_PATH全局变量表示PHPPowerPoint包的路径 pptphp/Classes/。

Classes下所有php文件全选替换成 include_once PPT_PATH.'xxxxx.php';  。 用require_once linux还是windows忘记哪一个了,还是报错。半路出家也不是很明白为啥 - - 

最后,linux下,应该是好了。因为,Fatal error: Class 'ZipArchive' not found,服务器缺php zip的支持有报错了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
操作PPT文档的PHP库,可以演示文档、添加幻灯片,支持图像、媒体的加入,作者的信息等等,并且完美兼容MS Office。本库配有十几个实际例子可以作为开发参考,而且配有对应的中文文档,使用起来相当方便,不需要在去啃英文单词。<?php date_default_timezone_set('UTC'); if (!defined('PHPPRESENTATION_TESTS_BASE_DIR')) {     define('PHPPRESENTATION_TESTS_BASE_DIR', realpath(__DIR__)); } $vendor = realpath(__DIR__ . '/../vendor'); if (file_exists($vendor . "/autoload.php")) {     require $vendor . "/autoload.php"; } else {     $vendor = realpath(__DIR__ . '/../../../');     if (file_exists($vendor . "/autoload.php")) {         require $vendor . "/autoload.php";     } else {         throw new Exception("Unable to load dependencies");     } } spl_autoload_register(function ($class) {     $class = ltrim($class, '\\');     $prefix = 'PhpOffice\\PhpPresentation\\Tests';     if (strpos($class, $prefix) === 0) {         $class = str_replace('\\', DIRECTORY_SEPARATOR, $class);         $class = join(DIRECTORY_SEPARATOR, array('PhpPresentation', 'Tests', '_includes')) .         substr($class, strlen($prefix));         $file = __DIR__ . DIRECTORY_SEPARATOR . $class . '.php';         if (file_exists($file)) {             require_once $file;         }     } }); require_once __DIR__ . "/../src/PhpPresentation/Autoloader.php"; \PhpOffice\PhpPresentation\Autoloader::register();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值