## 采用 PhpPresentation 插件生成PPT
下载方式 composer , composer.json里面增加, 然后 composer update
```
"require": {
"phpoffice/phppresentation": "dev-master"
},
```
git 地址: [https://github.com/PHPOffice/PHPPresentation](https://link.jianshu.com/?t=https%3A%2F%2Fgithub.com%2FPHPOffice%2FPHPPresentation)
手册地址:[http://phppowerpoint.readthedocs.io/en/latest/](https://link.jianshu.com/?t=http%3A%2F%2Fphppowerpoint.readthedocs.io%2Fen%2Flatest%2F)
demo
```
~~~
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;
class PhpOffice1
{
public function index()
{
// 2.创建ppt对象
$objPHPPowerPoint = new PhpPresentation();
// 3.设置属性
$objPHPPowerPoint->getDocumentProperties()->setCreator('PHPOffice')
->setLastModifiedBy('PHPPresentation Team')
->setTitle('Sample 02 Title')
->setSubject('Sample 02 Subject')
->setDescription('Sample 02 Description')
->setKeywords('office 2007 openxml libreoffice odt php')
->setCategory('Sample Category');
// 4.删除第一页(多页最好删除)
$objPHPPowerPoint->removeSlideByIndex(0);
//根据需求 调整for循环
for ($i = 1; $i <= 3; $i++) {
//创建幻灯片并添加到这个演示中
$slide = $objPHPPowerPoint->createSlide();
//创建一个形状(图)
$shape = $slide->createDrawingShape();
$shape->setName('内容图片name')
->setDescription('内容图片 描述')
->setPath(WEB_PATH . '/uploads/img/background.jpg')
->setResizeProportional(false)
->setHeight(720)
->setWidth(960);
//创建一个形状(文本)
$shape = $slide->createRichTextShape()
->setHeight(60)
->setWidth(960)
->setOffsetX(10)
->setOffsetY(50);
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$textRun = $shape->createTextRun('以后这个就是标题了');
$textRun->getFont()->setBold(true)
->setSize(20)
->setColor(new Color('FFE06B20'));
// 创建一个形状(文本)
$shape = $slide->createRichTextShape()
->setHeight(60)
->setWidth(960)
->setOffsetX()
->setOffsetY(700);
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
$textRun = $shape->createTextRun('时间:2017年10月19号');
$textRun->getFont()->setBold(true)
->setSize(10)
->setColor(new Color('FFE06B20'));
}
$oWriterPPTX = IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
//路径 /uploads/ppt/ 必须存在
$url = WEB_PATH . "/uploads/ppt/" . time() . ".pptx";
//生成PPT
$oWriterPPTX->save($url);
//下载PPT
download($url);
//删除PPT
deldir($url);
exit;
}
}
function download($file)
{
if(file_exists($file)){
header("Content-type:application/octet-stream");
$filename = basename($file);
header("Content-Disposition:attachment;filename = ".$filename);
header("Accept-ranges:bytes");
header("Accept-length:".filesize($file));
readfile($file);
}else{
echo "";
}
}
//删除文件
function deldir($dir)
{
unlink($dir);
closedir($dir);
}
~~~
```