欢迎来到福编程网,本站提供各种互联网专业知识!

PHP读取PPT文件的方法

发布时间:2015-12-10 作者:疯狂的流浪 来源:转载
这篇文章主要介绍了PHP读取PPT文件的方法,通过php第三方类库PHPPowerPoint实现对ppt文件的读取操作,需要的朋友可以参考下

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

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

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

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

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

代码
  1. <?php
  2. header("content-type:text/html;charset=utf-8");//字体设置防止乱码
  3. error_reporting(E_ALL);
  4. /** Include path **/
  5. set_include_path(get_include_path() . PATH_SEPARATOR . '../Classes/');
  6. /** PHPPowerPoint */
  7. include 'PHPPowerPoint.php';
  8. /** PHPPowerPoint_IOFactory */
  9. include 'PHPPowerPoint/IOFactory.php';
  10. // Create new PHPPowerPoint object
  11. //echo date('H:i:s') . " Create new PHPPowerPoint objectn";
  12. $objPHPPowerPoint = new PHPPowerPoint();
  13. $objPHPPowerPoint->getProperties()->setCreator("Maarten Balliauw");
  14. $objPHPPowerPoint->getProperties()->setLastModifiedBy("Maarten Balliauw");
  15. $objPHPPowerPoint->getProperties()->setTitle("Office 2007 PPTX Test Document");
  16. $objPHPPowerPoint->getProperties()->setSubject("Office 2007 PPTX Test Document");
  17. $objPHPPowerPoint->getProperties()->setDescription("Test document for Office 2007 PPTX, generated using PHP classes.");
  18. $objPHPPowerPoint->getProperties()->setKeywords("office 2007 openxml php");
  19. $objPHPPowerPoint->getProperties()->setCategory("Test result file");
  20. // Remove first slide
  21. //echo date('H:i:s') . " Remove first sliden";
  22. $objPHPPowerPoint->removeSlideByIndex(0);
  23. // Create templated slide
  24. //echo date('H:i:s') . " Create templated sliden";
  25. /*$currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
  26. // Create a shape (text)
  27. echo date('H:i:s') . " Create a shape (rich text)n";
  28. $shape = $currentSlide->createRichTextShape();
  29. $shape->setHeight(200);
  30. $shape->setWidth(600);
  31. $shape->setOffsetX(10);
  32. $shape->setOffsetY(400);
  33. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  34. $textRun = $shape->createTextRun('Introduction to');
  35. $textRun->getFont()->setBold(true);
  36. $textRun->getFont()->setSize(28);
  37. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  38. $shape->createBreak();
  39. $textRun = $shape->createTextRun('PHPPowerPoint');
  40. $textRun->getFont()->setBold(true);
  41. $textRun->getFont()->setSize(60);
  42. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  43. // Create templated slide
  44. echo date('H:i:s') . " Create templated sliden";
  45. $currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
  46. // Create a shape (text)
  47. echo date('H:i:s') . " Create a shape (rich text)n";
  48. $shape = $currentSlide->createRichTextShape();
  49. $shape->setHeight(100);
  50. $shape->setWidth(930);
  51. $shape->setOffsetX(10);
  52. $shape->setOffsetY(10);
  53. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  54. $textRun = $shape->createTextRun('What is PHPPowerPoint?');
  55. $textRun->getFont()->setBold(true);
  56. $textRun->getFont()->setSize(48);
  57. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  58. // Create a shape (text)
  59. echo date('H:i:s') . " Create a shape (rich text)n";
  60. $shape = $currentSlide->createRichTextShape();
  61. $shape->setHeight(600);
  62. $shape->setWidth(930);
  63. $shape->setOffsetX(10);
  64. $shape->setOffsetY(100);
  65. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  66. $textRun = $shape->createTextRun('- A class library');
  67. $textRun->getFont()->setSize(36);
  68. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  69. $shape->createBreak();
  70. $textRun = $shape->createTextRun('- Written in PHP');
  71. $textRun->getFont()->setSize(36);
  72. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  73. $shape->createBreak();
  74. $textRun = $shape->createTextRun('- Representing a presentation');
  75. $textRun->getFont()->setSize(36);
  76. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  77. $shape->createBreak();
  78. $textRun = $shape->createTextRun('- Supports writing to different file formats');
  79. $textRun->getFont()->setSize(36);
  80. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  81. // Create templated slide
  82. echo date('H:i:s') . " Create templated sliden";
  83. $currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
  84. // Create a shape (text)
  85. echo date('H:i:s') . " Create a shape (rich text)n";
  86. $shape = $currentSlide->createRichTextShape();
  87. $shape->setHeight(100);
  88. $shape->setWidth(930);
  89. $shape->setOffsetX(10);
  90. $shape->setOffsetY(10);
  91. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  92. $textRun = $shape->createTextRun('What's the point?');
  93. $textRun->getFont()->setBold(true);
  94. $textRun->getFont()->setSize(48);
  95. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  96. // Create a shape (text)
  97. echo date('H:i:s') . " Create a shape (rich text)n";
  98. $shape = $currentSlide->createRichTextShape();
  99. $shape->setHeight(600);
  100. $shape->setWidth(930);
  101. $shape->setOffsetX(10);
  102. $shape->setOffsetY(100);
  103. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  104. $textRun = $shape->createTextRun('- Generate slide decks');
  105. $textRun->getFont()->setSize(36);
  106. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  107. $shape->createBreak();
  108. $textRun = $shape->createTextRun(' - Represent business data');
  109. $textRun->getFont()->setSize(28);
  110. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  111. $shape->createBreak();
  112. $textRun = $shape->createTextRun(' - Show a family slide show');
  113. $textRun->getFont()->setSize(28);
  114. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  115. $shape->createBreak();
  116. $textRun = $shape->createTextRun(' - ...');
  117. $textRun->getFont()->setSize(28);
  118. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  119. $shape->createBreak();
  120. $textRun = $shape->createTextRun('- Export these to different formats');
  121. $textRun->getFont()->setSize(36);
  122. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  123. $shape->createBreak();
  124. $textRun = $shape->createTextRun(' - PowerPoint 2007');
  125. $textRun->getFont()->setSize(28);
  126. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  127. $shape->createBreak();
  128. $textRun = $shape->createTextRun(' - Serialized');
  129. $textRun->getFont()->setSize(28);
  130. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  131. $shape->createBreak();
  132. $textRun = $shape->createTextRun(' - ... (more to come) ...');
  133. $textRun->getFont()->setSize(28);
  134. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  135. // Create templated slide
  136. echo date('H:i:s') . " Create templated sliden";
  137. $currentSlide = createTemplatedSlide($objPHPPowerPoint); // local function
  138. // Create a shape (text)
  139. echo date('H:i:s') . " Create a shape (rich text)n";
  140. $shape = $currentSlide->createRichTextShape();
  141. $shape->setHeight(100);
  142. $shape->setWidth(930);
  143. $shape->setOffsetX(10);
  144. $shape->setOffsetY(10);
  145. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  146. $textRun = $shape->createTextRun('Need more info?');
  147. $textRun->getFont()->setBold(true);
  148. $textRun->getFont()->setSize(48);
  149. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  150. // Create a shape (text)
  151. echo date('H:i:s') . " Create a shape (rich text)n";
  152. $shape = $currentSlide->createRichTextShape();
  153. $shape->setHeight(600);
  154. $shape->setWidth(930);
  155. $shape->setOffsetX(10);
  156. $shape->setOffsetY(100);
  157. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  158. $textRun = $shape->createTextRun('Check the project site on CodePlex:');
  159. $textRun->getFont()->setSize(36);
  160. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  161. $shape->createBreak();
  162. $textRun = $shape->createTextRun(' );
  163. $textRun->getFont()->setSize(36);
  164. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( 'FFFFFFFF' ) );
  165. // Create templated slide
  166. echo date('H:i:s') . " Create templated sliden";*/
  167. //test
  168. //从数据库调取数据进行for循环
  169. $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'));
  170. for($i=0;$i<4;$i++)
  171. {
  172. $currentSlide = createTemplatedSlide1($objPHPPowerPoint,$row["titlepic"][$i]); // local function
  173. // Create a shape (text)
  174. //echo date('H:i:s') . " Create a shape (rich text)n";
  175. $shape = $currentSlide->createRichTextShape();
  176. $shape->setHeight(100);
  177. $shape->setWidth(930);
  178. //调整字体的高度宽度
  179. $shape->setOffsetX(20);
  180. $shape->setOffsetY(400);
  181. //$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  182. $row["price"]=iconv("utf-8","gb2312",$row["price"][$i]);
  183. $textRun = $shape->createTextRun('礼品网价格:'.$row["xsprice"][$i]);
  184. $textRun->getFont()->setBold(true);
  185. $textRun->getFont()->setSize(48);
  186. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) );
  187. $shape = $currentSlide->createRichTextShape();
  188. $shape->setHeight(600);
  189. $shape->setWidth(930);
  190. $shape->setOffsetX(20);
  191. $shape->setOffsetY(500);
  192. $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT );
  193. //添加多行内容从这开始
  194. $textRun = $shape->createTextRun('公司编号: '.$row["cjid"][$i]);
  195. $textRun->getFont()->setSize(36);
  196. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) );
  197. $shape->createBreak();
  198. $textRun = $shape->createTextRun('礼品网编号: '.$row["lpid"][$i]);
  199. $textRun->getFont()->setSize(36);
  200. $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#000000' ) );
  201. //test
  202. // Save PowerPoint 2007 file
  203. }
  204. //echo date('H:i:s') . " Write to PowerPoint2007 formatn";
  205. $objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
  206. $objWriter->save(str_replace('.php', '.pptx',__FILE__));
  207. header("Content-type:application/vnd.ms-powerpoint;");
  208. header("location:02presentation.pptx");
  209. // Echo memory peak usage
  210. //echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MBrn";
  211. // Echo done
  212. //echo date('H:i:s') . " Done writing file.rn";
  213. /**
  214. * Creates a templated slide
  215. *
  216. * @param PHPPowerPoint $objPHPPowerPoint
  217. * @return PHPPowerPoint_Slide
  218. */
  219. function createTemplatedSlide1(PHPPowerPoint $objPHPPowerPoint,$cs1)
  220. {
  221. // Create slide
  222. $slide = $objPHPPowerPoint->createSlide();
  223. // Add background image
  224. $shape = $slide->createDrawingShape();
  225. $shape->setName('Background');
  226. $shape->setDescription('Background');
  227. $shape->setPath('./images/realdolmen_bg.jpg');
  228. $shape->setWidth(950);
  229. $shape->setHeight(720);
  230. $shape->setOffsetX(0);
  231. $shape->setOffsetY(0);
  232. // Add logo
  233. $shape = $slide->createDrawingShape();
  234. $shape->setName('PHPPowerPoint logo');
  235. $shape->setDescription('PHPPowerPoint logo');
  236. $shape->setPath($cs1);
  237. $shape->setHeight(120);
  238. $shape->setOffsetX(10);
  239. $shape->setOffsetY(10);
  240. // Return slide
  241. return $slide;
  242. }

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

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

相关推荐

返回顶部