PHP建立PowerPoint2007文檔的方法_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了PHP建立PowerPoint2007文檔的方法。分享給大家供大家參考,具體如下:

今天早上從訂閱的 Zend DevZone 看到篇很有意思的文章. 試了一下. 果然很又意思, 分享給大家吧.

程式需要 PHP 5.2 以上環境, 另外需要 php_zip 和 php_xml 擴充模組支援. 另外需要下載 PHPPowerPoint 類庫. 官方網站地址: http://phppowerpoint.codeplex.com/ 目前穩定版本是 0.1.0。也可點擊此處本站下載。

說一下感覺吧. 這個類庫還可以. 編碼很規範. 完全PHP5的風格. 我喜歡的類型. 和 Zend Framework 一樣. 處理速度也非常快. 本次只做了簡單才測試. 更多進階功能未花時間去玩. 帖一下測試代碼吧.

<?php /**  * PHP 產生 PowerPoint 2007 樣本指令碼.  *  * 本程式需要 PHP 5.2 以上版本, 需要 php_zip 和 php_xml 擴充支援.  * 通常WIN下程式只要開啟 php_zip 擴充即可, php_xml 擴充內建支援.  * Linux 下需要根據編譯條件具體調整.  *  * @author: Guya  * @since: 2009-4-30  */ //目錄分割符號 define('DS', DIRECTORY_SEPARATOR); //定義根目錄 define('ROOT', dirname(__FILE__) . DS); //修改include路徑, PHPPowerPoint 包放在目前的目錄的 libs 目錄下. set_include_path(get_include_path() . PATH_SEPARATOR . ROOT . 'libs'); //不限制指令碼已耗用時間限制. set_time_limit(0); //簡單設定自動載入函數. function __autoload($className) {  include_once(str_replace("_", DS, $className) . ".php"); } //建立立一個 PHPPowerPoint 對象. $ppp = new PHPPowerPoint(); //擷取當前使用的一頁投影片 $activeSlide = $ppp->getActiveSlide(); //添加一個圖片到投影片. $shape = $activeSlide->createDrawingShape(); //設定圖片名稱. $shape->setName('MmClub.net Logo'); //設定圖片的描述資訊. $shape->setDescription('MmClub.net Logo'); //圖片實際路徑 $shape->setPath(ROOT . 'mmclub.net.jpg'); //圖片高度 $shape->setHeight(103); //設定圖片寬度 $shape->setWidth(339); //設定圖片相對於左上方X位置, 單位像素 $shape->setOffsetX(10); //設定圖片相對於左上方Y位置, 單位像素 $shape->setOffsetY(10); //設定圖顯示狀態 $shape->getShadow()->setVisible(true); $shape->getShadow()->setDirection(45); $shape->getShadow()->setDistance(10); //設定一個文字框 $shape = $activeSlide->createRichTextShape(); //設定文字框高度, 單位像素 $shape->setHeight(150); //設定文字框寬度, 單位像素 $shape->setWidth(600); //設定文字框相對於左上方X位置, 單位像素 $shape->setOffsetX(150); //設定文字框相對於左上方Y位置, 單位像素 $shape->setOffsetY(200); //設定文本布局位置為水平置中, 垂直置中. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER ); $shape->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); //設定文字框常值內容. 在中文環境下測試沒中文問題. 如果在 e 文環境. 注意要指定支援中文的字型. 否則可能出亂碼了. $textRun = $shape->createTextRun('歡迎使用 PHPPowerPoint2007'); //使用字型加粗 $textRun->getFont()->setBold(true); //設定字型尺寸為 38, 這裡注意一下文字的大小設定. 前面的文字框的大小是固定的. 如果文字超出的容器會被出容器被排到下面 $textRun->getFont()->setSize(38); //設定文字顏色, 這裡是ARGB模式 , 16進位模式, 前面2位為透明度, 後面為RGB值. 這裡設定為 blue藍色 $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFF0000' ) ); //下面再設定幾個文字框 $shape0 = $activeSlide->createRichTextShape(); $shape0->setHeight(50); $shape0->setWidth(400); $shape0->setOffsetX(250); $shape0->setOffsetY(400); $shape0->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER ); $shape0->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); $textRun0 = $shape0->createTextRun('http://www.jb51.net'); $textRun0->getFont()->setSize(26); $textRun0->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FF0000FF' ) ); $shape1 = $activeSlide->createRichTextShape(); $shape1->setHeight(30); $shape1->setWidth(200); $shape1->setOffsetX(700); $shape1->setOffsetY(500); $shape1->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $shape1->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); $textRun1 = $shape1->createTextRun('Author: Guya'); $textRun1->getFont()->setSize(14); $textRun1->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FF000000' ) ); $shape2 = $activeSlide->createRichTextShape(); $shape2->setHeight(30); $shape2->setWidth(200); $shape2->setOffsetX(700); $shape2->setOffsetY(540); $shape2->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $shape2->getAlignment()->setVertical( PHPPowerPoint_Style_Alignment::VERTICAL_CENTER ); $textRun2 = $shape2->createTextRun('Date: 2009-4-30'); $textRun2->getFont()->setSize(14); $textRun2->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FF000000' ) ); //儲存PPTX 檔案, 使用 2007 格式 $objWriter = PHPPowerPoint_IOFactory::createWriter($ppp, 'PowerPoint2007'); //儲存檔案 $objWriter->save(ROOT . 'myPhpPpt.pptx'); echo 'ppt create success!'; ?>

這個東西的應用前景的話. 在WEB的某些場合還是很有用的. 需要的朋友可以多花點時間去研究了

希望本文所述對大家php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.