首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 讀取與生成 PPTX 文件:PHPPowerPoint 庫實戰

     編輯:PHP 綜合知識

前言

在 Web 應用中實現類似百度文庫、豆丁網的線上 PPT 預覽功能,伺服器端無法安裝 Office 軟體。解決方案是使用 PHPPowerPoint 庫,純 PHP 實現 PPTX(Office 2007+ 格式)的讀寫與生成。

專案地址:https://phppowerpoint.codeplex.com/

核心功能

  • 創建 PPTX 簡報文件
  • 新增投影片、形狀、文字、圖片
  • 設置字體、顏色、大小、對齊等樣式
  • 支持背景圖片與 Logo 水印
  • 從資料庫讀取資料動態生成投影片

完整實作範例

<?php
header("content-type:text/html;charset=utf-8");
error_reporting(E_ALL);

set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
include 'PHPPowerPoint.php';
include 'PHPPowerPoint/IOFactory.php';

// 創建 PHPPowerPoint 物件
$objPHPPowerPoint = new PHPPowerPoint();
$objPHPPowerPoint->getProperties()
    ->setCreator("Maarten Balliauw")
    ->setLastModifiedBy("Maarten Balliauw")
    ->setTitle("Office 2007 PPTX Test Document")
    ->setSubject("Office 2007 PPTX Test Document")
    ->setDescription("Test document for Office 2007 PPTX, generated using PHP classes.")
    ->setKeywords("office 2007 openxml php")
    ->setCategory("Test result file");

// 移除預設第一頁
$objPHPPowerPoint->removeSlideByIndex(0);

// 模擬資料庫資料
$row = array(
    'titlepic' => array('./images/love.gif','./images/love1.gif','./images/love2.gif','./images/love3.gif'),
    'xsprice' => array("55","33","22","333"),
    'cjid' => array('100','222','333','3333'),
    'lpid' => array('111','222','333','444'),
    'price' => array('111','433','243','3245')
);

for ($i = 0; $i < 4; $i++) {
    $currentSlide = createTemplatedSlide1($objPHPPowerPoint, $row["titlepic"][$i]);
    
    // 標題區塊
    $shape = $currentSlide->createRichTextShape();
    $shape->setHeight(100)->setWidth(930)->setOffsetX(20)->setOffsetY(400);
    $textRun = $shape->createTextRun('禮品網價格:'.$row["xsprice"][$i]);
    $textRun->getFont()->setBold(true)->setSize(48)->setColor(new PHPPowerPoint_Style_Color('#000000'));
    
    // 內容區塊
    $shape = $currentSlide->createRichTextShape();
    $shape->setHeight(600)->setWidth(930)->setOffsetX(20)->setOffsetY(500);
    $shape->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT);
    
    $textRun = $shape->createTextRun('公司編號: '.$row["cjid"][$i]);
    $textRun->getFont()->setSize(36)->setColor(new PHPPowerPoint_Style_Color('#000000'));
    $shape->createBreak();
    
    $textRun = $shape->createTextRun('禮品網編號: '.$row["lpid"][$i]);
    $textRun->getFont()->setSize(36)->setColor(new PHPPowerPoint_Style_Color('#000000'));
}

// 輸出 PPTX 文件
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx', __FILE__));
header("Content-type:application/vnd.ms-powerpoint");
header("location:02presentation.pptx");
?>

關鍵技術點解析

1. 投影片模板函數

function createTemplatedSlide1(PHPPowerPoint $objPHPPowerPoint, $cs1) {
    $slide = $objPHPPowerPoint->createSlide();
    
    // 背景圖片
    $shape = $slide->createDrawingShape();
    $shape->setName('Background')->setDescription('Background');
    $shape->setPath('./images/realdolmen_bg.jpg');
    $shape->setWidth(950)->setHeight(720)->setOffsetX(0)->setOffsetY(0);
    
    // Logo
    $shape = $slide->createDrawingShape();
    $shape->setName('PHPPowerPoint logo')->setDescription('PHPPowerPoint logo');
    $shape->setPath($cs1)->setHeight(120)->setOffsetX(10)->setOffsetY(10);
    
    return $slide;
}

2. 文字樣式控制

  • createRichTextShape():創建富文本形狀
  • createTextRun():添加文字段落,可單獨設置字體屬性
  • createBreak():換行
  • getFont()->setBold()/setSize()/setColor():字體樣式鏈式調用
  • getAlignment()->setHorizontal():水平對齊方式

3. 圖片處理

使用 createDrawingShape() 插入圖片,支持設定位置、尺寸、名稱與描述。

注意事項

  • 字符編碼:務必設置 header("content-type:text/html;charset=utf-8") 並注意 iconv 轉換
  • 路徑問題:圖片路徑需為伺服器絕對路徑或相對執行腳本的路徑
  • 記憶體限制:大量投影片生成時可能需調整 memory_limit
  • 版本限制:僅支持 PPTX(OOXML 格式),不支持舊版 PPT

擴展應用

結合前端 JavaScript 庫(如 pdf.js 或專用 PPTX Viewer),可實現瀏覽器端直接預覽生成的 PPTX 文件,構建完整的線上文檔展示平台。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved