Document.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Reader\Word2007;
  18. use PhpOffice\PhpWord\Element\Section;
  19. use PhpOffice\PhpWord\PhpWord;
  20. use PhpOffice\PhpWord\Shared\XMLReader;
  21. /**
  22. * Document reader
  23. *
  24. * @since 0.10.0
  25. * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
  26. */
  27. class Document extends AbstractPart
  28. {
  29. /**
  30. * PhpWord object
  31. *
  32. * @var \PhpOffice\PhpWord\PhpWord
  33. */
  34. private $phpWord;
  35. /**
  36. * Read document.xml.
  37. *
  38. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  39. */
  40. public function read(PhpWord $phpWord)
  41. {
  42. $this->phpWord = $phpWord;
  43. $xmlReader = new XMLReader();
  44. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  45. $readMethods = array('w:p' => 'readWPNode', 'w:tbl' => 'readTable', 'w:sectPr' => 'readWSectPrNode');
  46. $nodes = $xmlReader->getElements('w:body/*');
  47. if ($nodes->length > 0) {
  48. $section = $this->phpWord->addSection();
  49. foreach ($nodes as $node) {
  50. if (isset($readMethods[$node->nodeName])) {
  51. $readMethod = $readMethods[$node->nodeName];
  52. $this->$readMethod($xmlReader, $node, $section);
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * Read header footer.
  59. *
  60. * @param array $settings
  61. * @param \PhpOffice\PhpWord\Element\Section &$section
  62. */
  63. private function readHeaderFooter($settings, Section &$section)
  64. {
  65. $readMethods = array('w:p' => 'readParagraph', 'w:tbl' => 'readTable');
  66. if (is_array($settings) && isset($settings['hf'])) {
  67. foreach ($settings['hf'] as $rId => $hfSetting) {
  68. if (isset($this->rels['document'][$rId])) {
  69. list($hfType, $xmlFile, $docPart) = array_values($this->rels['document'][$rId]);
  70. $addMethod = "add{$hfType}";
  71. $hfObject = $section->$addMethod($hfSetting['type']);
  72. // Read header/footer content
  73. $xmlReader = new XMLReader();
  74. $xmlReader->getDomFromZip($this->docFile, $xmlFile);
  75. $nodes = $xmlReader->getElements('*');
  76. if ($nodes->length > 0) {
  77. foreach ($nodes as $node) {
  78. if (isset($readMethods[$node->nodeName])) {
  79. $readMethod = $readMethods[$node->nodeName];
  80. $this->$readMethod($xmlReader, $node, $hfObject, $docPart);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * Read w:sectPr
  90. *
  91. * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
  92. * @param \DOMElement $domNode
  93. * @ignoreScrutinizerPatch
  94. * @return array
  95. */
  96. private function readSectionStyle(XMLReader $xmlReader, \DOMElement $domNode)
  97. {
  98. $styleDefs = array(
  99. 'breakType' => array(self::READ_VALUE, 'w:type'),
  100. 'vAlign' => array(self::READ_VALUE, 'w:vAlign'),
  101. 'pageSizeW' => array(self::READ_VALUE, 'w:pgSz', 'w:w'),
  102. 'pageSizeH' => array(self::READ_VALUE, 'w:pgSz', 'w:h'),
  103. 'orientation' => array(self::READ_VALUE, 'w:pgSz', 'w:orient'),
  104. 'colsNum' => array(self::READ_VALUE, 'w:cols', 'w:num'),
  105. 'colsSpace' => array(self::READ_VALUE, 'w:cols', 'w:space'),
  106. 'marginTop' => array(self::READ_VALUE, 'w:pgMar', 'w:top'),
  107. 'marginLeft' => array(self::READ_VALUE, 'w:pgMar', 'w:left'),
  108. 'marginBottom' => array(self::READ_VALUE, 'w:pgMar', 'w:bottom'),
  109. 'marginRight' => array(self::READ_VALUE, 'w:pgMar', 'w:right'),
  110. 'headerHeight' => array(self::READ_VALUE, 'w:pgMar', 'w:header'),
  111. 'footerHeight' => array(self::READ_VALUE, 'w:pgMar', 'w:footer'),
  112. 'gutter' => array(self::READ_VALUE, 'w:pgMar', 'w:gutter'),
  113. );
  114. $styles = $this->readStyleDefs($xmlReader, $domNode, $styleDefs);
  115. // Header and footer
  116. // @todo Cleanup this part
  117. $nodes = $xmlReader->getElements('*', $domNode);
  118. foreach ($nodes as $node) {
  119. if ($node->nodeName == 'w:headerReference' || $node->nodeName == 'w:footerReference') {
  120. $id = $xmlReader->getAttribute('r:id', $node);
  121. $styles['hf'][$id] = array(
  122. 'method' => str_replace('w:', '', str_replace('Reference', '', $node->nodeName)),
  123. 'type' => $xmlReader->getAttribute('w:type', $node),
  124. );
  125. }
  126. }
  127. return $styles;
  128. }
  129. /**
  130. * Read w:p node.
  131. *
  132. * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
  133. * @param \DOMElement $node
  134. * @param \PhpOffice\PhpWord\Element\Section &$section
  135. *
  136. * @todo <w:lastRenderedPageBreak>
  137. */
  138. private function readWPNode(XMLReader $xmlReader, \DOMElement $node, Section &$section)
  139. {
  140. // Page break
  141. if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') {
  142. $section->addPageBreak(); // PageBreak
  143. }
  144. // Paragraph
  145. $this->readParagraph($xmlReader, $node, $section);
  146. // Section properties
  147. if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
  148. $sectPrNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
  149. if ($sectPrNode !== null) {
  150. $this->readWSectPrNode($xmlReader, $sectPrNode, $section);
  151. }
  152. $section = $this->phpWord->addSection();
  153. }
  154. }
  155. /**
  156. * Read w:sectPr node.
  157. *
  158. * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
  159. * @param \DOMElement $node
  160. * @param \PhpOffice\PhpWord\Element\Section &$section
  161. */
  162. private function readWSectPrNode(XMLReader $xmlReader, \DOMElement $node, Section &$section)
  163. {
  164. $style = $this->readSectionStyle($xmlReader, $node);
  165. $section->setStyle($style);
  166. $this->readHeaderFooter($style, $section);
  167. }
  168. }