05 12 2021
//简单设置自动载入函数.
set_include_path(EXTEND_PATH.'ppt/');//下载包我放在tp框架中的extend目录下(关键)
require_once 'PHPPowerPoint.php';
require_once 'PHPPowerPoint/IOFactory.php';
//新建立一个 PHPPowerPoint 对象.
$objPHPPowerPoint = new \PHPPowerPoint();
$currentSlide = $objPHPPowerPoint->getActiveSlide();//获取首页
//设置背景图片
$shape = $currentSlide->createDrawingShape();
$shape->setName('Background');
$shape->setDescription('Background');
$shape->setPath('图片路径,远程不行');
$shape->setWidth(1.34);
$shape->setHeight(720);
$shape->setOffsetX(0);
$shape->setOffsetY(0);  
//添加文字
$shape = $currentSlide->createRichTextShape();
$shape->setWidth(580);
$shape->setHeight(580);
$shape->setOffsetX(200);
$shape->setOffsetY(200);
$shape->getAlignment()->setHorizontal( \PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER );
$shape->getAlignment()->setVertical( \PHPPowerPoint_Style_Alignment::VERTICAL_CENTER );
$textRun = $shape->createTextRun('展示的文字');
$textRun->getFont()->setBold(true);//字体加粗
$textRun->getFont()->setSize(44);//字体大小
$textRun->getFont()->setColor( new \PHPPowerPoint_Style_Color( '101010' ) );//字体颜色
//当前位置隔行在添加文字
$shape->createBreak();	
$textRun = $shape->createTextRun('第二行文字');
$textRun->getFont()->setSize(32);
$textRun->getFont()->setColor( new \PHPPowerPoint_Style_Color( '101010' ) );
//生成ppt并且自动下载
$fileName = 'ppt.pptx';
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename=".$fileName);
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); 
$objWriter = \PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(WEB_ROOT . 'template/static/images/'.$fileName);//存放的本地路径
$objWriter->save('php://output');//下载
延伸阅读
    发表评论