2014-03-22 10:06:08 +04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord
|
2014-03-22 10:06:08 +04:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
|
|
|
|
* @copyright 2014 PHPWord
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
|
2014-04-30 08:54:38 +07:00
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-04-10 11:47:26 +04:00
|
|
|
use PhpOffice\PhpWord\Element\Section;
|
|
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-05-04 21:03:28 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
2014-05-01 18:37:34 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\Word2007\Style\Section as SectionStyleWriter;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Word2007 document part writer
|
|
|
|
|
*/
|
2014-04-30 08:54:38 +07:00
|
|
|
class Document extends AbstractPart
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write word/document.xml
|
|
|
|
|
*
|
2014-04-10 11:47:26 +04:00
|
|
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
public function writeDocument(PhpWord $phpWord = null)
|
|
|
|
|
{
|
2014-04-05 01:41:48 +07:00
|
|
|
if (is_null($phpWord)) {
|
|
|
|
|
throw new Exception("No PhpWord assigned.");
|
|
|
|
|
}
|
2014-03-30 16:31:01 +07:00
|
|
|
$xmlWriter = $this->getXmlWriter();
|
2014-05-01 18:37:34 +07:00
|
|
|
$sections = $phpWord->getSections();
|
|
|
|
|
$sectionCount = count($sections);
|
|
|
|
|
$currentSection = 0;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
|
|
|
|
|
$xmlWriter->startElement('w:document');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
|
|
|
|
|
|
|
|
|
|
$xmlWriter->startElement('w:body');
|
|
|
|
|
|
|
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
if ($sectionCount > 0) {
|
2014-04-03 06:54:45 +07:00
|
|
|
foreach ($sections as $section) {
|
2014-05-01 18:37:34 +07:00
|
|
|
$currentSection++;
|
2014-04-03 06:54:45 +07:00
|
|
|
$this->writeContainerElements($xmlWriter, $section);
|
2014-05-01 18:37:34 +07:00
|
|
|
if ($currentSection == $sectionCount) {
|
|
|
|
|
$this->writeSectionSettings($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
} else {
|
2014-03-30 18:51:25 +07:00
|
|
|
$this->writeSection($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 22:13:00 +07:00
|
|
|
$xmlWriter->endElement(); // w:body
|
|
|
|
|
$xmlWriter->endElement(); // w:document
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
return $xmlWriter->getData();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write begin section
|
|
|
|
|
*
|
2014-04-10 11:47:26 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
|
|
|
|
* @param \PhpOffice\PhpWord\Element\Section $section
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-03-30 18:51:25 +07:00
|
|
|
private function writeSection(XMLWriter $xmlWriter, Section $section)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$xmlWriter->startElement('w:p');
|
|
|
|
|
$xmlWriter->startElement('w:pPr');
|
2014-05-01 18:37:34 +07:00
|
|
|
$this->writeSectionSettings($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write end section
|
|
|
|
|
*
|
2014-04-10 11:47:26 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
|
|
|
|
* @param \PhpOffice\PhpWord\Element\Section $section
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-05-01 18:37:34 +07:00
|
|
|
private function writeSectionSettings(XMLWriter $xmlWriter, Section $section)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$xmlWriter->startElement('w:sectPr');
|
|
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Header reference
|
2014-05-01 18:37:34 +07:00
|
|
|
foreach ($section->getHeaders() as $header) {
|
2014-04-03 06:54:45 +07:00
|
|
|
$rId = $header->getRelationId();
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:headerReference');
|
2014-04-03 06:54:45 +07:00
|
|
|
$xmlWriter->writeAttribute('w:type', $header->getType());
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-05-01 18:37:34 +07:00
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Footer reference
|
2014-05-01 18:37:34 +07:00
|
|
|
foreach ($section->getFooters() as $footer) {
|
2014-04-03 06:54:45 +07:00
|
|
|
$rId = $footer->getRelationId();
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:footerReference');
|
2014-04-05 19:02:49 +07:00
|
|
|
$xmlWriter->writeAttribute('w:type', $footer->getType());
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->writeAttribute('r:id', 'rId' . $rId);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-05-01 18:37:34 +07:00
|
|
|
|
2014-04-05 19:02:49 +07:00
|
|
|
// Different first page
|
|
|
|
|
if ($section->hasDifferentFirstPage()) {
|
|
|
|
|
$xmlWriter->startElement('w:titlePg');
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
// Section settings
|
|
|
|
|
$styleWriter = new SectionStyleWriter($xmlWriter, $section->getSettings());
|
|
|
|
|
$styleWriter->write();
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-05-01 18:37:34 +07:00
|
|
|
$xmlWriter->endElement(); // w:sectPr
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
}
|