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
|
2014-03-26 17:21:23 +07:00
|
|
|
* @copyright 2014 PHPWord
|
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord;
|
|
|
|
|
|
|
|
|
|
use PhpOffice\PhpWord\DocumentProperties;
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
|
|
|
|
use PhpOffice\PhpWord\Container\Section;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\Style;
|
|
|
|
|
use PhpOffice\PhpWord\Template;
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord main class
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
class PhpWord
|
|
|
|
|
{
|
|
|
|
|
const DEFAULT_FONT_COLOR = '000000'; // HEX
|
|
|
|
|
const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
|
|
|
|
|
const DEFAULT_FONT_NAME = 'Arial';
|
2014-04-02 11:02:56 +07:00
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
/**
|
|
|
|
|
* Default font size, in points.
|
|
|
|
|
*
|
|
|
|
|
* OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
|
|
|
|
|
* use, and the conversion will be conducted during XML writing.
|
|
|
|
|
*/
|
|
|
|
|
const DEFAULT_FONT_SIZE = 10;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Document properties object
|
|
|
|
|
*
|
2014-04-02 11:02:56 +07:00
|
|
|
* @var DocumentProperties
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
private $_documentProperties;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Default font name
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $_defaultFontName;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Default font size
|
2014-03-22 10:06:08 +04:00
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $_defaultFontSize;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Collection of sections
|
|
|
|
|
*
|
2014-04-02 11:02:56 +07:00
|
|
|
* @var Section[]
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
private $_sections = array();
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* Create new
|
|
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->_documentProperties = new DocumentProperties();
|
|
|
|
|
$this->_defaultFontName = self::DEFAULT_FONT_NAME;
|
|
|
|
|
$this->_defaultFontSize = self::DEFAULT_FONT_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Get document properties object
|
|
|
|
|
*
|
2014-04-02 11:02:56 +07:00
|
|
|
* @return DocumentProperties
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function getDocumentProperties()
|
|
|
|
|
{
|
|
|
|
|
return $this->_documentProperties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Set document properties object
|
|
|
|
|
*
|
2014-03-23 10:32:08 +04:00
|
|
|
* @param \PhpOffice\PhpWord\DocumentProperties $documentProperties
|
|
|
|
|
* @return \PhpOffice\PhpWord\PhpWord
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function setDocumentProperties(DocumentProperties $documentProperties)
|
|
|
|
|
{
|
|
|
|
|
$this->_documentProperties = $documentProperties;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Create new section
|
|
|
|
|
*
|
2014-03-31 01:13:02 +07:00
|
|
|
* @param \PhpOffice\PhpWord\Container\Settings $settings
|
2014-04-02 11:02:56 +07:00
|
|
|
* @return Section
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
2014-04-02 11:02:56 +07:00
|
|
|
public function addSection($settings = null)
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
|
|
|
|
$section = new Section(\count($this->_sections) + 1, $settings);
|
|
|
|
|
$this->_sections[] = $section;
|
|
|
|
|
|
|
|
|
|
return $section;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Get default font name
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDefaultFontName()
|
|
|
|
|
{
|
|
|
|
|
return $this->_defaultFontName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Set default font name
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @param string $fontName
|
|
|
|
|
*/
|
|
|
|
|
public function setDefaultFontName($fontName)
|
|
|
|
|
{
|
|
|
|
|
$this->_defaultFontName = $fontName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Get default font size
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDefaultFontSize()
|
|
|
|
|
{
|
|
|
|
|
return $this->_defaultFontSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Set default font size
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @param int $fontSize
|
|
|
|
|
*/
|
|
|
|
|
public function setDefaultFontSize($fontSize)
|
|
|
|
|
{
|
|
|
|
|
$this->_defaultFontSize = $fontSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set default paragraph style definition to styles.xml
|
|
|
|
|
*
|
|
|
|
|
* @param array $styles Paragraph style definition
|
|
|
|
|
*/
|
|
|
|
|
public function setDefaultParagraphStyle($styles)
|
|
|
|
|
{
|
|
|
|
|
Style::setDefaultParagraphStyle($styles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a paragraph style definition to styles.xml
|
|
|
|
|
*
|
|
|
|
|
* @param $styleName string
|
|
|
|
|
* @param $styles array
|
|
|
|
|
*/
|
|
|
|
|
public function addParagraphStyle($styleName, $styles)
|
|
|
|
|
{
|
|
|
|
|
Style::addParagraphStyle($styleName, $styles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a font style definition to styles.xml
|
|
|
|
|
*
|
|
|
|
|
* @param $styleName string
|
2014-03-24 00:26:10 +07:00
|
|
|
* @param mixed $styleFont
|
|
|
|
|
* @param mixed $styleParagraph
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function addFontStyle($styleName, $styleFont, $styleParagraph = null)
|
|
|
|
|
{
|
|
|
|
|
Style::addFontStyle($styleName, $styleFont, $styleParagraph);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a table style definition to styles.xml
|
|
|
|
|
*
|
2014-03-24 00:26:10 +07:00
|
|
|
* @param string $styleName
|
|
|
|
|
* @param mixed $styleTable
|
|
|
|
|
* @param mixed $styleFirstRow
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
|
|
|
|
|
{
|
|
|
|
|
Style::addTableStyle($styleName, $styleTable, $styleFirstRow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a heading style definition to styles.xml
|
|
|
|
|
*
|
2014-03-24 00:26:10 +07:00
|
|
|
* @param int $titleCount
|
|
|
|
|
* @param mixed $styleFont
|
|
|
|
|
* @param mixed $styleParagraph
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
|
|
|
|
|
{
|
|
|
|
|
Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a hyperlink style to styles.xml
|
|
|
|
|
*
|
2014-03-24 00:26:10 +07:00
|
|
|
* @param string $styleName
|
|
|
|
|
* @param mixed $styles
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function addLinkStyle($styleName, $styles)
|
|
|
|
|
{
|
|
|
|
|
Style::addLinkStyle($styleName, $styles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Get all sections
|
|
|
|
|
*
|
2014-03-31 01:13:02 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Container\Section[]
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function getSections()
|
|
|
|
|
{
|
|
|
|
|
return $this->_sections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Load template by filename
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @param string $filename Fully qualified filename.
|
2014-04-02 11:02:56 +07:00
|
|
|
* @return Template
|
|
|
|
|
* @throws Exception
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public function loadTemplate($filename)
|
|
|
|
|
{
|
|
|
|
|
if (\file_exists($filename)) {
|
|
|
|
|
return new Template($filename);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("Template file {$filename} not found.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-02 11:02:56 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create new section
|
|
|
|
|
*
|
|
|
|
|
* @param \PhpOffice\PhpWord\Container\Settings $settings
|
|
|
|
|
* @return Section
|
|
|
|
|
* @deprecated 0.9.2
|
|
|
|
|
*/
|
|
|
|
|
public function createSection($settings = null)
|
|
|
|
|
{
|
|
|
|
|
return $this->addSection($settings);
|
|
|
|
|
}
|
2014-03-23 17:37:26 +07:00
|
|
|
}
|