Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\Word2007\Part;
  18. use PhpOffice\PhpWord\Element\Section;
  19. use PhpOffice\PhpWord\Shared\XMLWriter;
  20. use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
  21. use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter;
  22. /**
  23. * Word2007 document part writer: word/document.xml
  24. */
  25. class Document extends AbstractPart
  26. {
  27. /**
  28. * Write part
  29. *
  30. * @return string
  31. */
  32. public function write()
  33. {
  34. $phpWord = $this->getParentWriter()->getPhpWord();
  35. $xmlWriter = $this->getXmlWriter();
  36. $sections = $phpWord->getSections();
  37. $sectionCount = count($sections);
  38. $currentSection = 0;
  39. $drawingSchema = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing';
  40. $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
  41. $xmlWriter->startElement('w:document');
  42. $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
  43. $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  44. $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
  45. $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
  46. $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  47. $xmlWriter->writeAttribute('xmlns:wp', $drawingSchema);
  48. $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
  49. $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
  50. $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
  51. $xmlWriter->startElement('w:body');
  52. if ($sectionCount > 0) {
  53. foreach ($sections as $section) {
  54. $currentSection++;
  55. $containerWriter = new Container($xmlWriter, $section);
  56. $containerWriter->write();
  57. if ($currentSection == $sectionCount) {
  58. $this->writeSectionSettings($xmlWriter, $section);
  59. } else {
  60. $this->writeSection($xmlWriter, $section);
  61. }
  62. }
  63. }
  64. $xmlWriter->endElement(); // w:body
  65. $xmlWriter->endElement(); // w:document
  66. return $xmlWriter->getData();
  67. }
  68. /**
  69. * Write begin section.
  70. *
  71. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  72. * @param \PhpOffice\PhpWord\Element\Section $section
  73. * @return void
  74. */
  75. private function writeSection(XMLWriter $xmlWriter, Section $section)
  76. {
  77. $xmlWriter->startElement('w:p');
  78. $xmlWriter->startElement('w:pPr');
  79. $this->writeSectionSettings($xmlWriter, $section);
  80. $xmlWriter->endElement();
  81. $xmlWriter->endElement();
  82. }
  83. /**
  84. * Write end section.
  85. *
  86. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  87. * @param \PhpOffice\PhpWord\Element\Section $section
  88. * @return void
  89. */
  90. private function writeSectionSettings(XMLWriter $xmlWriter, Section $section)
  91. {
  92. $xmlWriter->startElement('w:sectPr');
  93. // Header reference
  94. foreach ($section->getHeaders() as $header) {
  95. $rId = $header->getRelationId();
  96. $xmlWriter->startElement('w:headerReference');
  97. $xmlWriter->writeAttribute('w:type', $header->getType());
  98. $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
  99. $xmlWriter->endElement();
  100. }
  101. // Footer reference
  102. foreach ($section->getFooters() as $footer) {
  103. $rId = $footer->getRelationId();
  104. $xmlWriter->startElement('w:footerReference');
  105. $xmlWriter->writeAttribute('w:type', $footer->getType());
  106. $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
  107. $xmlWriter->endElement();
  108. }
  109. // Different first page
  110. if ($section->hasDifferentFirstPage()) {
  111. $xmlWriter->startElement('w:titlePg');
  112. $xmlWriter->endElement();
  113. }
  114. // Section settings
  115. $styleWriter = new SectionStyleWriter($xmlWriter, $section->getStyle());
  116. $styleWriter->write();
  117. $xmlWriter->endElement(); // w:sectPr
  118. }
  119. }