Document.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base {
  28. public function writeDocument(PHPWord $pPHPWord = null) {
  29. // Create XML writer
  30. if($this->getParentWriter()->getUseDiskCaching()) {
  31. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  32. } else {
  33. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
  34. }
  35. // XML header
  36. $objWriter->startDocument('1.0','UTF-8','yes');
  37. // w:document
  38. $objWriter->startElement('w:document');
  39. $objWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
  40. $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  41. $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  42. $objWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
  43. $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  44. $objWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
  45. $objWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
  46. $objWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
  47. $objWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
  48. $objWriter->startElement('w:body');
  49. $_sections = $pPHPWord->getSections();
  50. $countSections = count($_sections);
  51. $pSection = 0;
  52. if($countSections > 0) {
  53. foreach($_sections as $section) {
  54. $pSection++;
  55. $_elements = $section->getElements();
  56. foreach($_elements as $element) {
  57. if($element instanceof PHPWord_Section_Text) {
  58. $this->_writeText($objWriter, $element);
  59. } elseif($element instanceof PHPWord_Section_TextRun) {
  60. $this->_writeTextRun($objWriter, $element);
  61. } elseif($element instanceof PHPWord_Section_Link) {
  62. $this->_writeLink($objWriter, $element);
  63. } elseif($element instanceof PHPWord_Section_Title) {
  64. $this->_writeTitle($objWriter, $element);
  65. } elseif($element instanceof PHPWord_Section_TextBreak) {
  66. $this->_writeTextBreak($objWriter);
  67. } elseif($element instanceof PHPWord_Section_PageBreak) {
  68. $this->_writePageBreak($objWriter);
  69. } elseif($element instanceof PHPWord_Section_Table) {
  70. $this->_writeTable($objWriter, $element);
  71. } elseif($element instanceof PHPWord_Section_ListItem) {
  72. $this->_writeListItem($objWriter, $element);
  73. } elseif($element instanceof PHPWord_Section_Image ||
  74. $element instanceof PHPWord_Section_MemoryImage) {
  75. $this->_writeImage($objWriter, $element);
  76. } elseif($element instanceof PHPWord_Section_Object) {
  77. $this->_writeObject($objWriter, $element);
  78. } elseif($element instanceof PHPWord_TOC) {
  79. $this->_writeTOC($objWriter);
  80. }
  81. }
  82. if($pSection == $countSections) {
  83. $this->_writeEndSection($objWriter, $section);
  84. } else {
  85. $this->_writeSection($objWriter, $section);
  86. }
  87. }
  88. }
  89. $objWriter->endElement(); // End w:body
  90. $objWriter->endElement(); // End w:document
  91. // Return
  92. return $objWriter->getData();
  93. }
  94. private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) {
  95. $objWriter->startElement('w:p');
  96. $objWriter->startElement('w:pPr');
  97. $this->_writeEndSection($objWriter, $section, 3);
  98. $objWriter->endElement();
  99. $objWriter->endElement();
  100. }
  101. private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) {
  102. $_settings = $section->getSettings();
  103. $_header = $section->getHeader();
  104. $_footer = $section->getFooter();
  105. $pgSzW = $_settings->getPageSizeW();
  106. $pgSzH = $_settings->getPageSizeH();
  107. $orientation = $_settings->getOrientation();
  108. $marginTop = $_settings->getMarginTop();
  109. $marginLeft = $_settings->getMarginLeft();
  110. $marginRight = $_settings->getMarginRight();
  111. $marginBottom = $_settings->getMarginBottom();
  112. $borders = $_settings->getBorderSize();
  113. $objWriter->startElement('w:sectPr');
  114. if(!is_null($_header)) {
  115. $rId = $_header->getRelationId();
  116. $objWriter->startElement('w:headerReference');
  117. $objWriter->writeAttribute('w:type', 'default');
  118. $objWriter->writeAttribute('r:id', 'rId'.$rId);
  119. $objWriter->endElement();
  120. }
  121. if(!is_null($_footer)) {
  122. $rId = $_footer->getRelationId();
  123. $objWriter->startElement('w:footerReference');
  124. $objWriter->writeAttribute('w:type', 'default');
  125. $objWriter->writeAttribute('r:id', 'rId'.$rId);
  126. $objWriter->endElement();
  127. }
  128. $objWriter->startElement('w:pgSz');
  129. $objWriter->writeAttribute('w:w', $pgSzW);
  130. $objWriter->writeAttribute('w:h', $pgSzH);
  131. if(!is_null($orientation) && strtolower($orientation) != 'portrait') {
  132. $objWriter->writeAttribute('w:orient', $orientation);
  133. }
  134. $objWriter->endElement();
  135. $objWriter->startElement('w:pgMar');
  136. $objWriter->writeAttribute('w:top', $marginTop);
  137. $objWriter->writeAttribute('w:right', $marginRight);
  138. $objWriter->writeAttribute('w:bottom', $marginBottom);
  139. $objWriter->writeAttribute('w:left', $marginLeft);
  140. $objWriter->writeAttribute('w:header', '720');
  141. $objWriter->writeAttribute('w:footer', '720');
  142. $objWriter->writeAttribute('w:gutter', '0');
  143. $objWriter->endElement();
  144. if(!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) {
  145. $borderColor = $_settings->getBorderColor();
  146. $objWriter->startElement('w:pgBorders');
  147. $objWriter->writeAttribute('w:offsetFrom', 'page');
  148. if(!is_null($borders[0])) {
  149. $objWriter->startElement('w:top');
  150. $objWriter->writeAttribute('w:val', 'single');
  151. $objWriter->writeAttribute('w:sz', $borders[0]);
  152. $objWriter->writeAttribute('w:space', '24');
  153. $objWriter->writeAttribute('w:color', $borderColor[0]);
  154. $objWriter->endElement();
  155. }
  156. if(!is_null($borders[1])) {
  157. $objWriter->startElement('w:left');
  158. $objWriter->writeAttribute('w:val', 'single');
  159. $objWriter->writeAttribute('w:sz', $borders[1]);
  160. $objWriter->writeAttribute('w:space', '24');
  161. $objWriter->writeAttribute('w:color', $borderColor[1]);
  162. $objWriter->endElement();
  163. }
  164. if(!is_null($borders[2])) {
  165. $objWriter->startElement('w:right');
  166. $objWriter->writeAttribute('w:val', 'single');
  167. $objWriter->writeAttribute('w:sz', $borders[2]);
  168. $objWriter->writeAttribute('w:space', '24');
  169. $objWriter->writeAttribute('w:color', $borderColor[2]);
  170. $objWriter->endElement();
  171. }
  172. if(!is_null($borders[3])) {
  173. $objWriter->startElement('w:bottom');
  174. $objWriter->writeAttribute('w:val', 'single');
  175. $objWriter->writeAttribute('w:sz', $borders[3]);
  176. $objWriter->writeAttribute('w:space', '24');
  177. $objWriter->writeAttribute('w:color', $borderColor[3]);
  178. $objWriter->endElement();
  179. }
  180. $objWriter->endElement();
  181. }
  182. $objWriter->startElement('w:cols');
  183. $objWriter->writeAttribute('w:space', '720');
  184. $objWriter->endElement();
  185. $objWriter->endElement();
  186. }
  187. private function _writePageBreak(PHPWord_Shared_XMLWriter $objWriter = null) {
  188. $objWriter->startElement('w:p');
  189. $objWriter->startElement('w:r');
  190. $objWriter->startElement('w:br');
  191. $objWriter->writeAttribute('w:type', 'page');
  192. $objWriter->endElement();
  193. $objWriter->endElement();
  194. $objWriter->endElement();
  195. }
  196. private function _writeListItem(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_ListItem $listItem) {
  197. $textObject = $listItem->getTextObject();
  198. $text = $textObject->getText();
  199. $styleParagraph = $textObject->getParagraphStyle();
  200. $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
  201. $depth = $listItem->getDepth();
  202. $listType = $listItem->getStyle()->getListType();
  203. $objWriter->startElement('w:p');
  204. $objWriter->startElement('w:pPr');
  205. if($SpIsObject) {
  206. $this->_writeParagraphStyle($objWriter, $styleParagraph, true);
  207. } elseif(!$SpIsObject && !is_null($styleParagraph)) {
  208. $objWriter->startElement('w:pStyle');
  209. $objWriter->writeAttribute('w:val', $styleParagraph);
  210. $objWriter->endElement();
  211. }
  212. $objWriter->startElement('w:numPr');
  213. $objWriter->startElement('w:ilvl');
  214. $objWriter->writeAttribute('w:val', $depth);
  215. $objWriter->endElement();
  216. $objWriter->startElement('w:numId');
  217. $objWriter->writeAttribute('w:val', $listType);
  218. $objWriter->endElement();
  219. $objWriter->endElement();
  220. $objWriter->endElement();
  221. $this->_writeText($objWriter, $textObject, true);
  222. $objWriter->endElement();
  223. }
  224. protected function _writeObject(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Object $object) {
  225. $rIdObject = $object->getRelationId();
  226. $rIdImage = $object->getImageRelationId();
  227. $shapeId = md5($rIdObject.'_'.$rIdImage);
  228. $objectId = $object->getObjectId();
  229. $style = $object->getStyle();
  230. $width = $style->getWidth();
  231. $height = $style->getHeight();
  232. $align = $style->getAlign();
  233. $objWriter->startElement('w:p');
  234. if(!is_null($align)) {
  235. $objWriter->startElement('w:pPr');
  236. $objWriter->startElement('w:jc');
  237. $objWriter->writeAttribute('w:val', $align);
  238. $objWriter->endElement();
  239. $objWriter->endElement();
  240. }
  241. $objWriter->startElement('w:r');
  242. $objWriter->startElement('w:object');
  243. $objWriter->writeAttribute('w:dxaOrig', '249');
  244. $objWriter->writeAttribute('w:dyaOrig', '160');
  245. $objWriter->startElement('v:shape');
  246. $objWriter->writeAttribute('id', $shapeId);
  247. $objWriter->writeAttribute('type', '#_x0000_t75');
  248. $objWriter->writeAttribute('style', 'width:104px;height:67px');
  249. $objWriter->writeAttribute('o:ole', '');
  250. $objWriter->startElement('v:imagedata');
  251. $objWriter->writeAttribute('r:id', 'rId'.$rIdImage);
  252. $objWriter->writeAttribute('o:title', '');
  253. $objWriter->endElement();
  254. $objWriter->endElement();
  255. $objWriter->startElement('o:OLEObject');
  256. $objWriter->writeAttribute('Type', 'Embed');
  257. $objWriter->writeAttribute('ProgID', 'Package');
  258. $objWriter->writeAttribute('ShapeID', $shapeId);
  259. $objWriter->writeAttribute('DrawAspect', 'Icon');
  260. $objWriter->writeAttribute('ObjectID', '_'.$objectId);
  261. $objWriter->writeAttribute('r:id', 'rId'.$rIdObject);
  262. $objWriter->endElement();
  263. $objWriter->endElement();
  264. $objWriter->endElement(); // w:r
  265. $objWriter->endElement(); // w:p
  266. }
  267. private function _writeTOC(PHPWord_Shared_XMLWriter $objWriter = null) {
  268. $titles = PHPWord_TOC::getTitles();
  269. $styleFont = PHPWord_TOC::getStyleFont();
  270. $styleTOC = PHPWord_TOC::getStyleTOC();
  271. $fIndent = $styleTOC->getIndent();
  272. $tabLeader = $styleTOC->getTabLeader();
  273. $tabPos = $styleTOC->getTabPos();
  274. $isObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
  275. for($i=0; $i<count($titles); $i++) {
  276. $title = $titles[$i];
  277. $indent = ($title['depth'] - 1) * $fIndent;
  278. $objWriter->startElement('w:p');
  279. $objWriter->startElement('w:pPr');
  280. if($isObject && !is_null($styleFont->getParagraphStyle())) {
  281. $this->_writeParagraphStyle($objWriter, $styleFont->getParagraphStyle());
  282. }
  283. if($indent > 0) {
  284. $objWriter->startElement('w:ind');
  285. $objWriter->writeAttribute('w:left', $indent);
  286. $objWriter->endElement();
  287. }
  288. if(!empty($styleFont) && !$isObject) {
  289. $objWriter->startElement('w:pPr');
  290. $objWriter->startElement('w:pStyle');
  291. $objWriter->writeAttribute('w:val', $styleFont);
  292. $objWriter->endElement();
  293. $objWriter->endElement();
  294. }
  295. $objWriter->startElement('w:tabs');
  296. $objWriter->startElement('w:tab');
  297. $objWriter->writeAttribute('w:val', 'right');
  298. if(!empty($tabLeader)) {
  299. $objWriter->writeAttribute('w:leader', $tabLeader);
  300. }
  301. $objWriter->writeAttribute('w:pos', $tabPos);
  302. $objWriter->endElement();
  303. $objWriter->endElement();
  304. $objWriter->endElement(); // w:pPr
  305. if($i == 0) {
  306. $objWriter->startElement('w:r');
  307. $objWriter->startElement('w:fldChar');
  308. $objWriter->writeAttribute('w:fldCharType', 'begin');
  309. $objWriter->endElement();
  310. $objWriter->endElement();
  311. $objWriter->startElement('w:r');
  312. $objWriter->startElement('w:instrText');
  313. $objWriter->writeAttribute('xml:space', 'preserve');
  314. $objWriter->writeRaw('TOC \o "1-9" \h \z \u');
  315. $objWriter->endElement();
  316. $objWriter->endElement();
  317. $objWriter->startElement('w:r');
  318. $objWriter->startElement('w:fldChar');
  319. $objWriter->writeAttribute('w:fldCharType', 'separate');
  320. $objWriter->endElement();
  321. $objWriter->endElement();
  322. }
  323. $objWriter->startElement('w:hyperlink');
  324. $objWriter->writeAttribute('w:anchor', $title['anchor']);
  325. $objWriter->writeAttribute('w:history', '1');
  326. $objWriter->startElement('w:r');
  327. if($isObject) {
  328. $this->_writeTextStyle($objWriter, $styleFont);
  329. }
  330. $objWriter->startElement('w:t');
  331. $objWriter->writeRaw($title['text']);
  332. $objWriter->endElement();
  333. $objWriter->endElement();
  334. $objWriter->startElement('w:r');
  335. $objWriter->writeElement('w:tab', null);
  336. $objWriter->endElement();
  337. $objWriter->startElement('w:r');
  338. $objWriter->startElement('w:fldChar');
  339. $objWriter->writeAttribute('w:fldCharType', 'begin');
  340. $objWriter->endElement();
  341. $objWriter->endElement();
  342. $objWriter->startElement('w:r');
  343. $objWriter->startElement('w:instrText');
  344. $objWriter->writeAttribute('xml:space', 'preserve');
  345. $objWriter->writeRaw('PAGEREF '.$title['anchor'].' \h');
  346. $objWriter->endElement();
  347. $objWriter->endElement();
  348. $objWriter->startElement('w:r');
  349. $objWriter->startElement('w:fldChar');
  350. $objWriter->writeAttribute('w:fldCharType', 'end');
  351. $objWriter->endElement();
  352. $objWriter->endElement();
  353. $objWriter->endElement(); // w:hyperlink
  354. $objWriter->endElement(); // w:p
  355. }
  356. $objWriter->startElement('w:p');
  357. $objWriter->startElement('w:r');
  358. $objWriter->startElement('w:fldChar');
  359. $objWriter->writeAttribute('w:fldCharType', 'end');
  360. $objWriter->endElement();
  361. $objWriter->endElement();
  362. $objWriter->endElement();
  363. }
  364. }