PHPPresentation API使用方法整理,设置页面尺寸,绘制本地、网络图片,设置页面过渡效果,下载ppt
创建新ppt对象后,可以通过getActiveSlide获取默认的页面,也可以通过createSlide添加新页面
use PhpOffice\PhpPresentation\PhpPresentation; //创建PPT对象 $objPHPPresentation = new PhpPresentation(); //创建之后应该是默认有一个页面,可以直接获取 $currentSlide = $objPHPPresentation->getActiveSlide(); //或添加新页面 $slide = $objPHPPresentation->createSlide(); //删除页面 $objPHPPresentation->removeSlideByIndex(0);
也可以从ppt模板复制页面,不过我这边复制下载之后打开提示文件已损坏,需要修复,页面背景、文本框等无法复制,未找到原因
use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Shape\RichText; $objPHPPresentation = new PhpPresentation(); //新对象默认有一个slide,删除之 $objPHPPresentation->removeSlideByIndex(0); //读取ppt模板 $oReader = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007'); $oPresentation04 = $oReader->load(__DIR__ . '/results/Sample_04_Table.pptx'); //遍历模板的slide,添加到新PPT对象 foreach ($oPresentation04->getAllSlides() as $oSlide) { $objPHPPresentation->addExternalSlide($oSlide); }
默认的ppt尺寸比例为4:3,需要改为16:9,16:9的页面像素值为 960px:540px
use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\DocumentLayout; $objPHPPresentation = new PhpPresentation(); $layout = new DocumentLayout(); $layout->setDocumentLayout('screen16x9'); $objPHPPresentation->setLayout($layout); //可选的layout值 //const LAYOUT_CUSTOM = ''; //const LAYOUT_SCREEN_4X3 = 'screen4x3'; //const LAYOUT_SCREEN_16X10 = 'screen16x10'; //const LAYOUT_SCREEN_16X9 = 'screen16x9'; //const LAYOUT_35MM = '35mm'; //const LAYOUT_A3 = 'A3'; //const LAYOUT_A4 = 'A4'; //const LAYOUT_B4ISO = 'B4ISO'; //const LAYOUT_B5ISO = 'B5ISO'; //const LAYOUT_BANNER = 'banner'; //const LAYOUT_LETTER = 'letter'; //const LAYOUT_OVERHEAD = 'overhead';
添加本地图片
$objPHPPresentation = new PhpPresentation(); $currentSlide = $objPHPPresentation->getActiveSlide(); $pptDir = \Yii::$app->params['excel_tpl_sz_ppt']; //图片本地目录 $img1 = $pptDir.'01.png'; //图片本地地址 //设置图片绘制参数 $shape1 = new Drawing\File(); $shape1->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') ->setPath($img1) ->setWidth(960) //设置图片宽高 ->setHeight(540) ->setOffsetX(0) //设置图片在页面中的偏移量 ->setOffsetY(0); $currentSlide->addShape($shape1); //添加进页面
绘制网络图片之前,需要将图片先下载到本地,之后同绘制本地图片
$img = $this->download(' $shape1 = new Drawing\File(); $shape1->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') ->setPath($img) ->setWidth(960) //设置图片宽高 ->setHeight(540) ->setOffsetX(0) //设置图片在页面中的偏移量 ->setOffsetY(0); $currentSlide->addShape($shape1); //添加进页面 .......... private function download($url, $path = 'temp/'){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书 $file = curl_exec($ch); curl_close($ch); $filename = pathinfo($url, PATHINFO_BASENAME); $resource = fopen($path . $filename, 'a'); fwrite($resource, $file); fclose($resource); return $path . $filename; }
use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Style\Alignment; use PhpOffice\PhpPresentation\Style\Color; use PhpOffice\PhpPresentation\Shape\Drawing; use PhpOffice\PhpPresentation\Style\Bullet; //设置富文本对象,设置宽高、偏移 $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100) ->setWidth(940) ->setOffsetX(10) ->setOffsetY(243); //设置对齐方式 $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //设置项目符号 $shape->getActiveParagraph()->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET)->setBulletColor(new Color("FFFFFFFF"))->setBulletChar('•'); //设置文本内容 $textRun = $shape->createTextRun('北京3W孵化器管理有限公司'); //设置文本属性 $textRun->getFont()->setBold(true) ->setName("微软雅黑") ->setSize(36) ->setColor(new Color("FFFFFFFF"));
设置行高
$shape->getActiveParagraph()->setLineSpacing(100); //100代表一倍行间距,1.5倍则为150,以此类推
绘制多行文本,方式1
$textRun = $shape->createTextRun('Check the project site on GitHub:'); $shape->createBreak(); //换行 $textRun = $shape->createTextRun('https://github.com/PHPOffice/PHPPresentation/');
绘制多行文本,方式2
$shape->createTextRun("文本1"); $shape->createParagraph()->createTextRun("文本2"); $shape->createParagraph()->setLineSpacing(120)->createTextRun("文本3"); //可单独设置行间距
仍然通过绘制文本框的createRichTextShape,通过设置填充颜色,来绘制一个矩形区域
use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Style\Color; use PhpOffice\PhpPresentation\Style\Fill; .... $objPHPPresentation = new PhpPresentation(); $slide = $objPHPPresentation->createSlide(); //设置矩形宽高及位置 $shape = $slide->createRichTextShape() ->setHeight(200) ->setWidth(420) ->setOffsetX(59) ->setOffsetY(218); //填充颜色 $shape->getFill() ->setFillType(Fill::FILL_SOLID) ->setStartColor(new Color( 'FF4AC4A9' )) ->setEndColor(new Color( 'FF4AC4A9' ));
设置页面放映时的动画
use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Slide\Transition; $objPHPPresentation = new PhpPresentation(); $slide = $objPHPPresentation->createSlide(); $oTransition = new Transition(); $oTransition->setManualTrigger(false); //是否手动触发,设为true为手动触发,false为自动,自动配合下面的时间触发 $oTransition->setTimeTrigger(true, 4000);//设置时间触发,4s之后触发,自动进入下一页 $oTransition->setTransitionType(Transition::TRANSITION_PULL_LEFT); $slide->setTransition($oTransition);
可选的过渡动画有
const TRANSITION_BLINDS_HORIZONTAL = 'blinds_horz'; const TRANSITION_BLINDS_VERTICAL = 'blinds_vert'; const TRANSITION_CHECKER_HORIZONTAL = 'checker_horz'; const TRANSITION_CHECKER_VERTICAL = 'checker_vert'; const TRANSITION_CIRCLE_HORIZONTAL = 'circle_horz'; const TRANSITION_CIRCLE_VERTICAL = 'circle_vert'; const TRANSITION_COMB_HORIZONTAL = 'comb_horz'; const TRANSITION_COMB_VERTICAL = 'comb_vert'; const TRANSITION_COVER_DOWN = 'cover_d'; const TRANSITION_COVER_LEFT = 'cover_l'; const TRANSITION_COVER_LEFT_DOWN = 'cover_ld'; const TRANSITION_COVER_LEFT_UP = 'cover_lu'; const TRANSITION_COVER_RIGHT = 'cover_r'; const TRANSITION_COVER_RIGHT_DOWN = 'cover_rd'; const TRANSITION_COVER_RIGHT_UP = 'cover_ru'; const TRANSITION_COVER_UP = 'cover_u'; const TRANSITION_CUT = 'cut'; const TRANSITION_DIAMOND = 'diamond'; const TRANSITION_DISSOLVE = 'dissolve'; const TRANSITION_FADE = 'fade'; const TRANSITION_NEWSFLASH = 'newsflash'; const TRANSITION_PLUS = 'plus'; const TRANSITION_PULL_DOWN = 'pull_d'; const TRANSITION_PULL_LEFT = 'pull_l'; const TRANSITION_PULL_RIGHT = 'pull_r'; const TRANSITION_PULL_UP = 'pull_u'; const TRANSITION_PUSH_DOWN = 'push_d'; const TRANSITION_PUSH_LEFT = 'push_l'; const TRANSITION_PUSH_RIGHT = 'push_r'; const TRANSITION_PUSH_UP = 'push_u'; const TRANSITION_RANDOM = 'random'; const TRANSITION_RANDOMBAR_HORIZONTAL = 'randomBar_horz'; const TRANSITION_RANDOMBAR_VERTICAL = 'randomBar_vert'; const TRANSITION_SPLIT_IN_HORIZONTAL = 'split_in_horz'; const TRANSITION_SPLIT_OUT_HORIZONTAL = 'split_out_horz'; const TRANSITION_SPLIT_IN_VERTICAL = 'split_in_vert'; const TRANSITION_SPLIT_OUT_VERTICAL = 'split_out_vert'; const TRANSITION_STRIPS_LEFT_DOWN = 'strips_ld'; const TRANSITION_STRIPS_LEFT_UP = 'strips_lu'; const TRANSITION_STRIPS_RIGHT_DOWN = 'strips_rd'; const TRANSITION_STRIPS_RIGHT_UP = 'strips_ru'; const TRANSITION_WEDGE = 'wedge'; const TRANSITION_WIPE_DOWN = 'wipe_d'; const TRANSITION_WIPE_LEFT = 'wipe_l'; const TRANSITION_WIPE_RIGHT = 'wipe_r'; const TRANSITION_WIPE_UP = 'wipe_u'; const TRANSITION_ZOOM_IN = 'zoom_in'; const TRANSITION_ZOOM_OUT = 'zoom_out';
header("Content-type:application/vnd.ms-powerpoint;"); header("Content-Disposition:attachment;filename="."cesshi.pptx"); header('Cache-Control: max-age=0'); $oWriterPPTX = IOFactory::createWriter($objPHPPresentation, 'PowerPoint2007'); $oWriterPPTX->save("php://output");
PHPWord使用方法整理:https://www.shanhuxueyuan.com/news/detail/124.html
PHPExcel使用方法整理:https://www.shanhuxueyuan.com/news/detail/119.html
-END-
点赞(4)