PhpPresentation ppt 导出PPT

最近老大让我用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.jianshu.com/p/28309fb38bdb?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

https://www.5axxw.com/wiki/content/2f7vay

 

posted @ 2021-12-10 18:07  声声慢43  阅读(1191)  评论(0编辑  收藏  举报