本文实例讲述了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程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!