最近老大讓我用php做一個生成並下載PPT的功能,從一無所知,到慢慢做成,成就感還是滿滿的,現在將最近的收獲記錄下來,以備后用。
通過百度,發現有兩個可以生成ppt的工具,一個是PhpPresentation插件,一個是phppowerpoint,但是phppowerpoint已經暫停使用了,只能用PhpPresentation插件來整一下。

要求:PHP版本>php7.1
1.下載 PhpPresentation插件
下載git地址:https://github.com/PHPOffice/PHPPresentation,我用的是develop分支的代碼
composer下載方式:composer require phpoffice/phppresentation dev-develop
據說是master 分支 3 年前已經停止合並了,develop 分支仍在維護,有相當一部分 bug 是最近幾年修改的
手冊地址:https://phppowerpoint.readthedocs.io/en/latest/
2.學習PHPPresentation/samples中的用例
學習用例,一來配置一下環境,二來看是否能快速找到自己想要的功能。
在執行php Sample_04_Table.php時,報錯缺少Common\Adapter\Zip\ZipArchiveAdapter,這個Common目錄為https://github.com/PHPOffice/Common
忘記自己當時怎么想的了,,我是將Common目錄直接放到了PhpPresentation中,修改了一下namespace中的路徑什么的,,最終達到的效果是執行php Sample_04_Table.php能夠成功ppt。
我使用的PhpPresentation 代碼為PhpPresentation.zip
3.將附件運用到wamp環境中
1)將PhpPresentation目錄放到Include/Library/Vendor/目錄
2)在xxController.class.php的導出ppt方法中引入PhpPresentation
vendor('PHPPresentation.src.PhpPresentation.Autoloader','','.php');
\PhpPresentation\Autoloader::register();
3)使用
(1)新建ppt對象,設置幻燈片大小
#新建ppt對象 $pptObj=new \PhpPresentation\PhpPresentation(); $layout = new \PhpPresentation\DocumentLayout(); #設置幻燈片大小 ##指定長寬 $layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_CUSTOM,true) #set scale ->setCX(959,\PhpPresentation\DocumentLayout::UNIT_POINT) ->setCY(540,\PhpPresentation\DocumentLayout::UNIT_POINT); #33.83cm*19.05cm
$layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_CUSTOM,true) #set scale ->setCX(1280,\PhpPresentation\DocumentLayout::UNIT_PIXEL) ->setCY(720,\PhpPresentation\DocumentLayout::UNIT_PIXEL); #單位是像素px
$layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_CUSTOM,true) #set scale ->setCX(33.87,\PhpPresentation\DocumentLayout::UNIT_CENTIMETER) ->setCY(19.05,\PhpPresentation\DocumentLayout::UNIT_CENTIMETER); #1280x720 ##指定比例 $layout->setDocumentLayout(\PhpPresentation\DocumentLayout::LAYOUT_SCREEN_16X9); $pptObj->setLayout($layout); #移除首頁
$pptObj->removeSlideByIndex(0); //remove first page
(2)設置slide背景樣式
如果你有ppt模板的話,是可以導入模板的,但是我一開始不知道,所以我定義了一個方法,將需要的版式截圖作為背景插入到slide中
public function createTemplatedSlide(\PhpPresentation\PhpPresentation $pptObj,$title){ #title 每個slide最上邊的標題 // Create slide $slide = $pptObj->createSlide(); // Add background 實例化背景圖片對象 $oBkgImage = new \PhpPresentation\Slide\Background\Image(); $oBkgImage->setPath('./Public/Images/ppt_middle.png'); #ppt_middle.png背景圖片 $slide->setBackground($oBkgImage); //add title in text shape $shape = $slide->createRichTextShape() ->setHeight(60) ->setWidth(1200) ->setOffsetX(60) ->setOffsetY(20); $shape->getActiveParagraph()->getAlignment()->setHorizontal(\PhpPresentation\Style\Alignment::HORIZONTAL_LEFT); $textRun = $shape->createTextRun($title); $textRun->getFont()->setBold(true) ->setSize(32) ->setColor(new \PhpPresentation\Style\Color('FFB60005')); return $slide; }
同理,首頁、最后一頁這些特殊頁,都可以用這中插入圖片做背景的方式來實現,咋方便咋來
(3)生成表格
#實例化table對象 $shape = $pptObj->createTableShape(2); #2是列數 #設置表格大小,以及偏移量 $shape->setHeight(300)->setWidth(800)->setOffsetX(120)->setOffsetY(120); #創建行 $row = $shape->createRow(); $oCell = $row->nextCell(); $oCell->setWidth(202); $text=$oCell->createTextRun('test'); $text->getFont()->setSize(16)->setBold(true)->setColor(new \PhpPresentation\Style\Color('FFFFFFFF')); #白色字體 $oCell = $row->nextCell(); $oCell->setWidth(104); $text=$oCell->createTextRun('test2'); $text->getFont()->setSize(16)->setBold(true)->setColor(new \PhpPresentation\Style\Color('FFFFFFFF')); #白色字體 $row->setHeight(40); #設置行高 $row->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID) #設置行的填充顏色 ->setStartColor(new \PhpPresentation\Style\Color('FFB60005')) ->setEndColor(new \PhpPresentation\Style\Color('FFB60005')); foreach ($row->getCells() as $cell){ #設置單元格的border顏色,FFFFFF為白色,即不要邊界線 getTop/getLeft/getRight/getBottom $cell->getBorders()->getTop()->setColor(new \PhpPresentation\Style\Color('FFFFFFFF')); #設置單元格中的字體水平垂直居中 $cell->getActiveParagraph()->getAlignment()->setHorizontal(\PhpPresentation\Style\Alignment::HORIZONTAL_CENTER) ->setVertical(\PhpPresentation\Style\Alignment::VERTICAL_CENTER ); }
(4)生成折線圖
#兩條線 $line_data=array('line1'=>array('Monday 01' => 2,'Tuesday 02' => 5,'Wednesday 03' => 3,'Thursday 04' => 7,'Friday 05' => 4,'Saturday 06' => 9,'Sunday 07' => 7),
'line2'=>array('Monday 01' => 12,'Tuesday 02' => 15,'Wednesday 03' => 13,'Thursday 04' => 17,'Friday 05' => 14,'Saturday 06' => 19,'Sunday 07' => 17)); #創建chart實例 $shape1 = $pptObj->createChartShape(); $shape1->setResizeProportional(false)->setHeight(260)->setWidth(880) ->setOffsetX(80)->setOffsetY(280); $shape1->getTitle()->setVisible(false); //$shape1->getPlotArea()->getAxisX()->setMajorUnit(1); #設置legend $shape1->getLegend()->setVisible(true); $shape1->getLegend()->getFont()->setSize(12); $shape1->getLegend()->getBorder()->setLineStyle(\PhpPresentation\Style\Border::LINE_NONE); #設置x軸,y軸 $oOutlineAxisX = new \PhpPresentation\Style\Outline(); //$oOutlineAxisX->setWidth(400000000); $oOutlineAxisX->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID) ->setStartColor(new \PhpPresentation\Style\Color('FF808080')); $oOutlineAxisY = new \PhpPresentation\Style\Outline(); //$oOutlineAxisY->setWidth(400000000); $oOutlineAxisY->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID) ->setStartColor(new \PhpPresentation\Style\Color('FF808080')); $shape1->getPlotArea()->getAxisX()->setOutline($oOutlineAxisX); #顯示x軸線 $shape1->getPlotArea()->getAxisY()->setOutline($oOutlineAxisY); #顯示y軸線 $shape1->getPlotArea()->getAxisX()->setTitle(''); #不要標題 $shape1->getPlotArea()->getAxisY()->setTitle(''); $shape1->getPlotArea()->getAxisX()->setMajorTickMark(\PhpPresentation\Shape\Chart\Axis::TICK_MARK_OUTSIDE); #x軸上的刻度 $shape1->getPlotArea()->getAxisY()->setMajorTickMark(\PhpPresentation\Shape\Chart\Axis::TICK_MARK_OUTSIDE); #設置網格 $oGridLines2 = new \PhpPresentation\Shape\Chart\Gridlines(); $oGridLines2->getOutline()->setWidth(10); $oGridLines2->getOutline()->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID) ->setStartColor(new \PhpPresentation\Style\Color('FF808080')); $shape1->getPlotArea()->getAxisY()->setMajorGridlines($oGridLines2); #只設置了y軸上有網格 #畫線 $color=array('FFB60005','FFF08080','FF808080','FF4169E1','FFFF8C00'); $marker=array('diamond','square','triangle','x','star'); #circle,plus $i=0; $lineChart = new \PhpPresentation\Shape\Chart\Type\Line(); foreach($line_data as $name=>$vals){ $series = new \PhpPresentation\Shape\Chart\Series($name, $vals); $series->setShowSeriesName(false); $series->setShowValue(false); $series->setShowLeaderLines(false); #set marker $series->getMarker()->setSymbol($marker[$i]); if(in_array($marker[$i],array('circle','square','diamond','triangle'))){ $series->getMarker()->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID); $series->getMarker()->getFill()->setStartColor(new \PhpPresentation\Style\Color($color[$i])); } $series->getMarker()->getBorder()->setColor(new \PhpPresentation\Style\Color($color[$i])); $series->getMarker()->setSize(5); #set color $oOutline = new \PhpPresentation\Style\Outline(); $oOutline->getFill()->setFillType(\PhpPresentation\Style\Fill::FILL_SOLID); $oOutline->getFill()->setStartColor(new \PhpPresentation\Style\Color($color[$i])); $oOutline->setWidth(150000000); $series->setOutline($oOutline); $lineChart->addSeries($series); $i=$i+1; } $shape1->getPlotArea()->setType($lineChart);
4.下載ppt
$ppt_name="test.pptx"; header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation"); header("Content-Disposition: attachment; filename=$ppt_name"); $oWriterPPTX = \PhpPresentation\IOFactory::createWriter($pptObj,'PowerPoint2007'); $oWriterPPTX->save('php://output'); unlink($ppt_name); exit;
以上只作為工作后的常用命令總結,不保證完全正確性!
參考:https://learnku.com/articles/45161
https://blog.csdn.net/weixin_42412237/article/details/113765765
https://www.5axxw.com/wiki/content/2f7vay
