Merge pull request #115 from ivanlanin/develop

Reorganize samples and write unsupported element to ODText and RTF
This commit is contained in:
Progi1984 2014-03-13 15:04:10 +01:00
commit 8e175984b6
41 changed files with 777 additions and 385 deletions

1
.gitignore vendored
View File

@ -19,4 +19,3 @@ vendor
*.rtf *.rtf
*.txt *.txt
*.xml *.xml
nbproject

View File

@ -254,8 +254,8 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
if ($elm->getName() != 'style') { if ($elm->getName() != 'style') {
continue; continue;
} }
unset($pStyle); $pStyle = null;
unset($fStyle); $fStyle = null;
$hasParagraphStyle = isset($elm->pPr); $hasParagraphStyle = isset($elm->pPr);
$hasFontStyle = isset($elm->rPr); $hasFontStyle = isset($elm->rPr);
$styleName = (string)$elm->name['val']; $styleName = (string)$elm->name['val'];

View File

@ -243,7 +243,9 @@ class PHPWord_Section
$this->_elementCollection[] = $object; $this->_elementCollection[] = $object;
return $object; return $object;
} else { } else {
trigger_error('Source does not exist or unsupported object type.'); throw new PHPWord_Exception(
'Source does not exist or unsupported object type.'
);
} }
} }
@ -265,7 +267,9 @@ class PHPWord_Section
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
trigger_error('Source does not exist or unsupported image type.'); throw new PHPWord_Exception(
'Source does not exist or unsupported image type.'
);
} }
} }
@ -286,7 +290,9 @@ class PHPWord_Section
$this->_elementCollection[] = $memoryImage; $this->_elementCollection[] = $memoryImage;
return $memoryImage; return $memoryImage;
} else { } else {
trigger_error('Unsupported image type.'); throw new PHPWord_Exception(
'Unsupported image type.'
);
} }
} }

View File

@ -133,7 +133,7 @@ class PHPWord_Section_Footer
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
trigger_error('Src does not exist or invalid image type.', E_USER_ERROR); throw new Exception('Src does not exist or invalid image type.');
} }
} }
@ -154,7 +154,7 @@ class PHPWord_Section_Footer
$this->_elementCollection[] = $memoryImage; $this->_elementCollection[] = $memoryImage;
return $memoryImage; return $memoryImage;
} else { } else {
trigger_error('Unsupported image type.'); throw new Exception('Unsupported image type.');
} }
} }

View File

@ -162,7 +162,7 @@ class PHPWord_Section_Header
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
trigger_error('Src does not exist or invalid image type.', E_USER_ERROR); throw new Exception('Src does not exist or invalid image type.');
} }
} }
@ -183,7 +183,7 @@ class PHPWord_Section_Header
$this->_elementCollection[] = $memoryImage; $this->_elementCollection[] = $memoryImage;
return $memoryImage; return $memoryImage;
} else { } else {
trigger_error('Unsupported image type.'); throw new Exception('Unsupported image type.');
} }
} }
@ -223,7 +223,7 @@ class PHPWord_Section_Header
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
trigger_error('Src does not exist or invalid image type.', E_USER_ERROR); throw new Exception('Src does not exist or invalid image type.');
} }
} }

View File

@ -140,7 +140,7 @@ class PHPWord_Section_Table_Cell
$this->_elementCollection[] = $link; $this->_elementCollection[] = $link;
return $link; return $link;
} else { } else {
trigger_error('Unsupported Link header / footer reference'); throw new Exception('Unsupported Link header / footer reference');
return false; return false;
} }
} }
@ -198,7 +198,7 @@ class PHPWord_Section_Table_Cell
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
trigger_error('Source does not exist or unsupported image type.'); throw new Exception('Source does not exist or unsupported image type.');
} }
} }
@ -225,7 +225,7 @@ class PHPWord_Section_Table_Cell
$this->_elementCollection[] = $memoryImage; $this->_elementCollection[] = $memoryImage;
return $memoryImage; return $memoryImage;
} else { } else {
trigger_error('Unsupported image type.'); throw new Exception('Unsupported image type.');
} }
} }
@ -266,7 +266,7 @@ class PHPWord_Section_Table_Cell
$this->_elementCollection[] = $object; $this->_elementCollection[] = $object;
return $object; return $object;
} else { } else {
trigger_error('Source does not exist or unsupported object type.'); throw new Exception('Source does not exist or unsupported object type.');
} }
} }
@ -288,7 +288,7 @@ class PHPWord_Section_Table_Cell
$this->_elementCollection[] = $ptext; $this->_elementCollection[] = $ptext;
return $ptext; return $ptext;
} else { } else {
trigger_error('addPreserveText only supported in footer/header.'); throw new Exception('addPreserveText only supported in footer/header.');
} }
} }

View File

@ -127,7 +127,7 @@ class PHPWord_Section_TextRun
$this->_elementCollection[] = $image; $this->_elementCollection[] = $image;
return $image; return $image;
} else { } else {
trigger_error('Source does not exist or unsupported image type.'); throw new Exception('Source does not exist or unsupported image type.');
} }
} }

View File

@ -28,7 +28,8 @@
/** /**
* PHPWord_Settings * PHPWord_Settings
*/ */
class PHPWord_Settings { class PHPWord_Settings
{
/** /**
* Compatibility option for XMLWriter * Compatibility option for XMLWriter
* *
@ -42,12 +43,13 @@ class PHPWord_Settings {
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility * @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
* @return boolean Success or failure * @return boolean Success or failure
*/ */
public static function setCompatibility($compatibility) { public static function setCompatibility($compatibility)
{
if (is_bool($compatibility)) { if (is_bool($compatibility)) {
self::$_xmlWriterCompatibility = $compatibility; self::$_xmlWriterCompatibility = $compatibility;
return TRUE; return true;
} }
return FALSE; return false;
} // function setCompatibility() } // function setCompatibility()
@ -56,7 +58,8 @@ class PHPWord_Settings {
* *
* @return boolean Compatibility * @return boolean Compatibility
*/ */
public static function getCompatibility() { public static function getCompatibility()
{
return self::$_xmlWriterCompatibility; return self::$_xmlWriterCompatibility;
} // function getCompatibility() } // function getCompatibility()
} }

View File

@ -179,8 +179,6 @@ class PHPWord_Style_Font
} elseif (is_array($paragraphStyle)) { } elseif (is_array($paragraphStyle)) {
$this->_paragraphStyle = new PHPWord_Style_Paragraph; $this->_paragraphStyle = new PHPWord_Style_Paragraph;
$this->_paragraphStyle->setArrayStyle($paragraphStyle); $this->_paragraphStyle->setArrayStyle($paragraphStyle);
} elseif (null === $paragraphStyle) {
$this->_paragraphStyle = new PHPWord_Style_Paragraph;
} else { } else {
$this->_paragraphStyle = $paragraphStyle; $this->_paragraphStyle = $paragraphStyle;
} }

View File

@ -36,22 +36,20 @@ class PHPWord_Style_Row
* *
* @var bool * @var bool
*/ */
private $_tblHeader; private $_tblHeader = false;
/** /**
* Table row cannot break across pages * Table row cannot break across pages
* *
* @var bool * @var bool
*/ */
private $_cantSplit; private $_cantSplit = false;
/** /**
* Create a new row style * Create a new row style
*/ */
public function __construct() public function __construct()
{ {
$this->_tblHeader = null;
$this->_cantSplit = null;
} }
/** /**
@ -62,23 +60,31 @@ class PHPWord_Style_Row
$this->$key = $value; $this->$key = $value;
} }
public function setTblHeader($pValue = null) public function setTblHeader($pValue = false)
{ {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_tblHeader = $pValue; $this->_tblHeader = $pValue;
return $this;
} }
public function getTblHeader() public function getTblHeader()
{ {
return $this->_tblHeader ? 1 : 0; return $this->_tblHeader;
} }
public function setCantSplit($pValue = null) public function setCantSplit($pValue = false)
{ {
if (!is_bool($pValue)) {
$pValue = false;
}
$this->_cantSplit = $pValue; $this->_cantSplit = $pValue;
return $this;
} }
public function getCantSplit() public function getCantSplit()
{ {
return $this->_cantSplit ? 1 : 0; return $this->_cantSplit;
} }
} }

View File

@ -164,7 +164,7 @@ class PHPWord_Template
$rowStart = strrpos($this->_documentXML, "<w:tr>", ((strlen($this->_documentXML) - $offset) * -1)); $rowStart = strrpos($this->_documentXML, "<w:tr>", ((strlen($this->_documentXML) - $offset) * -1));
} }
if (!$rowStart) { if (!$rowStart) {
trigger_error("Can not find the start position of the row to clone."); throw new Exception("Can not find the start position of the row to clone.");
return false; return false;
} }
return $rowStart; return $rowStart;
@ -208,7 +208,7 @@ class PHPWord_Template
$tagPos = strpos($this->_documentXML, $search); $tagPos = strpos($this->_documentXML, $search);
if (!$tagPos) { if (!$tagPos) {
trigger_error("Can not clone row, template variable not found or variable contains markup."); throw new Exception("Can not clone row, template variable not found or variable contains markup.");
return false; return false;
} }

View File

@ -253,28 +253,25 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
$this->_writeTextRun($objWriter, $element); $this->_writeTextRun($objWriter, $element);
} elseif ($element instanceof PHPWord_Section_TextBreak) { } elseif ($element instanceof PHPWord_Section_TextBreak) {
$this->_writeTextBreak($objWriter); $this->_writeTextBreak($objWriter);
/*
} elseif ($element instanceof PHPWord_Section_Link) { } elseif ($element instanceof PHPWord_Section_Link) {
$this->_writeLink($objWriter, $element); $this->writeUnsupportedElement($objWriter, 'link');
} elseif ($element instanceof PHPWord_Section_Title) { } elseif ($element instanceof PHPWord_Section_Title) {
$this->_writeTitle($objWriter, $element); $this->writeUnsupportedElement($objWriter, 'title');
} elseif ($element instanceof PHPWord_Section_PageBreak) { } elseif ($element instanceof PHPWord_Section_PageBreak) {
$this->_writePageBreak($objWriter); $this->writeUnsupportedElement($objWriter, 'page break');
} elseif ($element instanceof PHPWord_Section_Table) { } elseif ($element instanceof PHPWord_Section_Table) {
$this->_writeTable($objWriter, $element); $this->writeUnsupportedElement($objWriter, 'table');
} elseif ($element instanceof PHPWord_Section_ListItem) { } elseif ($element instanceof PHPWord_Section_ListItem) {
$this->_writeListItem($objWriter, $element); $this->writeUnsupportedElement($objWriter, 'list item');
} elseif ($element instanceof PHPWord_Section_Image || } elseif ($element instanceof PHPWord_Section_Image ||
$element instanceof PHPWord_Section_MemoryImage) { $element instanceof PHPWord_Section_MemoryImage) {
$this->_writeImage($objWriter, $element); $this->writeUnsupportedElement($objWriter, 'image');
} elseif ($element instanceof PHPWord_Section_Object) { } elseif ($element instanceof PHPWord_Section_Object) {
$this->_writeObject($objWriter, $element); $this->writeUnsupportedElement($objWriter, 'object');
} elseif ($element instanceof PHPWord_TOC) { } elseif ($element instanceof PHPWord_TOC) {
$this->_writeTOC($objWriter); $this->writeUnsupportedElement($objWriter, 'TOC');
*/
} else { } else {
print_r($element); $this->writeUnsupportedElement($objWriter, 'other');
echo '<br />';
} }
} }
@ -387,4 +384,17 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null) private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null)
{ {
} }
/**
* Write unsupported element
*
* @param PHPWord_Shared_XMLWriter $objWriter
* @param string $element
*/
private function writeUnsupportedElement($objWriter, $element)
{
$objWriter->startElement('text:p');
$objWriter->writeRaw("Cannot write content. This version of PHPWord has not supported {$element} element in ODText.");
$objWriter->endElement();
}
} }

View File

@ -314,28 +314,25 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$sRTFBody .= $this->getDataContentTextBreak(); $sRTFBody .= $this->getDataContentTextBreak();
} elseif ($element instanceof PHPWord_Section_TextRun) { } elseif ($element instanceof PHPWord_Section_TextRun) {
$sRTFBody .= $this->getDataContentTextRun($element); $sRTFBody .= $this->getDataContentTextRun($element);
/*
} elseif ($element instanceof PHPWord_Section_Link) { } elseif ($element instanceof PHPWord_Section_Link) {
$this->_writeLink($objWriter, $element); $sRTFBody .= $this->getDataContentUnsupportedElement('link');
} elseif ($element instanceof PHPWord_Section_Title) { } elseif ($element instanceof PHPWord_Section_Title) {
$this->_writeTitle($objWriter, $element); $sRTFBody .= $this->getDataContentUnsupportedElement('title');
} elseif ($element instanceof PHPWord_Section_PageBreak) { } elseif ($element instanceof PHPWord_Section_PageBreak) {
$this->_writePageBreak($objWriter); $sRTFBody .= $this->getDataContentUnsupportedElement('page break');
} elseif ($element instanceof PHPWord_Section_Table) { } elseif ($element instanceof PHPWord_Section_Table) {
$this->_writeTable($objWriter, $element); $sRTFBody .= $this->getDataContentUnsupportedElement('table');
} elseif ($element instanceof PHPWord_Section_ListItem) { } elseif ($element instanceof PHPWord_Section_ListItem) {
$this->_writeListItem($objWriter, $element); $sRTFBody .= $this->getDataContentUnsupportedElement('list item');
} elseif ($element instanceof PHPWord_Section_Image || } elseif ($element instanceof PHPWord_Section_Image ||
$element instanceof PHPWord_Section_MemoryImage) { $element instanceof PHPWord_Section_MemoryImage) {
$this->_writeImage($objWriter, $element); $sRTFBody .= $this->getDataContentUnsupportedElement('image');
} elseif ($element instanceof PHPWord_Section_Object) { } elseif ($element instanceof PHPWord_Section_Object) {
$this->_writeObject($objWriter, $element); $sRTFBody .= $this->getDataContentUnsupportedElement('object');
} elseif ($element instanceof PHPWord_TOC) { } elseif ($element instanceof PHPWord_TOC) {
$this->_writeTOC($objWriter); $sRTFBody .= $this->getDataContentUnsupportedElement('TOC');
*/
} else { } else {
print_r($element); $sRTFBody .= $this->getDataContentUnsupportedElement('other');
echo '<br />';
} }
} }
} }
@ -381,7 +378,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
$this->_lastParagraphStyle = ''; $this->_lastParagraphStyle = '';
} }
if ($styleFont) { if ($styleFont instanceof PHPWord_Style_Font) {
if ($styleFont->getColor() != null) { if ($styleFont->getColor() != null) {
$idxColor = array_search($styleFont->getColor(), $this->_colorTable); $idxColor = array_search($styleFont->getColor(), $this->_colorTable);
if ($idxColor !== false) { if ($idxColor !== false) {
@ -413,7 +410,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
} }
$sRTFText .= $text->getText(); $sRTFText .= $text->getText();
if ($styleFont) { if ($styleFont instanceof PHPWord_Style_Font) {
$sRTFText .= '\cf0'; $sRTFText .= '\cf0';
$sRTFText .= '\f0'; $sRTFText .= '\f0';
@ -461,4 +458,19 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
return '\par' . PHP_EOL; return '\par' . PHP_EOL;
} }
/**
* Write unsupported element
*
* @param string $element
*/
private function getDataContentUnsupportedElement($element)
{
$sRTFText = '';
$sRTFText .= '\pard\nowidctlpar' . PHP_EOL;
$sRTFText .= "Cannot write content. This version of PHPWord has not supported {$element} element in RTF.";
$sRTFText .= '\par' . PHP_EOL;
return $sRTFText;
}
} }

86
Classes/PHPWord/Writer/Word2007/Base.php Normal file → Executable file
View File

@ -29,9 +29,14 @@
/** /**
* Class PHPWord_Writer_Word2007_Base * Class PHPWord_Writer_Word2007_Base
*/ */
class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart
{
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) { /**
* Write text
*/
protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false)
{
$styleFont = $text->getFontStyle(); $styleFont = $text->getFontStyle();
$SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false; $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
@ -80,7 +85,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
} }
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) { /**
* Write text run
*/
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun)
{
$elements = $textrun->getElements(); $elements = $textrun->getElements();
$styleParagraph = $textrun->getParagraphStyle(); $styleParagraph = $textrun->getParagraphStyle();
@ -221,7 +230,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
} }
protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false) { /**
* Write link
*/
protected function _writeLink(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Link $link, $withoutP = false)
{
$rID = $link->getRelationId(); $rID = $link->getRelationId();
$linkName = $link->getLinkName(); $linkName = $link->getLinkName();
if (is_null($linkName)) { if (is_null($linkName)) {
@ -276,7 +289,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
} }
protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun) { /**
* Write preserve text
*/
protected function _writePreserveText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footer_PreserveText $textrun)
{
$styleFont = $textrun->getFontStyle(); $styleFont = $textrun->getFontStyle();
$styleParagraph = $textrun->getParagraphStyle(); $styleParagraph = $textrun->getParagraphStyle();
@ -367,7 +384,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); // p $objWriter->endElement(); // p
} }
protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style) { /**
* Write text style
*/
protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Font $style)
{
$font = $style->getName(); $font = $style->getName();
$bold = $style->getBold(); $bold = $style->getBold();
$italic = $style->getItalic(); $italic = $style->getItalic();
@ -454,11 +475,19 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); $objWriter->endElement();
} }
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null) { /**
* Write text break
*/
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
{
$objWriter->writeElement('w:p', null); $objWriter->writeElement('w:p', null);
} }
protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) { /**
* Write table
*/
protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table)
{
$_rows = $table->getRows(); $_rows = $table->getRows();
$_cRows = count($_rows); $_cRows = count($_rows);
@ -500,14 +529,14 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->writeAttribute('w:val', $height); $objWriter->writeAttribute('w:val', $height);
$objWriter->endElement(); $objWriter->endElement();
} }
if (!is_null($tblHeader)) { if ($tblHeader) {
$objWriter->startElement('w:tblHeader'); $objWriter->startElement('w:tblHeader');
$objWriter->writeAttribute('w:val', $tblHeader); $objWriter->writeAttribute('w:val', '1');
$objWriter->endElement(); $objWriter->endElement();
} }
if (!is_null($cantSplit)) { if ($cantSplit) {
$objWriter->startElement('w:cantSplit'); $objWriter->startElement('w:cantSplit');
$objWriter->writeAttribute('w:val', $cantSplit); $objWriter->writeAttribute('w:val', '1');
$objWriter->endElement(); $objWriter->endElement();
} }
$objWriter->endElement(); $objWriter->endElement();
@ -566,7 +595,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
} }
protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null) { /**
* Write table style
*/
protected function _writeTableStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Table $style = null)
{
$margins = $style->getCellMargin(); $margins = $style->getCellMargin();
$mTop = (!is_null($margins[0])) ? true : false; $mTop = (!is_null($margins[0])) ? true : false;
$mLeft = (!is_null($margins[1])) ? true : false; $mLeft = (!is_null($margins[1])) ? true : false;
@ -610,7 +643,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
} }
protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null) { /**
* Write cell style
*/
protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Cell $style = null)
{
$bgColor = $style->getBgColor(); $bgColor = $style->getBgColor();
$valign = $style->getVAlign(); $valign = $style->getVAlign();
$textDir = $style->getTextDirection(); $textDir = $style->getTextDirection();
@ -716,7 +753,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
* @param \PHPWord_Shared_XMLWriter $objWriter * @param \PHPWord_Shared_XMLWriter $objWriter
* @param \PHPWord_Section_Image $image * @param \PHPWord_Section_Image $image
*/ */
protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) { protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false)
{
$rId = $image->getRelationId(); $rId = $image->getRelationId();
$style = $image->getStyle(); $style = $image->getStyle();
@ -792,7 +830,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
} }
} }
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) { /**
* Write watermark
*/
protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image)
{
$rId = $image->getRelationId(); $rId = $image->getRelationId();
$style = $image->getStyle(); $style = $image->getStyle();
@ -835,7 +877,11 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); $objWriter->endElement();
} }
protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title) { /**
* Write title
*/
protected function _writeTitle(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Title $title)
{
$text = htmlspecialchars($title->getText()); $text = htmlspecialchars($title->getText());
$text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text); $text = PHPWord_Shared_String::ControlCharacterPHP2OOXML($text);
$anchor = $title->getAnchor(); $anchor = $title->getAnchor();
@ -876,6 +922,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); $objWriter->endElement();
} }
/**
* Write footnote
*/
protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote) protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote)
{ {
$objWriter->startElement('w:footnote'); $objWriter->startElement('w:footnote');
@ -911,6 +960,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
$objWriter->endElement(); // w:footnote $objWriter->endElement(); // w:footnote
} }
/**
* Write footnote reference
*/
protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false) protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false)
{ {
if (!$withoutP) { if (!$withoutP) {

View File

@ -15,7 +15,8 @@ __Want to contribute?__ Fork us!
## Requirements ## Requirements
* PHP version 5.3.0 or higher * PHP version 5.3.0 or higher
* PHP extension [php_zip](http://php.net/manual/en/book.zip.php) enabled * PHP extension [ZipArchive](http://php.net/manual/en/book.zip.php)
* PHP extension [XMLWriter](http://php.net/manual/en/book.xmlwriter.php)
## Installation ## Installation

View File

@ -3,35 +3,131 @@ namespace PHPWord\Tests;
use PHPWord_Section; use PHPWord_Section;
/**
* Class TOCTest
*
* @package PHPWord\Tests
* @covers PHPWord_Section
* @runTestsInSeparateProcesses
*/
class SectionTest extends \PHPUnit_Framework_TestCase class SectionTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @covers PHPWord_Section::getSettings
*/
public function testGetSettings() public function testGetSettings()
{ {
$oSection = new PHPWord_Section(0); $oSection = new PHPWord_Section(0);
$this->assertAttributeEquals($oSection->getSettings(), '_settings', new PHPWord_Section(0)); $this->assertAttributeEquals($oSection->getSettings(), '_settings', new PHPWord_Section(0));
} }
public function testGetElementss() /**
* @covers PHPWord_Section::getElements
*/
public function testGetElements()
{ {
$oSection = new PHPWord_Section(0); $oSection = new PHPWord_Section(0);
$this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new PHPWord_Section(0)); $this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new PHPWord_Section(0));
} }
/**
* @covers PHPWord_Section::getFooter
*/
public function testGetFooter() public function testGetFooter()
{ {
$oSection = new PHPWord_Section(0); $oSection = new PHPWord_Section(0);
$this->assertAttributeEquals($oSection->getFooter(), '_footer', new PHPWord_Section(0)); $this->assertAttributeEquals($oSection->getFooter(), '_footer', new PHPWord_Section(0));
} }
/**
* @covers PHPWord_Section::getHeaders
*/
public function testGetHeaders() public function testGetHeaders()
{ {
$oSection = new PHPWord_Section(0); $oSection = new PHPWord_Section(0);
$this->assertAttributeEquals($oSection->getHeaders(), '_headers', new PHPWord_Section(0)); $this->assertAttributeEquals($oSection->getHeaders(), '_headers', new PHPWord_Section(0));
} }
public function testGetElements() /**
* @covers PHPWord_Section::setSettings
*/
public function testSetSettings()
{ {
$oSection = new PHPWord_Section(0); $expected = 'landscape';
$this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new PHPWord_Section(0)); $object = new PHPWord_Section(0);
$object->setSettings(array('orientation' => $expected));
$this->assertEquals($expected, $object->getSettings()->getOrientation());
}
/**
* @covers PHPWord_Section::addText
* @covers PHPWord_Section::addLink
* @covers PHPWord_Section::addTextBreak
* @covers PHPWord_Section::addPageBreak
* @covers PHPWord_Section::addTable
* @covers PHPWord_Section::addListItem
* @covers PHPWord_Section::addObject
* @covers PHPWord_Section::addImage
* @covers PHPWord_Section::addMemoryImage
* @covers PHPWord_Section::addTOC
* @covers PHPWord_Section::addTitle
* @covers PHPWord_Section::createTextRun
* @covers PHPWord_Section::createFootnote
*/
public function testAddElements()
{
$objectSource = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
);
$imageSource = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
);
$imageUrl = 'http://php.net//images/logos/php-med-trans-light.gif';
$section = new PHPWord_Section(0);
$section->addText(utf8_decode('ä'));
$section->addLink(utf8_decode('http://äää.com'), utf8_decode('ä'));
$section->addTextBreak();
$section->addPageBreak();
$section->addTable();
$section->addListItem(utf8_decode('ä'));
$section->addObject($objectSource);
$section->addImage($imageSource);
$section->addMemoryImage($imageUrl);
$section->addTOC();
$section->addTitle(utf8_decode('ä'), 1);
$section->createTextRun();
$section->createFootnote();
$elementCollection = $section->getElements();
$elementType = 'Link';
$objectType = "PHPWord_Section_{$elementType}";
$this->assertInstanceOf($objectType, $elementCollection[1]);
// $elementTypes = array('Text', 'Link', 'TextBreak', 'PageBreak',
// 'Table', 'ListItem', 'Object', 'Image', 'MemoryImage', 'TOC',
// 'Title', 'TextRun');
// $i = 0;
// foreach ($elementTypes as $elementType) {
// $objectType = "PHPWord_Section_{$elementType}";
// $this->assertInstanceOf($objectType, $elementCollection[$i]);
// $i++;
// }
}
/**
* @covers PHPWord_Section::createHeader
* @covers PHPWord_Section::createFooter
*/
public function testCreateHeaderFooter()
{
$object = new PHPWord_Section(0);
$elements = array('Header', 'Footer');
foreach ($elements as $element) {
$objectType = "PHPWord_Section_{$element}";
$method = "create{$element}";
$this->assertInstanceOf($objectType, $object->$method());
}
} }
} }

View File

@ -7,6 +7,7 @@ use PHPWord_Style_TOC;
* Class TOCTest * Class TOCTest
* *
* @package PHPWord\Tests * @package PHPWord\Tests
* @covers PHPWord_Style_TOC
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class TOCTest extends \PHPUnit_Framework_TestCase class TOCTest extends \PHPUnit_Framework_TestCase

View File

@ -0,0 +1,45 @@
<?php
namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Style;
/**
* Class StyleTest
*
* @package PHPWord\Tests
* @covers PHPWord_Style
* @runTestsInSeparateProcesses
*/
class StyleTest extends PHPUnit_Framework_TestCase
{
/**
* @covers PHPWord_Style::addParagraphStyle
* @covers PHPWord_Style::addFontStyle
* @covers PHPWord_Style::addLinkStyle
* @covers PHPWord_Style::addTitleStyle
*/
public function testStyles()
{
$paragraph = array('align' => 'center');
$font = array('italic' => true);
$table = array('bgColor' => 'CCCCCC');
$styles = array('Paragraph' => 'Paragraph', 'Font' => 'Font',
'Link' => 'Font', 'Table' => 'TableFull',
'Heading_1' => 'Font', 'Normal' => 'Paragraph');
$elementCount = 6;
PHPWord_Style::addParagraphStyle('Paragraph', $paragraph);
PHPWord_Style::addFontStyle('Font', $font);
PHPWord_Style::addLinkStyle('Link', $font);
PHPWord_Style::addTableStyle('Table', $table);
PHPWord_Style::addTitleStyle(1, $font);
PHPWord_Style::setDefaultParagraphStyle($paragraph);
$this->assertEquals($elementCount, count(PHPWord_Style::getStyles()));
foreach ($styles as $name => $style) {
$expected = "PHPWord_Style_{$style}";
$this->assertInstanceOf($expected, PHPWord_Style::getStyle($name));
}
$this->assertNull(PHPWord_Style::getStyle('Unknown'));
}
}

View File

@ -5,7 +5,11 @@ use PHPWord_TOC;
use PHPWord_Style_TOC; use PHPWord_Style_TOC;
/** /**
* Class TOCTest
*
* @package PHPWord\Tests
* @covers PHPWord_TOC * @covers PHPWord_TOC
* @runTestsInSeparateProcesses
*/ */
class TOCTest extends \PHPUnit_Framework_TestCase class TOCTest extends \PHPUnit_Framework_TestCase
{ {
@ -45,7 +49,7 @@ class TOCTest extends \PHPUnit_Framework_TestCase
// Prepare variables // Prepare variables
$titleCount = 3; $titleCount = 3;
$anchor = '_Toc' . (252634154 + $titleCount); $anchor = '_Toc' . (252634154 + $titleCount);
$bookmark = $titleCount - 1; // zero based $bookmark = $titleCount - 1;
$titles = array( $titles = array(
'Heading 1' => 1, 'Heading 1' => 1,
'Heading 2' => 2, 'Heading 2' => 2,

View File

@ -6,7 +6,9 @@ use PHPWord\Tests\TestHelperDOCX;
/** /**
* Class BaseTest * Class BaseTest
*
* @package PHPWord\Tests * @package PHPWord\Tests
* @coversDefaultClass PHPWord_Writer_Word2007_Base
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class BaseTest extends \PHPUnit_Framework_TestCase class BaseTest extends \PHPUnit_Framework_TestCase
@ -19,6 +21,89 @@ class BaseTest extends \PHPUnit_Framework_TestCase
TestHelperDOCX::clear(); TestHelperDOCX::clear();
} }
public function testWriteText()
{
$rStyle = 'rStyle';
$pStyle = 'pStyle';
$PHPWord = new PHPWord();
$PHPWord->addFontStyle($rStyle, array('bold' => true));
$PHPWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
$section = $PHPWord->createSection();
$section->addText('Test', $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = "/w:document/w:body/w:p/w:r/w:rPr/w:rStyle";
$this->assertEquals($rStyle, $doc->getElementAttribute($element, 'w:val'));
$element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
$this->assertEquals($pStyle, $doc->getElementAttribute($element, 'w:val'));
}
/**
* Write text run
*/
public function testWriteTextRun()
{
$pStyle = 'pStyle';
$aStyle = array('align' => 'justify', 'spaceBefore' => 120, 'spaceAfter' => 120);
$imageSrc = join(
DIRECTORY_SEPARATOR,
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'earth.jpg')
);
$PHPWord = new PHPWord();
$PHPWord->addParagraphStyle($pStyle, $aStyle);
$section = $PHPWord->createSection('Test');
$textrun = $section->createTextRun($pStyle);
$textrun->addText('Test');
$textrun->addTextBreak();
$textrun = $section->createTextRun($aStyle);
$textrun->addLink('http://test.com');
$textrun->addImage($imageSrc);
$doc = TestHelperDOCX::getDocument($PHPWord);
$parent = "/w:document/w:body/w:p";
$this->assertTrue($doc->elementExists("{$parent}/w:pPr/w:pStyle[@w:val='{$pStyle}']"));
}
/**
* Write link
*/
public function testWriteLink()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$expected = 'PHPWord';
$section->addLink('http://github.com/phpoffice/phpword', $expected);
$doc = TestHelperDOCX::getDocument($PHPWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t');
$this->assertEquals($expected, $element->nodeValue);
}
/**
* Write preserve text
*/
public function testWritePreserveText()
{
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$footer = $section->createFooter();
$footer->addPreserveText('{PAGE}');
$doc = TestHelperDOCX::getDocument($PHPWord);
$preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml');
$this->assertEquals('PAGE', $preserve->nodeValue);
$this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
}
/**
* Write paragraph style: Alignment
*/
public function testWriteParagraphStyleAlign() public function testWriteParagraphStyleAlign()
{ {
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
@ -33,7 +118,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* Test write paragraph pagination * Write paragraph style: Pagination
*/ */
public function testWriteParagraphStylePagination() public function testWriteParagraphStylePagination()
{ {
@ -62,6 +147,99 @@ class BaseTest extends \PHPUnit_Framework_TestCase
} }
} }
/**
* covers ::_writeTextStyle
*/
public function testWriteFontStyle()
{
$PHPWord = new PHPWord();
$styles['name'] = 'Verdana';
$styles['size'] = 14;
$styles['bold'] = true;
$styles['italic'] = true;
$styles['underline'] = 'dash';
$styles['strikethrough'] = true;
$styles['superScript'] = true;
$styles['color'] = 'FF0000';
$styles['fgColor'] = 'yellow';
$section = $PHPWord->createSection();
$section->addText('Test', $styles);
$doc = TestHelperDOCX::getDocument($PHPWord);
$parent = '/w:document/w:body/w:p/w:r/w:rPr';
$this->assertEquals($styles['name'], $doc->getElementAttribute("{$parent}/w:rFonts", 'w:ascii'));
$this->assertEquals($styles['size'] * 2, $doc->getElementAttribute("{$parent}/w:sz", 'w:val'));
$this->assertTrue($doc->elementExists("{$parent}/w:b"));
$this->assertTrue($doc->elementExists("{$parent}/w:i"));
$this->assertEquals($styles['underline'], $doc->getElementAttribute("{$parent}/w:u", 'w:val'));
$this->assertTrue($doc->elementExists("{$parent}/w:strike"));
$this->assertEquals('superscript', $doc->getElementAttribute("{$parent}/w:vertAlign", 'w:val'));
$this->assertEquals($styles['color'], $doc->getElementAttribute("{$parent}/w:color", 'w:val'));
$this->assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val'));
}
/**
* Write table
*/
public function testWriteTableStyle()
{
$PHPWord = new PHPWord();
$tWidth = 120;
$rHeight = 120;
$cWidth = 120;
$tStyles["cellMarginTop"] = 120;
$tStyles["cellMarginRight"] = 120;
$tStyles["cellMarginBottom"] = 120;
$tStyles["cellMarginLeft"] = 120;
$rStyles["tblHeader"] = true;
$rStyles["cantSplit"] = true;
$cStyles["valign"] = 'top';
$cStyles["textDirection"] = 'btLr';
$cStyles["bgColor"] = 'FF0000';
$cStyles["borderTopSize"] = 120;
$cStyles["borderBottomSize"] = 120;
$cStyles["borderLeftSize"] = 120;
$cStyles["borderRightSize"] = 120;
$cStyles["borderTopColor"] = 'FF0000';
$cStyles["borderBottomColor"] = 'FF0000';
$cStyles["borderLeftColor"] = 'FF0000';
$cStyles["borderRightColor"] = 'FF0000';
$section = $PHPWord->createSection();
$table = $section->addTable($tStyles);
$table->setWidth = 100;
$table->addRow($rHeight, $rStyles);
$cell = $table->addCell($cWidth, $cStyles);
$cell->addText('Test');
$cell->addTextBreak();
$cell->addLink('http://google.com');
$cell->addListItem('Test');
$textrun = $cell->createTextRun();
$textrun->addText('Test');
$doc = TestHelperDOCX::getDocument($PHPWord);
$parent = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellMar';
$this->assertEquals($tStyles['cellMarginTop'], $doc->getElementAttribute("{$parent}/w:top", 'w:w'));
$this->assertEquals($tStyles['cellMarginRight'], $doc->getElementAttribute("{$parent}/w:right", 'w:w'));
$this->assertEquals($tStyles['cellMarginBottom'], $doc->getElementAttribute("{$parent}/w:bottom", 'w:w'));
$this->assertEquals($tStyles['cellMarginLeft'], $doc->getElementAttribute("{$parent}/w:right", 'w:w'));
$parent = '/w:document/w:body/w:tbl/w:tr/w:trPr';
$this->assertEquals($rHeight, $doc->getElementAttribute("{$parent}/w:trHeight", 'w:val'));
$this->assertEquals($rStyles['tblHeader'], $doc->getElementAttribute("{$parent}/w:tblHeader", 'w:val'));
$this->assertEquals($rStyles['cantSplit'], $doc->getElementAttribute("{$parent}/w:cantSplit", 'w:val'));
$parent = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr';
$this->assertEquals($cWidth, $doc->getElementAttribute("{$parent}/w:tcW", 'w:w'));
$this->assertEquals($cStyles['valign'], $doc->getElementAttribute("{$parent}/w:vAlign", 'w:val'));
$this->assertEquals($cStyles['textDirection'], $doc->getElementAttribute("{$parent}/w:textDirection", 'w:val'));
}
/**
* Write cell style
*/
public function testWriteCellStyleCellGridSpan() public function testWriteCellStyleCellGridSpan()
{ {
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
@ -86,6 +264,9 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(5, $element->getAttribute('w:val')); $this->assertEquals(5, $element->getAttribute('w:val'));
} }
/**
* Write image
*/
public function testWriteImagePosition() public function testWriteImagePosition()
{ {
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
@ -108,18 +289,19 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertRegExp('/position:absolute;/', $style); $this->assertRegExp('/position:absolute;/', $style);
} }
public function testWritePreserveText() /**
* Write title
*/
public function testWriteTitle()
{ {
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
$section = $PHPWord->createSection(); $PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
$footer = $section->createFooter(); $PHPWord->createSection()->addTitle('Test', 1);
$footer->addPreserveText('{PAGE}');
$doc = TestHelperDOCX::getDocument($PHPWord); $doc = TestHelperDOCX::getDocument($PHPWord);
$preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml');
$this->assertEquals('PAGE', $preserve->nodeValue); $element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
$this->assertEquals('preserve', $preserve->getAttribute('xml:space')); $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
$element = "/w:document/w:body/w:p/w:r/w:fldChar";
$this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
} }
} }

View File

@ -65,23 +65,28 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
*/ */
public function testCheckContentTypes() public function testCheckContentTypes()
{ {
$images = array(
'mars_noext_jpg' => '1.jpg',
'mars.jpg' => '2.jpg',
'mario.gif' => '3.gif',
'firefox.png' => '4.png',
'duke_nukem.bmp' => '5.bmp',
'angela_merkel.tif' => '6.tif',
);
$phpWord = new PHPWord(); $phpWord = new PHPWord();
$section = $phpWord->createSection(); $section = $phpWord->createSection();
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); foreach ($images as $source => $target) {
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/{$source}");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); }
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp");
$section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif");
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);
$mediaPath = $doc->getPath() . "/word/media"; $mediaPath = $doc->getPath() . "/word/media";
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg", $mediaPath . "/section_image1.jpg"); foreach ($images as $source => $target) {
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg", $mediaPath . "/section_image2.jpg"); $this->assertFileEquals(
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif", $mediaPath . "/section_image3.gif"); PHPWORD_TESTS_DIR_ROOT . "/_files/images/{$source}",
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png", $mediaPath . "/section_image4.png"); $mediaPath . "/section_image{$target}"
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp", $mediaPath . "/section_image5.bmp"); );
$this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif", $mediaPath . "/section_image6.tif"); }
} }
} }

View File

@ -7,7 +7,11 @@ use PHPWord_Section;
use PHPWord_Style; use PHPWord_Style;
/** /**
* Class PHPWordTest
*
* @package PHPWord\Tests
* @covers PHPWord * @covers PHPWord
* @runTestsInSeparateProcesses
*/ */
class PHPWordTest extends \PHPUnit_Framework_TestCase class PHPWordTest extends \PHPUnit_Framework_TestCase
{ {

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -47,9 +47,9 @@ class XmlDocument
/** /**
* @param string $path * @param string $path
* @param string $file * @param string $file
* @return \DOMElement * @return \DOMNodeList
*/ */
public function getElement($path, $file = 'word/document.xml') public function getNodeList($path, $file = 'word/document.xml')
{ {
if ($this->dom === null || $file !== $this->file) { if ($this->dom === null || $file !== $this->file) {
$this->getFileDom($file); $this->getFileDom($file);
@ -60,7 +60,18 @@ class XmlDocument
} }
$elements = $this->xpath->query($path); return $this->xpath->query($path);
}
/**
* @param string $path
* @param string $file
* @return \DOMElement
*/
public function getElement($path, $file = 'word/document.xml')
{
$elements = $this->getNodeList($path, $file);
return $elements->item(0); return $elements->item(0);
} }
@ -79,4 +90,26 @@ class XmlDocument
{ {
return $this->path; return $this->path;
} }
/**
* @param string $path
* @param string $attribute
* @param string $file
* @return string
*/
public function getElementAttribute($path, $attribute, $file = 'word/document.xml')
{
return $this->getElement($path, $file)->getAttribute($attribute);
}
/**
* @param string $path
* @param string $file
* @return string
*/
public function elementExists($path, $file = 'word/document.xml')
{
$nodeList = $this->getNodeList($path, $file);
return !($nodeList->length == 0);
}
} }

View File

@ -1,50 +1,62 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
echo date('H:i:s') , " Create new PHPWord object" , EOL; echo date('H:i:s') , " Create new PHPWord object" , EOL;
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
$PHPWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16));
$PHPWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
$PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
// New portrait section // New portrait section
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
// Add text elements // Simple text
$section->addTitle('Welcome to PHPWord', 1);
$section->addText('Hello World!'); $section->addText('Hello World!');
// Two text break
$section->addTextBreak(2); $section->addTextBreak(2);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699')); // Defined style
$section->addTextBreak(2); $section->addText('I am styled by a font style definition.', 'rStyle');
$section->addText('I am styled by a paragraph style definition.', null, 'pStyle');
$section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle');
$section->addTextBreak();
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16)); // Inline font style
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100)); $fontStyle['name'] = 'Times New Roman';
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle'); $fontStyle['size'] = 20;
$section->addText('I have only a paragraph style definition.', null, 'pStyle'); $fontStyle['bold'] = true;
$fontStyle['italic'] = true;
$fontStyle['underline'] = 'dash';
$fontStyle['strikethrough'] = true;
$fontStyle['superScript'] = true;
$fontStyle['color'] = 'FF0000';
$fontStyle['fgColor'] = 'yellow';
$section->addText('I am inline styled.', $fontStyle);
$section->addTextBreak();
// Save File // Link
echo date('H:i:s') , " Write to Word2007 format" , EOL; $section->addLink('http://www.google.com', null, 'NLink');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $section->addTextBreak();
$objWriter->save(str_replace('.php', '.docx', __FILE__));
echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; // Image
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); $section->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
$objWriter->save(str_replace('.php', '.odt', __FILE__));
echo date('H:i:s') , " Write to RTF format" , EOL; // Save file
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); $name = basename(__FILE__, '.php');
$objWriter->save(str_replace('.php', '.rtf', __FILE__)); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
// Done
// Echo memory peak usage echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , EOL;

View File

@ -1,14 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -42,22 +35,16 @@ $section->addText("Multiple Tabs:\tOne\tTwo\tThree", NULL, 'multipleTab');
$section->addText("Left Aligned\tRight Aligned", NULL, 'rightTab'); $section->addText("Left Aligned\tRight Aligned", NULL, 'rightTab');
$section->addText("\tCenter Aligned", NULL, 'centerTab'); $section->addText("\tCenter Aligned", NULL, 'centerTab');
// Save File // Save file
echo date('H:i:s') , ' Write to Word2007 format' , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; // Done
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); echo date('H:i:s'), " Done writing file(s)", EOL;
$objWriter->save(str_replace('.php', '.odt', __FILE__)); echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;

View File

@ -1,13 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -34,22 +28,16 @@ $section->addText('This section and we play with header/footer height.');
$section->createHeader()->addText('Header'); $section->createHeader()->addText('Header');
$section->createFooter()->addText('Footer'); $section->createFooter()->addText('Footer');
// Save File // Save file
echo date('H:i:s') , ' Write to Word2007 format' , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; // Done
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); echo date('H:i:s'), " Done writing file(s)", EOL;
$objWriter->save(str_replace('.php', '.odt', __FILE__)); echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;

View File

@ -1,14 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -42,21 +35,16 @@ $textrun->addText(' Sample Image: ');
$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18)); $textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18));
$textrun->addText(' Here is some more text. '); $textrun->addText(' Here is some more text. ');
// Save File // Save file
echo date('H:i:s') , ' Write to Word2007 format' , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; // Done
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); echo date('H:i:s'), " Done writing file(s)", EOL;
$objWriter->save(str_replace('.php', '.odt', __FILE__)); echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;

View File

@ -1,14 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -45,22 +38,16 @@ $section->addText('Three columns, half inch (720 twips) spacing. ' . $filler);
$section = $PHPWord->createSection(array('breakType' => 'continuous')); $section = $PHPWord->createSection(array('breakType' => 'continuous'));
$section->addText('Normal paragraph again.'); $section->addText('Normal paragraph again.');
// Save File // Save file
echo date('H:i:s') , " Write to Word2007 format" , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; // Done
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); echo date('H:i:s'), " Done writing file(s)", EOL;
$objWriter->save(str_replace('.php', '.odt', __FILE__));
echo date('H:i:s') , " Write to RTF format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , EOL;

View File

@ -1,12 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -41,14 +36,16 @@ $section->addText('You can also create the footnote directly from the section ma
$footnote = $section->createFootnote(); $footnote = $section->createFootnote();
$footnote->addText('The reference for this is wrapped in its own line'); $footnote->addText('The reference for this is wrapped in its own line');
// Save File // Save file
echo date('H:i:s') , " Write to Word2007 format" , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
// Done
// Echo memory peak usage echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , EOL;

View File

@ -1,9 +1,14 @@
<?php <?php
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word document
echo date('H:i:s') , " Create new PHPWord object" , EOL;
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Sample_07_TemplateCloneRow.docx'); $document = $PHPWord->loadTemplate('resources/Sample_07_TemplateCloneRow.docx');
// Simple table // Simple table
$document->cloneRow('rowValue', 10); $document->cloneRow('rowValue', 10);
@ -51,4 +56,11 @@ $document->setValue('userFirstName#3', 'Michael');
$document->setValue('userName#3', 'Ray'); $document->setValue('userName#3', 'Ray');
$document->setValue('userPhone#3', '+1 428 889 775'); $document->setValue('userPhone#3', '+1 428 889 775');
$document->saveAs('Sample_07_TemplateCloneRow_result.docx'); $name = 'Sample_07_TemplateCloneRow_result.docx';
echo date('H:i:s'), " Write to Word2007 format", EOL;
$document->saveAs($name);
rename($name, "results/{$name}");
// Done
echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;

View File

@ -1,14 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word document // New Word document
@ -55,22 +48,16 @@ $section->addText('Paragraph with pageBreakBefore = true (default: false). ' .
'heading styles.', 'heading styles.',
null, array('pageBreakBefore' => true)); null, array('pageBreakBefore' => true));
// Save File // Save file
echo date('H:i:s') , " Write to Word2007 format" , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; // Done
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); echo date('H:i:s'), " Done writing file(s)", EOL;
$objWriter->save(str_replace('.php', '.odt', __FILE__));
echo date('H:i:s') , " Write to RTF format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL; echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , EOL;

View File

@ -1,13 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -82,21 +76,16 @@ $table->addCell(2000, $cellVCentered)->addText('C', null, $cellHCentered);
$table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered); $table->addCell(2000, $cellVCentered)->addText('D', null, $cellHCentered);
$table->addCell(null, $cellRowContinue); $table->addCell(null, $cellRowContinue);
// Save File // Save file
echo date('H:i:s') , ' Write to Word2007 format' , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
// echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; // Done
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); echo date('H:i:s'), " Done writing file(s)", EOL;
// $objWriter->save(str_replace('.php', '.odt', __FILE__)); echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
//
// echo date('H:i:s') , ' Write to RTF format' , EOL;
// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
// $objWriter->save(str_replace('.php', '.rtf', __FILE__));
// Echo memory peak usage
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;

View File

@ -1,13 +1,7 @@
<?php <?php
// Init
error_reporting(E_ALL); error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}
require_once '../Classes/PHPWord.php'; require_once '../Classes/PHPWord.php';
// New Word Document // New Word Document
@ -18,13 +12,16 @@ $header = array('size' => 16, 'bold' => true);
//1.Use EastAisa FontStyle //1.Use EastAisa FontStyle
$section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232')); $section->addText('中文楷体样式测试',array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
// Save File // Save file
echo date('H:i:s') , ' Write to Word2007 format' , EOL; $name = basename(__FILE__, '.php');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
$objWriter->save(str_replace('.php', '.docx', __FILE__)); foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
// Echo memory peak usage // Done
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s') , ' Done writing file' , EOL;

View File

@ -1,33 +0,0 @@
<?php
// error_reporting(E_ALL );
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php';
// Read contents
$sample = 'Sample_10_ReadWord2007';
$source = "resources/{$sample}.docx";
$target = "results/{$sample}";
echo '<p><strong>', date('H:i:s'), " Reading contents from `{$source}`</strong></p>";
$PHPWord = PHPWord_IOFactory::load($source);
// Rewrite contents
echo date('H:i:s') , " Write to Word2007 format" , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save("{$sample}.docx");
rename("{$sample}.docx", "{$target}.docx");
echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
$objWriter->save("{$sample}.odt");
rename("{$sample}.odt", "{$target}.odt");
echo date('H:i:s') , ' Write to RTF format' , EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save("{$sample}.rtf");
rename("{$sample}.rtf", "{$target}.rtf");
// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
echo date('H:i:s') , " Done writing file" , EOL;

View File

@ -0,0 +1,24 @@
<?php
// Init
error_reporting(E_ALL);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once '../Classes/PHPWord.php';
// Read contents
$name = basename(__FILE__, '.php');
$source = "resources/{$name}.docx";
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$PHPWord = PHPWord_IOFactory::load($source);
// (Re)write contents
$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf');
foreach ($writers as $writer => $extension) {
echo date('H:i:s'), " Write to {$writer} format", EOL;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, $writer);
$objWriter->save("{$name}.{$extension}");
rename("{$name}.{$extension}", "results/{$name}.{$extension}");
}
// Done
echo date('H:i:s'), " Done writing file(s)", EOL;
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;