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
|
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
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-17 16:19:23 +07:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-04-10 11:47:26 +04:00
|
|
|
use PhpOffice\PhpWord\Element\Section;
|
|
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
|
|
|
|
|
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-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-04-03 06:54:45 +07:00
|
|
|
$sections = $phpWord->getSections();
|
|
|
|
|
$countSections = count($sections);
|
2014-03-22 10:06:08 +04:00
|
|
|
$pSection = 0;
|
|
|
|
|
|
|
|
|
|
if ($countSections > 0) {
|
2014-04-03 06:54:45 +07:00
|
|
|
foreach ($sections as $section) {
|
2014-03-22 10:06:08 +04:00
|
|
|
$pSection++;
|
|
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
$this->writeContainerElements($xmlWriter, $section);
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
if ($pSection == $countSections) {
|
2014-03-30 18:51:25 +07:00
|
|
|
$this->writeEndSection($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
|
|
|
|
|
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-04-03 11:34:06 +07:00
|
|
|
$this->writeEndSection($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-03-30 18:51:25 +07:00
|
|
|
private function writeEndSection(XMLWriter $xmlWriter, Section $section)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$settings = $section->getSettings();
|
2014-04-03 06:54:45 +07:00
|
|
|
$headers = $section->getHeaders();
|
2014-04-05 19:02:49 +07:00
|
|
|
$footers = $section->getFooters();
|
2014-03-22 10:06:08 +04:00
|
|
|
$pgSzW = $settings->getPageSizeW();
|
|
|
|
|
$pgSzH = $settings->getPageSizeH();
|
|
|
|
|
$orientation = $settings->getOrientation();
|
|
|
|
|
|
|
|
|
|
$marginTop = $settings->getMarginTop();
|
|
|
|
|
$marginLeft = $settings->getMarginLeft();
|
|
|
|
|
$marginRight = $settings->getMarginRight();
|
|
|
|
|
$marginBottom = $settings->getMarginBottom();
|
|
|
|
|
|
|
|
|
|
$headerHeight = $settings->getHeaderHeight();
|
|
|
|
|
$footerHeight = $settings->getFooterHeight();
|
|
|
|
|
|
|
|
|
|
$borders = $settings->getBorderSize();
|
|
|
|
|
|
|
|
|
|
$colsNum = $settings->getColsNum();
|
|
|
|
|
$colsSpace = $settings->getColsSpace();
|
|
|
|
|
$breakType = $settings->getBreakType();
|
|
|
|
|
|
|
|
|
|
$xmlWriter->startElement('w:sectPr');
|
|
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Section break
|
|
|
|
|
if (!is_null($breakType)) {
|
|
|
|
|
$xmlWriter->startElement('w:type');
|
|
|
|
|
$xmlWriter->writeAttribute('w:val', $breakType);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Header reference
|
|
|
|
|
foreach ($headers as &$header) {
|
|
|
|
|
$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-04-03 06:54:45 +07:00
|
|
|
// Footer reference
|
2014-04-05 19:02:49 +07:00
|
|
|
foreach ($footers 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-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-04-03 06:54:45 +07:00
|
|
|
// Page size & orientation
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:pgSz');
|
|
|
|
|
$xmlWriter->writeAttribute('w:w', $pgSzW);
|
|
|
|
|
$xmlWriter->writeAttribute('w:h', $pgSzH);
|
|
|
|
|
if (!is_null($orientation) && strtolower($orientation) != 'portrait') {
|
|
|
|
|
$xmlWriter->writeAttribute('w:orient', $orientation);
|
|
|
|
|
}
|
2014-04-03 06:54:45 +07:00
|
|
|
$xmlWriter->endElement(); // w:pgSz
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Margins
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:pgMar');
|
|
|
|
|
$xmlWriter->writeAttribute('w:top', $marginTop);
|
|
|
|
|
$xmlWriter->writeAttribute('w:right', $marginRight);
|
|
|
|
|
$xmlWriter->writeAttribute('w:bottom', $marginBottom);
|
|
|
|
|
$xmlWriter->writeAttribute('w:left', $marginLeft);
|
|
|
|
|
$xmlWriter->writeAttribute('w:header', $headerHeight);
|
|
|
|
|
$xmlWriter->writeAttribute('w:footer', $footerHeight);
|
|
|
|
|
$xmlWriter->writeAttribute('w:gutter', '0');
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Borders
|
|
|
|
|
$hasBorders = false;
|
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
|
if (!is_null($borders[$i])) {
|
|
|
|
|
$hasBorders = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($hasBorders) {
|
2014-03-22 10:06:08 +04:00
|
|
|
$borderColor = $settings->getBorderColor();
|
2014-04-26 11:14:27 +07:00
|
|
|
$mbWriter = new \PhpOffice\PhpWord\Writer\Word2007\Style\MarginBorder($xmlWriter);
|
|
|
|
|
$mbWriter->setSizes($borders);
|
|
|
|
|
$mbWriter->setColors($borderColor);
|
|
|
|
|
$mbWriter->setAttributes(array('space' => '24'));
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:pgBorders');
|
|
|
|
|
$xmlWriter->writeAttribute('w:offsetFrom', 'page');
|
2014-04-26 11:14:27 +07:00
|
|
|
$mbWriter->write();
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Page numbering
|
|
|
|
|
if (null !== $settings->getPageNumberingStart()) {
|
|
|
|
|
$xmlWriter->startElement('w:pgNumType');
|
|
|
|
|
$xmlWriter->writeAttribute('w:start', $section->getSettings()->getPageNumberingStart());
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 06:54:45 +07:00
|
|
|
// Columns
|
2014-03-22 10:06:08 +04:00
|
|
|
$xmlWriter->startElement('w:cols');
|
|
|
|
|
$xmlWriter->writeAttribute('w:num', $colsNum);
|
|
|
|
|
$xmlWriter->writeAttribute('w:space', $colsSpace);
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
|
|
|
|
|
$xmlWriter->endElement();
|
|
|
|
|
}
|
|
|
|
|
}
|