PHP讀取PPT文件的方法
本文實(shí)例講述了PHP讀取PPT文件的方法。分享給大家供大家參考,具體如下:
最近做一個(gè)和FLASH有關(guān)的東西,其中就要用到在網(wǎng)站上看PPT就像百度,豆丁網(wǎng)那樣可以直接在網(wǎng)站上讀,在網(wǎng)上搜了半天沒搜到,都是些什么安裝個(gè)軟件什么的,PHP網(wǎng)站放到空間上,誰能讓你在哪裝軟件呢?不是在瞎扯么?不過還好,最后在國(guó)外一個(gè)網(wǎng)站上搜到了一個(gè)解決思路,就是一個(gè)PHP操作PPT的類,當(dāng)然這個(gè)網(wǎng)站還提供了操作OFFICES軟件的其他類,不過是2007版的OFFICES,現(xiàn)把網(wǎng)址貼出來奉獻(xiàn)給大家:http://phppowerpoint.codeplex.com/。也可點(diǎn)擊此處本站下載。
這個(gè)類可以實(shí)現(xiàn) PPT2007格式的讀寫/生成
下面也貼出當(dāng)時(shí)看到的一個(gè)例子:
本例子使用phppowerpoint類,使用該類注意兩點(diǎn),一個(gè)是了解參數(shù)含義,二注意字符集
<?php header("content-type:text/html;charset=utf-8");//字體設(shè)置防止亂碼 error_reporting(E_ALL); /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/'); /** PHPPowerPoint */ include 'PHPPowerPoint.php'; /** PHPPowerPoint_IOFactory */ include 'PHPPowerPoint/IOFactory.php'; // Create new PHPPowerPoint object //echo date('H:i:s') . " Create new PHPPowerPoint object\n"; $objPHPPowerPoint = new PHPPowerPoint(); $objPHPPowerPoint->getProperties()->setCreator("Maarten Balliauw"); $objPHPPowerPoint->getProperties()->setLastModifiedBy("Maarten Balliauw"); $objPHPPowerPoint->getProperties()->setTitle("Office 2007 PPTX Test Document"); $objPHPPowerPoint->getProperties()->setSubject("Office 2007 PPTX Test Document"); $objPHPPowerPoint->getProperties()->setDescription("Test document for Office 2007 PPTX, generated using PHP classes."); $objPHPPowerPoint->getProperties()->setKeywords("office 2007 openxml php"); $objPHPPowerPoint->getProperties()->setCategory("Test result file"); // Remove first slide //echo date('H:i:s') . " Remove first slide\n"; $objPHPPowerPoint->removeSlideByIndex(0); // Create templated slide //echo date('H:i:s') . " Create templated slide\n"; /*$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(200); $shape->setWidth(600); $shape->setOffsetX(10); $shape->setOffsetY(400); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('Introduction to'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun('PHPPowerPoint'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(60); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create templated slide echo date('H:i:s') . " Create templated slide\n"; $currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(10); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('What is PHPPowerPoint?'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(100); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('- A class library'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun('- Written in PHP'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun('- Representing a presentation'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun('- Supports writing to different file formats'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create templated slide echo date('H:i:s') . " Create templated slide\n"; $currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(10); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('What\'s the point?'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(100); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('- Generate slide decks'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' - Represent business data'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' - Show a family slide show'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' - ...'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun('- Export these to different formats'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' - PowerPoint 2007'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' - Serialized'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' - ... (more to come) ...'); $textRun->getFont()->setSize(28); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create templated slide echo date('H:i:s') . " Create templated slide\n"; $currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(10); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('Need more info?'); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create a shape (text) echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(10); $shape->setOffsetY(100); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $textRun = $shape->createTextRun('Check the project site on CodePlex:'); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); $shape->createBreak(); $textRun = $shape->createTextRun(' ); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) ); // Create templated slide echo date('H:i:s') . " Create templated slide\n";*/ //test //從數(shù)據(jù)庫調(diào)取數(shù)據(jù)進(jìn)行for循環(huán) $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]); // local function // Create a shape (text) //echo date('H:i:s') . " Create a shape (rich text)\n"; $shape = $currentSlide->createRichTextShape(); $shape->setHeight(100); $shape->setWidth(930); //調(diào)整字體的高度寬度 $shape->setOffsetX(20); $shape->setOffsetY(400); //$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); $row["price"]=iconv("utf-8","gb2312",$row["price"][$i]); $textRun = $shape->createTextRun('禮品網(wǎng)價(jià)格:'.$row["xsprice"][$i]); $textRun->getFont()->setBold(true); $textRun->getFont()->setSize(48); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) ); $shape = $currentSlide->createRichTextShape(); $shape->setHeight(600); $shape->setWidth(930); $shape->setOffsetX(20); $shape->setOffsetY(500); $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT ); //添加多行內(nèi)容從這開始 $textRun = $shape->createTextRun('公司編號(hào): '.$row["cjid"][$i]); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) ); $shape->createBreak(); $textRun = $shape->createTextRun('禮品網(wǎng)編號(hào): '.$row["lpid"][$i]); $textRun->getFont()->setSize(36); $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) ); //test // Save PowerPoint 2007 file } //echo date('H:i:s') . " Write to PowerPoint2007 format\n"; $objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007'); $objWriter->save(str_replace('.php', '.pptx',__FILE__)); header("Content-type:application/vnd.ms-powerpoint;"); header("location:02presentation.pptx"); // Echo memory peak usage //echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n"; // Echo done //echo date('H:i:s') . " Done writing file.\r\n"; /** * Creates a templated slide * * @param PHPPowerPoint $objPHPPowerPoint * @return PHPPowerPoint_Slide */ function createTemplatedSlide1(PHPPowerPoint $objPHPPowerPoint,$cs1) { // Create slide $slide = $objPHPPowerPoint->createSlide(); // Add background image $shape = $slide->createDrawingShape(); $shape->setName('Background'); $shape->setDescription('Background'); $shape->setPath('./images/realdolmen_bg.jpg'); $shape->setWidth(950); $shape->setHeight(720); $shape->setOffsetX(0); $shape->setOffsetY(0); // Add logo $shape = $slide->createDrawingShape(); $shape->setName('PHPPowerPoint logo'); $shape->setDescription('PHPPowerPoint logo'); $shape->setPath($cs1); $shape->setHeight(120); $shape->setOffsetX(10); $shape->setOffsetY(10); // Return slide return $slide; }
我的問題仍然還在研究中,如果大家有更好的解決方法,請(qǐng)繼續(xù)貼,期待更多好的分享
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP讀取大文件的多種方法介紹
- PHP使用fopen與file_get_contents讀取文件實(shí)例分享
- php讀取txt文件并將數(shù)據(jù)插入到數(shù)據(jù)庫
- PHP讀取文件內(nèi)容的五種方式
- php實(shí)現(xiàn)讀取和寫入tab分割的文件
- php讀取csc文件并輸出
- php讀取文件內(nèi)容到數(shù)組的方法
- php從csv文件讀取數(shù)據(jù)并輸出到網(wǎng)頁的方法
- php讀取csv文件并輸出的方法
- PHP讀取txt文本文件并分頁顯示的方法
- php遞歸法讀取目錄及文件的方法
- php讀取torrent種子文件內(nèi)容的方法(測(cè)試可用)
相關(guān)文章
PHP獲取日期對(duì)應(yīng)星期、一周日期、星期開始與結(jié)束日期的方法
這篇文章主要介紹了PHP獲取日期對(duì)應(yīng)星期、一周日期、星期開始與結(jié)束日期的方法,涉及php日期與時(shí)間相關(guān)計(jì)算操作技巧,需要的朋友可以參考下2018-06-06php創(chuàng)建桌面快捷方式實(shí)現(xiàn)方法
這篇文章主要介紹了php創(chuàng)建桌面快捷方式實(shí)現(xiàn)方法,需要的朋友可以參考下2015-12-12php中strstr、strrchr、substr、stristr四個(gè)函數(shù)的區(qū)別總結(jié)
這篇文章主要介紹了php中strstr、strrchr、substr、stristr四個(gè)函數(shù)的區(qū)別總結(jié),這4個(gè)函數(shù)是PHP中常用的字符串相關(guān)函數(shù),需要的朋友可以參考下2014-09-09php 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳的方法
php 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳的方法,主要使用到了 php 的時(shí)間函數(shù) mktime,下面首先還是直奔主題以示例說明如何使用 mktime 獲取今日、昨日、上周、本月的起始時(shí)間戳和結(jié)束時(shí)間戳,然后在介紹一下 mktime 函數(shù)作用和用法2013-09-09php.ini中date.timezone設(shè)置詳解
本文給大家分享的是php.ini中date.timezone設(shè)置的方法,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-11-11jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼
這篇文章主要介紹了jquery+php+ajax顯示上傳進(jìn)度的多圖片上傳并生成縮略圖代碼,可實(shí)現(xiàn)帶有進(jìn)度條效果的多圖上傳功能與生成縮略圖功能,是web開發(fā)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10PHP中防止SQL注入攻擊和XSS攻擊的兩個(gè)簡(jiǎn)單方法
所有有打印的語句如echo,print等 在打印前都要使用htmlentities() 進(jìn)行過濾,這樣可以防止Xss,注意中文要寫出htmlentities2010-04-04