在 Web 應用中實現類似百度文庫、豆丁網的線上 PPT 預覽功能,伺服器端無法安裝 Office 軟體。解決方案是使用 PHPPowerPoint 庫,純 PHP 實現 PPTX(Office 2007+ 格式)的讀寫與生成。
專案地址:https://phppowerpoint.codeplex.com/
<?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");
?>
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;
}
createRichTextShape():創建富文本形狀createTextRun():添加文字段落,可單獨設置字體屬性createBreak():換行getFont()->setBold()/setSize()/setColor():字體樣式鏈式調用getAlignment()->setHorizontal():水平對齊方式使用 createDrawingShape() 插入圖片,支持設定位置、尺寸、名稱與描述。
header("content-type:text/html;charset=utf-8") 並注意 iconv 轉換memory_limit結合前端 JavaScript 庫(如 pdf.js 或專用 PPTX Viewer),可實現瀏覽器端直接預覽生成的 PPTX 文件,構建完整的線上文檔展示平台。