TP框架下,如何生成PPT文件

  场景说明:PPT(power point的缩写)现已成为办公生活商务来往必不可少的部分,网站生成ppt也自然会成为一种需求,如何实现这种需求将是郑州app开发公司燚轩科技加下来和大家讨论的重点。TP框架下,引入第三方开源类库。此处仅以PHPPowerpoint 0.1.0版为例进行展示说明。

  步骤如下

  1. 将PHPPowerpoint下的Classes文件夹引入到Vendor下。

  2. 然后就可以使用了,注意要调整include_path,否则会导致引用失败。

  3. 代码中分享了新建ppt空白页,填充图片,填充文字,设置文字样式等方法

  4. 最后,既然要导出ppt,那么需要下载方法。普通下载方法会出错,参考大神的下载方法完美解决问题,这里一并贴出。

  5. 注意:生成ppt的路径必须存在。否则会生成失败linux下注意写入权限。

  代码如下:

  function get_ppt_handle(){

  //设置include_path

  define('DS', DIRECTORY_SEPARATOR);

  define('ROOT', VENDOR_PATH );

  set_include_path(get_include_path() . PATH_SEPARATOR . ROOT . 'Classes');

  //引入类库

  require_once VENDOR_PATH.'Classes/PHPPowerPoint.php';

  //实例化ppt类

  $yxppt = new PHPPowerPoint();

  /新幻灯片/

  //移除第一张空白页(实例化后自动生成的)

  $yxppt->removeSlideByIndex(0);

  //生成第一张幻灯片,方法在下方定义

  $firstSlide = $this->createFirstSlide($yxppt);

  $list = $this->get_all_ppts();

  //循环生成中间的幻灯片

  foreach($list as $v){

  //图片:产品图片

  $img= $_SERVER['DOCUMENT_ROOT'] .$v['info']['img'];

  $currentSlide = $this->createTemplatedSlide($yxppt,$img); // local function

  //标题:产品名称

  $shape_Title = $currentSlide->createRichTextShape();

  $shape_Title->setWidth(1000);

  $shape_Title->setHeight(40);

  $shape_Title->setOffsetX(70);

  $shape_Title->setOffsetY(150);

  $shape_Title->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );

  $shape_Title->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER );

  $strtext = $v['info']['name'];

  $textRun = $shape_Title->createTextRun($strtext);

  $textRun->getFont()->setSize(20);

  $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '333333' ) );

  }

  //生成最后一张幻灯片

  $lastSlide = $this->createLastSlide($yxppt);

  //保存PPTX 文件, 使用 2007 格式

  include_once 'PHPPowerPoint/IOFactory.php';

  $objWriter = PHPPowerPoint_IOFactory::createWriter($yxppt, 'PowerPoint2007');

  //保存文件

  $map['id'] = $_SESSION['uid'];

  $userinfo = M('users')->where($map)->find();

  $objWriter->save( . 'userppt/'.$userinfo['username'].'_PPT.pptx');

  $this->download( . 'userppt/'.$userinfo['username'].'_PPT.pptx');

  }

  function createTemplatedSlide(PHPPowerPoint $objPHPPowerPoint, $img='')

  {

  // Create slide

  $slide = $objPHPPowerPoint->createSlide();

  $shape = $slide->createDrawingShape();

  $shape->setName('Background');

  $shape->setDescription('Background');

  $shape->setPath($img);

  $shape->setWidth(500);

  $shape->setHeight(400);

  $shape->setOffsetX(70);

  $shape->setOffsetY(200);

  // Add logo

  $shape = $slide->createDrawingShape();

  $shape->setName('PHPPowerPoint logo');

  $shape->setDescription('PHPPowerPoint logo');

  $shape->setPath(.'Public/Home/images/3.png');

  $shape->setWidth(950);

  $shape->setHeight(40);

  $shape->setOffsetY(50);

  return $slide;

  }

  function createFirstSlide(PHPPowerPoint $objPHPPowerPoint){

  $slide = $objPHPPowerPoint->createSlide();

  $shape = $slide->createDrawingShape();

  $shape->setName('Background');

  $shape->setDescription('Background');

  $shape->setPath(.'Public/Home/images/1.png');

  $shape->setWidth(950);

  $shape->setHeight(720);

  $shape->setOffsetX(0);

  $shape->setOffsetY(0);

  }

  function createLastSlide(PHPPowerPoint $objPHPPowerPoint){

  $slide = $objPHPPowerPoint->createSlide();

  $shape = $slide->createDrawingShape();

  $shape->setName('Background');

  $shape->setDescription('Background');

  $shape->setPath(.'Public/Home/images/2.png');

  $shape->setWidth(950);

  $shape->setHeight(720);

  $shape->setOffsetX(0);

  $shape->setOffsetY(0);

  }

  function fileReader($file_url){

  // 这个函数其实就是借用了php的file相关函数,从fopen,fread,fclose,所以跟thinkphp没有多大关系

  $file=fopen($file_url,'r');

  // 获取文件路径中最后的文件名部分

  $file_name=basename($file_url);

  header("Content-type: application/octet-stream");

  header("Accept-Ranges: bytes");

  header("Accept-Length: ".filesize($file_url));

  header("Content-Disposition: attachment; filename=".$file_name);

  // 这里一定要使用echo 进行输出,否则下载的文家是空白的

  echo fread($file,filesize($file_url));

  // 断开链接

  fclose($file_url);

  }

  function download($file_url,$new_name=''){

  $file_url=iconv('utf-8','gb2312',$file_url);

  //将编码转为支持中英文的gb2312编码

  if(!isset($file_url)||trim($file_url)==''){

  return '500';

  }

  if(!file_exists($file_url)){ //检查文件是否存在

  return '404';

  }

  $file_name=basename($file_url);

  $file_type=explode('.',$file_url);

  $file_type=$file_type[count($file_type)-1];

  $file_name=trim($new_name=='')?$file_name:urlencode($new_name).'.'.$file_type;

  //输入文件标签

  header("Content-type: application/octet-stream");

  header("Accept-Ranges: bytes");

  header("Accept-Length: ".filesize($file_url));

  header("Content-Disposition: attachment; filename=".$file_name);

  $file_type=fopen($file_url,'r'); //打开文件

  //输出文件内容

  $file_size=filesize($file_url);//获取文件大小

  $buffer=1024; //定义1KB的缓存空间

  $file_count=0; //计数器,计算发送了多少数据

  while(!feof($file_type) && ($file_size>$file_count)){

  //如果文件还没读到结尾,且还有数据没有发送

  $senddata=fread($file_type,$buffer);

  //读取文件内容到缓存区

  $file_count+=$senddata;

  echo $senddata;

  }

  //echo fread($file_type,filesize($file_url));

  fclose($file_type);

  }

  到这里就算是结束了,现在大家也可以动手尝试一下,如果对于文中还存在有不理解或者需要指正的地方,可以留言,同时也欢迎各路大神批评指正。

  本文由郑州app开发公司燚轩科技整理发布,如需转载请注明出处。

猜你喜欢

转载自blog.51cto.com/13686158/2138550