PhpPresentation

本文介绍如何使用PHPPresentation库创建和编辑PPT文件,包括设置文档属性、添加文本框、插入图片、绘制表格等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHPPresentation

说明

使用方式有:

  1. 直接下载文件(本文使用)
  2. composer下载
  3. git下载

文档

Welcome to PHPPresentation’s documentation

需要下载的文件:

  1. PHPPresentation

    Gitee

    GitHub

  2. Common

使用

引入自动加载类

require_once '../../PhpOffice/PHPPresentation/src/PhpPresentation/Autoloader.php' ;
\ PhpOffice \ PhpPresentation \ Autoloader :: register ();
require_once '../../PhpOffice/Common/src/Common/Autoloader.php';
\ PhpOffice \ Common \ Autoloader :: register ();

use 一些常用的类

例:若出现Class 'PhpPresentation' not found则需 use PhpPresentation类

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Fill;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\DocumentLayout;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Shape\Drawing;

实例化PHPPresentation

$objPHPPowerPoint = new PhpPresentation();

设置PPT信息

$objPHPPowerPoint->getDocumentProperties()->setCreator('PHPOffice')
    ->setLastModifiedBy('PHPPresentation Team')
    ->setTitle('Sample 19 SlideMaster')
    ->setSubject('Sample 19 Subject')
    ->setDescription('Sample 19 Description')
    ->setKeywords('office 2007 openxml libreoffice odt php')
    ->setCategory('Sample Category');

设置长宽

// 方式1
$layout = new DocumentLayout();
$layout->setDocumentLayout(DocumentLayout::LAYOUT_SCREEN_16X10); 
$objPHPPowerPoint->setLayout($layout);

//方式2
$objPHPPowerPoint->getLayout()->setDocumentLayout(DocumentLayout::LAYOUT_CUSTOM, true)
        ->setCX(33.87, DocumentLayout::UNIT_CENTIMETER)
        ->setCY(19.05, DocumentLayout::UNIT_CENTIMETER);//长宽:1280  720

方式2需要在\PHPPresentation\src\PhpPresentation\DocumentLayout.php文件的131行和132行中间加一个break;

获取第一张幻灯片

$currentSlide = $objPHPPowerPoint -> getActiveSlide();

创建形状(文本) 添加文本框

// 添加文本框
$shape = $currentSlide->createRichTextShape()
    ->setHeight(30) //高
    ->setWidth(500) //宽
    ->setOffsetX(30) //x轴偏移量
    ->setOffsetY(25); //y轴偏移
// 设置行距
$shape->getActiveParagraph()->setLineSpacing(300)
// 设置文本框样式
$shape->getBorder()
    ->setLineStyle(\PhpOffice\PhpPresentation\Style\Border::LINE_SINGLE) //单行线
    ->setLineWidth(2) //宽度
    ->setDashstyle(\PhpOffice\PhpPresentation\Style\Border::DASH_SYSDASH) //虚线
    ->getColor()->setARGB(\PhpOffice\PhpPresentation\Style\Color::COLOR_DARKBLUE);//颜色
// 设置文本框内字体对齐方式
$shape->getActiveParagraph()->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT)//水平向左
    ->setVertical(Alignment::VERTICAL_BOTTOM)// 垂直底部
    ->setTextDirection(Alignment::TEXT DIRECTION HORIZONTAL);//文字方向水平
// 设置文字
$textRun = $shape->createTextRun('文本文字');
// 设置文字样式
$textRun->getFont()->setBold(true)// 是否加粗
            ->setSize(18) //字体大小
            ->setName('微軟正黑體')//字体样式
    		->setColor(new Color('FF000000');//字体颜色
// 设置文本框填充色
$shape->getFill()->setFillType(Fill::FILL_SOLID)->setRotation(90)->setStartColor(new Color( 'FF4672A8' ))->setEndColor(new Color( 'FF4672A8' ));

设置PPT背景图片

$imagePath = '';
$oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Image();
$oBackground->setPath($imagePath);
$currentSlide->setBackground($oBackground);
//$objPHPPowerPoint->getAllMasterSlides()[0]->setBackground($oBackground);//设置所有背景

画一条线

$currentSlide->createLineShape(30, 50, 900, 50)
    ->getBorder()
    	->setDashStyle(Border::DASH_DASH)//设置折线样式(实线,虚线,...)
    	->setLineStyle(Border::LINE_SINGLE)// 设置线条样式(单条线,双条线,...)
    	->setColor(new Color(Color::COLOR_BLUE))
    	->setLineWidth(0.5);

表格

// 设置表格
$tableShape = $currentSlide->createTableShape($columns);// $columns:列数
$tableShape->setHeight(380); // 高
$tableShape->setWidth(1225); // 宽
$tableShape->setOffsetX(20); // x轴偏移
$tableShape->setOffsetY(60); // y轴偏移
// 新建行
$row = $tableShape->createRow();
// 行填充色
$row->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR)
               ->setRotation(90)
               ->setStartColor(new Color('FFD9D9D9'))
               ->setEndColor(new Color('FFD9D9D9'));
// 设置行高
$row->setHeight(20);
// 访问单元格1:
$cell = $row->nextCell(); 
// 访问单元格2:
$cellA1 = $row->getCell(1);
// 设置colspan
$cell->setColSpan(1);
// 设置文字及样式
$cell->createTextRun('填值')
    ->getFont()
    ->setBold(false) //加粗
    ->setSize(10) // 文字大小
    ->setColor(new Color('FF000000'));// 设置颜色
// 设置底边框
$cell->getBorders()->getBottom()
    ->setLineWidth(4)
    ->setLineStyle(Border::LINE_SINGLE) // 线条样式
    ->setDashStyle(Border::DASH_DASH);	// 破折号样式
// 设置margin
$cell->getActiveParagraph()->getAlignment()
    ->setMarginTop(10) // 设置margintop
    ->setMarginLeft(10); // 设置marginleft
// 设置文字对齐方式
$cell->getParagraph()->getAlignment()
        ->setHorizontal(Alignment::HORIZONTAL_CENTER)
        ->setVertical(Alignment::VERTICAL_CENTER)
        ->setTextDirection(Alignment::TEXT_DIRECTION_HORIZONTAL);
// 设置单元格宽度
$cell->setWidth(100);
// 单元格内换行
$textRunC2 = $cell->createTextRun('换行前');
$cell->createBreak();
$textRunC2 = $cell->createTextRun('换行后');
$textRunC2->getFont()->setBold(false)->setSize(10);

段落

// 设置位置
$shape = $currentSlide->createRichTextShape()
      ->setHeight(426)
      ->setWidth(426)
      ->setOffsetX(25)
      ->setOffsetY(132);

$shape->getActiveParagraph()->getAlignment()
    		->setHorizontal(Alignment::HORIZONTAL_LEFT)// 对齐方式
            ->setMarginLeft(0)// 距左边缘
            ->setIndent(-10);// 列标位置
$shape->getActiveParagraph()->getFont()->setSize(12)
                       ->setColor(new Color( 'FF000000' ));// 设置内容字体
$shape->getActiveParagraph()->getBulletStyle()
    	->setBulletType(Bullet::TYPE_BULLET)// 显示类型 字符,空,数字
    	->setBulletChar('>')         // 项目符号
    	->setBulletColor(new Color(Color::COLOR_BLACK)); // 设置项目符号颜色
$shape->createTextRun('A class library');// 设置内容
$shape->createParagraph()->createTextRun('Written in PHP');//加一行
$shape->createBreak();//换行

添加图片

To create a drawing, you have four sources : File, GD, Base64 and ZipFile.

Base64方式添加图片 [此方式貌似适合png格式图片]

$base64 = imgToBase64($url);
$shape = new Drawing\Base64();
$shape->setName('PHPPresentation logo')
      ->setDescription('PHPPresentation logo')
      ->setData($base64, false)
      ->setResizeProportional(false)
      ->setHeight($hight)
      ->setWidth($width)
      ->setOffsetX($offx)
      ->setOffsetY($offy);
$currentSlide->addShape($shape);
/**
 * 获取图片的Base64编码(不支持url) *
 * @param $img_file 传入本地图片地址 *
 * @return string
 * @ https://www.jb51.net/article/173512.htm
 */
function imgToBase64($img_file) {
  $img_base64 = '';
  if (file_exists($img_file)) {
    $app_img_file = $img_file; // 图片路径
    $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
    $fp = fopen($app_img_file, "r"); // 图片是否可读权限
    if ($fp) {
      $filesize = filesize($app_img_file);
      $content = fread($fp, $filesize);
      $file_content = chunk_split(base64_encode($content)); // base64编码
      switch ($img_info[2]) {      //判读图片类型
        case 1: $img_type = "gif";
          break;
        case 2: $img_type = "jpg";
          break;
        case 3: $img_type = "png";
          break;
      }
      $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
    }
    fclose($fp);
  }
  return $img_base64; //返回图片的base64
}

直接添加[这种貌似设置不了宽]

$shape->setName('PHPPresentation logo')
    ->setDescription('PHPPresentation logo')
    ->setPath('./resources/phppowerpoint_logo.gif')
    ->setHeight(36)
    ->setOffsetX(10)
    ->setOffsetY(10);

一些装饰线

// Some decorative lines   getAllMasterSlides给所有幻灯片加上装饰线
$oMasterSlide = $objPHPPowerPoint->getAllMasterSlides()[0];
$shape = $oMasterSlide->createLineShape(0, 670, 160, 670)->getBorder()->setColor(new Color(Color::COLOR_RED))->setLineWidth(2);
$shape = $oMasterSlide->createLineShape(0, 672, 160, 672)->getBorder()->setColor(new Color(Color::COLOR_WHITE))->setLineWidth(2);
$shape = $oMasterSlide->createLineShape(0, 674, 160, 674)->getBorder()->setColor(new Color(Color::COLOR_DARKBLUE))->setLineWidth(2);

新建幻灯片

$currentSlide = $objPHPPowerPoint -> createSlide();

保存下载

//告诉浏览器这是一个文件流格式的文件
Header ( "Content-type: application/octet-stream" ); 
//请求范围的度量单位
Header ( "Accept-Ranges: bytes" );
//Content-Length是指定包含于请求或响应中数据的字节长度
//Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );
//用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
Header ( "Content-Disposition: attachment; filename=samp.pptx" );
$oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$oWriterPPTX ->save('php://output');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值