1. 程式人生 > >TP框架下,如何生成PPT文件

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

tle ges pen sets current $_server 斷開 rect efi

場景說明: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開發公司燚軒科技整理發布,如需轉載請註明出處。

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