欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP读取PPT文件的方法

程序员文章站 2022-06-02 23:24:51
本文实例讲述了php读取ppt文件的方法。分享给大家供大家参考,具体如下: 最近做一个和flash有关的东西,其中就要用到在网站上看ppt就像百度,豆丁网那样可以直接在网...

本文实例讲述了php读取ppt文件的方法。分享给大家供大家参考,具体如下:

最近做一个和flash有关的东西,其中就要用到在网站上看ppt就像百度,豆丁网那样可以直接在网站上读,在网上搜了半天没搜到,都是些什么安装个软件什么的,php网站放到空间上,谁能让你在哪装软件呢?不是在瞎扯么?不过还好,最后在国外一个网站上搜到了一个解决思路,就是一个php操作ppt的类,当然这个网站还提供了操作offices软件的其他类,不过是2007版的offices,现把网址贴出来奉献给大家:。也可点击此处本站下载

这个类可以实现 ppt2007格式的读写/生成

下面也贴出当时看到的一个例子:

本例子使用phppowerpoint类,使用该类注意两点,一个是了解参数含义,二注意字符集

<?php
header("content-type:text/html;charset=utf-8");//字体设置防止乱码
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
//从数据库调取数据进行for循环
$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);
//调整字体的高度宽度
$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('礼品网价格:'.$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 );
//添加多行内容从这开始
$textrun = $shape->createtextrun('公司编号:  '.$row["cjid"][$i]);
$textrun->getfont()->setsize(36);
$textrun->getfont()->setcolor( new phppowerpoint_style_color( '#000000' ) );
$shape->createbreak();
$textrun = $shape->createtextrun('礼品网编号:  '.$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;
}

我的问题仍然还在研究中,如果大家有更好的解决方法,请继续贴,期待更多好的分享

希望本文所述对大家php程序设计有所帮助。