phppresentation 踩坑指南

介绍

这个包是 github.com/PHPOffice 家族用于导出ppt用的.

安装

composer require phpoffice/phppresentation

默认安装的是master分支的,建议安装develop分支.master分支3年前已经停止合并了,develop分支仍在维护,有相当一部分bug是最近几年修改的.如我遇到的导出的ppt在office显示正常,但是在wps上的文本框中的内容位置错乱.

composer require phpoffice/phppresentation dev-develop

开始

创建ppt

$presentation = new PhpPresentation();

// 移除0索引
$presentation->removeSlideByIndex(0);

// 设置长宽比
$layout = new DocumentLayout();
// 长宽比,在PowerPoint2007下实际长宽是25cm和15cm左右,但是2016之后的office版本是32cm和24cm,产品给模板的时候得按照这个长宽比给,不然会有偏差
$layout->setDocumentLayout('screen16x9');
$presentation->setLayout($layout);

// 写一些页面
$this->generateSheet($presentation);

$oWriterPPTX = IOFactory::createWriter($presentation, 'PowerPoint2007');
$path = storage_path('ppts/导出ppt' . now()->format('YmdHis') . '.pptx');
if (!file_exists(storage_path('ppts'))) {
 mkdir(storage_path('ppts'));
}

// 生成ppt
$oWriterPPTX->save($path);
return $path;

生成sheet

private function sheet(PhpPresentation $presentation)
{
    $currentSlide = $presentation->createSlide();
    // 创建画布
    $shape = $currentSlide->createDrawingShape();

    // 背景图,07版ppt下长宽为960像素和540像素
    $this->createBackImage($shape, 'xxx.png', 960, 540);

    // 生成文本框
    $this->createText(...$params);

    // 添加画布图片
    $this->addImage(...$params);
}

背景图

private function createBackImage(AbstractShape $shape, $url, $width, $height, $offsetX = 0, $offsetY = 0)
{
      $url = public_path("imgs/{$url}");

    // 设置图片属性
      $shape->setPath($url) // 本地图片路径
     ->setWidth($width) // 设置宽
     ->setHeight($height) // 设置高
     ->setOffsetX($offsetX) // 设置x轴偏移量
     ->setOffsetY($offsetY); // 设置y轴偏移量
}

文本内容

public function createText(
        Slide $slide,
        $width,
        $height,
        $content,
        $size,
        $color,
        $horizontalAlign = 'ctr',
        $verticalAlign = 'ctr',
        $offsetX = 0,
        $offsetY = 0,
        $fillColor = Color::COLOR_WHITE,
        $font = '微软雅黑',
        $bold = false
    ) {
    // 填充属性
    $fill = new Fill();
    $fill->setFillType(Fill::FILL_SOLID)
         ->setStartColor(new Color($fillColor))
         ->setEndColor(new Color($fillColor));
    // 文本框属性
    $shape = $slide->createRichTextShape() // 创建文本框
        ->setFill($fill) // 设置背景填充
        ->setWidth($width)
        ->setHeight($height)
        ->setOffsetX($offsetX)
        ->setOffsetY($offsetY);
    // 段落属性
    $shape->getActiveParagraph()
        ->getAlignment()
        ->setHorizontal($horizontalAlign)
        ->setVertical($verticalAlign);
    // 生成
    $textRun = $shape->createTextRun($content);
    // 字体属性
    $textRun->getFont()
        ->setName($font)
        ->setBold($bold)
        ->setSize($size)
        ->setColor(new Color($color));
}

放置图片

public function addImage(Slide $slide, $url, $width, $height, $offsetX = 0, $offsetY = 0)
{
    $shape = new Base64();
    $shape->setResizeProportional(false);
    // 这里与上面不同的是使用了获取数据流转base64使用网络图片填充图片的方式
    $shape->setData("data:image/jpeg;base64," . base64_encode(file_get_contents($url)))
        ->setWidth($width)   //设置图片宽高
        ->setHeight($height)
        ->setOffsetX($offsetX)   //设置图片在页面中的偏移量
        ->setOffsetY($offsetY);

        $slide->addShape($shape);
}

以上就是我在这段时间使用该插件所用到的一些方法了,能解决绝大部分问题了.如果还有其他更多的需求如设置过场动画,都可以阅读官方文档 phppresentation.readthedocs.io/en/... 找到.

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1

xayts.com/post/php-exports-ppt-wit...

借鉴了老哥的文章,又加了一些东西,形成了一篇文章,请指点

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
1
粉丝
1
喜欢
1
收藏
2
排名:2675
访问:323
私信
所有博文
社区赞助商