美文网首页
PHP导出PPT

PHP导出PPT

作者: dongshixiao | 来源:发表于2018-03-25 11:07 被阅读0次

    正在做的一个广告公司的项目,里面有将Excel导出的功能和导出PPT的功能,导出PPT的我们应该怎么办呢?以前也没有接触过导出PPT这块,就在github上找到一个.查查手册,再此处记录下.

    比如一个订单下面,有很多图片,我们需要把这么多图片导出来,给下订单的客户看.就需要这种需求

    首先感谢作者:https://github.com/PHPOffice/PHPPresentation

    手册地址:http://phppowerpoint.readthedocs.io/en/latest/

    项目使用laravel,直接在composer.json中添加

    {    
        "require": 
            {      
                "phpoffice/phppresentation": "dev-master"
        }
    }
    

    贴一个简单demo

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use PhpOffice\PhpPresentation\PhpPresentation;
    use PhpOffice\PhpPresentation\IOFactory;
    use PhpOffice\PhpPresentation\Style\Color;
    use PhpOffice\PhpPresentation\Style\Alignment;
    
    class IndexController extends Controller
    {
        public function index()
        {
            //todo 1.开始引入所有需要的文件
            /*
            use PhpOffice\PhpPresentation\PhpPresentation;
            use PhpOffice\PhpPresentation\IOFactory;
            use PhpOffice\PhpPresentation\Style\Color;
            use PhpOffice\PhpPresentation\Style\Alignment;
            */
    
            //todo 2.创建ppt对象
            $objPHPPowerPoint = new PhpPresentation();
    
            //todo 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');
    
            //todo 4.删除第一页(多页最好删除)
            $objPHPPowerPoint->removeSlideByIndex(0);
    
            //todo 5.创建幻灯片
    
    
            //根据需求 调整for循环
            for ($i=1;$i<=3;$i++){
                //创建幻灯片并添加到这个演示中
                $slide = $objPHPPowerPoint->createSlide();
                //创建一个形状(文本)
                $shape = $slide->createRichTextShape()
                    ->setHeight(60)
                    ->setWidth(1000)
                    ->setOffsetX()
                    ->setOffsetY(50);
                $shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
                $textRun = $shape->createTextRun('以后这个就是标题了');
                $textRun->getFont()->setBold(true)
                    ->setSize(20)
                    ->setColor(new Color('FFE06B20'));
    
                //创建一个形状(图)
                $shape = $slide->createDrawingShape();
                $shape->setName('内容图片name')
                    ->setDescription('内容图片 描述')
                    ->setPath('./'.$i.'.jpg')
                    /*->setHeight(600)
                    ->setWidth(750)*/
                    ->setOffsetX(50)
                    ->setOffsetY(100);
                $shape->setWidthAndHeight(850,600);
                $shape->getShadow()->setVisible(true)
                    ->setDirection(45)
                    ->setDistance(10);
    
                // 创建一个形状(文本)
                $shape = $slide->createRichTextShape()
                    ->setHeight(60)
                    ->setWidth(900)
                    ->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');
            $oWriterPPTX->save("sample.pptx");
        }
    }
    贴一个可能在导出中会用到的几个方法:
        //下载文件
        function download($url, $path = 'temp_image/')
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            $file = curl_exec($ch);
            curl_close($ch);
            $filename = pathinfo($url, PATHINFO_BASENAME);
            $resource = fopen($path . $filename, 'a');
            fwrite($resource, $file);
            fclose($resource);
            return './' . $path . $filename;
        }
    
            //清空文件夹
        function deldir($dir)
        {
            //删除目录下的文件:
            $dh = opendir($dir);
    
            while ($file = readdir($dh)) {
                if ($file != "." && $file != "..") {
                    $fullpath = $dir . "/" . $file;
    
                    if (!is_dir($fullpath)) {
                        unlink($fullpath);
                    } else {
                        deldir($fullpath);
                    }
                }
            }
    
            closedir($dh);
    
        }
    

    需要更多功能就去查看手册吧!!!

    相关文章

      网友评论

          本文标题:PHP导出PPT

          本文链接:https://www.haomeiwen.com/subject/egqaqftx.html