场景说明: 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);
- }
到这里就算是结束了, 现在大家也可以动手尝试一下, 如果对于文中还存在有不理解或者需要指正的地方, 可以留言, 同时也欢迎各路大神批评指正.
来源: http://www.bubuko.com/infodetail-2675628.html