From 891798bc160948c6c08fcab3a0990b980a485447 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Tue, 31 Dec 2013 14:34:50 +0700 Subject: [PATCH 01/18] Unix format --- Classes/PHPWord/_staticDocParts/settings.xml | 2 +- README.md | 184 +++++++++---------- 2 files changed, 93 insertions(+), 93 deletions(-) diff --git a/Classes/PHPWord/_staticDocParts/settings.xml b/Classes/PHPWord/_staticDocParts/settings.xml index 5eb22891..56ca9860 100644 --- a/Classes/PHPWord/_staticDocParts/settings.xml +++ b/Classes/PHPWord/_staticDocParts/settings.xml @@ -1,4 +1,4 @@ - + diff --git a/README.md b/README.md index 21c15959..d1d73199 100755 --- a/README.md +++ b/README.md @@ -1,92 +1,92 @@ -# PHPWord - OpenXML - Read, Write and Create Word documents in PHP - -PHPWord is a library written in PHP that create word documents. -No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be -opened by all major office software. - -## Want to contribute? -Fork us! - -## Requirements - -* PHP version 5.3.0 or higher - -## License -PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md) - -## Installation - -It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add -the following lines to your ``composer.json``. - -```json -{ - "require": { - "phpoffice/phpword": "dev-master" - } -} -``` - -## Usage - -The following is a basic example of the PHPWord library. - -```php -$PHPWord = new PHPWord(); - -// Every element you want to append to the word document is placed in a section. So you need a section: -$section = $PHPWord->createSection(); - -// After creating a section, you can append elements: -$section->addText('Hello world!'); - -// You can directly style your text by giving the addText function an array: -$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); - -// If you often need the same style again you can create a user defined style to the word document -// and give the addText function the name of the style: -$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); -$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); - -// You can also putthe appended element to local object an call functions like this: -$myTextElement = $section->addText('Hello World!'); -$myTextElement->setBold(); -$myTextElement->setName('Verdana'); -$myTextElement->setSize(22); - -// At least write the document to webspace: -$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); -$objWriter->save('helloWorld.docx'); -``` - -## Images - -You can add images easily using the following example. - -```php -$section = $PHPWord->createSection(); -$section->addImage('mars.jpg'); -``` - -Images settings include: - * ``width`` width in pixels - * ``height`` height in pixels - * ``align`` image alignment, __left__, __right__ or __center__ - * ``marginTop`` top margin in inches, can be negative - * ``marginLeft`` left margin in inches, can be negative - * ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__ - - To add an image with settings, consider the following example. - - ```php -$section->addImage( - 'mars.jpg', - array( - 'width' => 100, - 'height' => 100, - 'marginTop' => -1, - 'marginLeft' => -1, - wrappingStyle => 'behind' - ) -); - ``` +# PHPWord - OpenXML - Read, Write and Create Word documents in PHP + +PHPWord is a library written in PHP that create word documents. +No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be +opened by all major office software. + +## Want to contribute? +Fork us! + +## Requirements + +* PHP version 5.3.0 or higher + +## License +PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md) + +## Installation + +It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add +the following lines to your ``composer.json``. + +```json +{ + "require": { + "phpoffice/phpword": "dev-master" + } +} +``` + +## Usage + +The following is a basic example of the PHPWord library. + +```php +$PHPWord = new PHPWord(); + +// Every element you want to append to the word document is placed in a section. So you need a section: +$section = $PHPWord->createSection(); + +// After creating a section, you can append elements: +$section->addText('Hello world!'); + +// You can directly style your text by giving the addText function an array: +$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); + +// If you often need the same style again you can create a user defined style to the word document +// and give the addText function the name of the style: +$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); +$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); + +// You can also putthe appended element to local object an call functions like this: +$myTextElement = $section->addText('Hello World!'); +$myTextElement->setBold(); +$myTextElement->setName('Verdana'); +$myTextElement->setSize(22); + +// At least write the document to webspace: +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save('helloWorld.docx'); +``` + +## Images + +You can add images easily using the following example. + +```php +$section = $PHPWord->createSection(); +$section->addImage('mars.jpg'); +``` + +Images settings include: + * ``width`` width in pixels + * ``height`` height in pixels + * ``align`` image alignment, __left__, __right__ or __center__ + * ``marginTop`` top margin in inches, can be negative + * ``marginLeft`` left margin in inches, can be negative + * ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__ + + To add an image with settings, consider the following example. + + ```php +$section->addImage( + 'mars.jpg', + array( + 'width' => 100, + 'height' => 100, + 'marginTop' => -1, + 'marginLeft' => -1, + wrappingStyle => 'behind' + ) +); + ``` From 3037fbf59e3b2a019dc3af4f81e2201aa4771219 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 1 Jan 2014 18:59:19 +0700 Subject: [PATCH 02/18] Read README.md --- Classes/PHPWord/Section/Settings.php | 20 ++++++++++++ Classes/PHPWord/Section/TextRun.php | 10 ++++++ Classes/PHPWord/Style/Paragraph.php | 34 +++++++++++++++++++- Classes/PHPWord/Writer/Word2007/Base.php | 21 ++++++++++-- Classes/PHPWord/Writer/Word2007/Document.php | 8 ++++- Classes/PHPWord/Writer/Word2007/Styles.php | 30 +++++++++++++++++ README.md | 10 +++++- 7 files changed, 128 insertions(+), 5 deletions(-) diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index e168821c..87596a3e 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -150,6 +150,9 @@ class PHPWord_Section_Settings */ private $_borderBottomColor; + private $_colsNum; + private $_colsSpace; + /** * Create new Section Settings */ @@ -170,6 +173,8 @@ class PHPWord_Section_Settings $this->_borderRightColor = null; $this->_borderBottomSize = null; $this->_borderBottomColor = null; + $this->_colsNum = 1; + $this->_colsSpace = 360; } /** @@ -542,4 +547,19 @@ class PHPWord_Section_Settings { return $this->_borderBottomColor; } + + public function getColsNum() { + return $this->_colsNum; + } + public function setColsNum($pValue = '') { + $this->_colsNum = $pValue; + return $this; + } + public function getColsSpace() { + return $this->_colsSpace; + } + public function setColsSpace($pValue = '') { + $this->_colsSpace = $pValue; + return $this; + } } diff --git a/Classes/PHPWord/Section/TextRun.php b/Classes/PHPWord/Section/TextRun.php index 2c7a2166..cfa5bc6c 100755 --- a/Classes/PHPWord/Section/TextRun.php +++ b/Classes/PHPWord/Section/TextRun.php @@ -109,6 +109,16 @@ class PHPWord_Section_TextRun return $link; } + /** + * Add a TextBreak Element + * + * @param int $count + */ + public function addTextBreak($count = 1) { + for($i=1; $i<=$count; $i++) { + $this->_elementCollection[] = new PHPWord_Section_TextBreak(); + } + } /** * Get TextRun content * diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index ccbbd616..3f367e5e 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -73,6 +73,12 @@ class PHPWord_Style_Paragraph */ private $_indent; + /** + * Hanging by how much + * + * @var int + */ + private $_hanging; /** * New Paragraph Style @@ -85,6 +91,7 @@ class PHPWord_Style_Paragraph $this->_spacing = null; $this->_tabs = null; $this->_indent = null; + $this->_hanging = null; } /** @@ -96,7 +103,10 @@ class PHPWord_Style_Paragraph public function setStyleValue($key, $value) { if ($key == '_indent') { - $value = (int)$value * 720; // 720 twips per indent + $value = $value * 1440 / 2.54; // Measured by cm + } + if ($key == '_hanging') { + $value = $value * 1440 / 2.54; // Measured by cm } if ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips @@ -221,6 +231,28 @@ class PHPWord_Style_Paragraph return $this; } + /** + * Get hanging + * + * @return int + */ + public function getHanging() + { + return $this->_hanging; + } + + /** + * Set hanging + * + * @param int $pValue + * @return PHPWord_Style_Paragraph + */ + public function setHanging($pValue = null) + { + $this->_hanging = $pValue; + return $this; + } + /** * Get tabs * diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 331625fc..c9e81ab8 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -106,6 +106,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $this->_writeText($objWriter, $element, true); } elseif ($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element, true); + } elseif($element instanceof PHPWord_Section_TextBreak) { + $objWriter->writeElement('w:br'); } } } @@ -120,6 +122,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $spaceAfter = $style->getSpaceAfter(); $spacing = $style->getSpacing(); $indent = $style->getIndent(); + $hanging = $style->getHanging(); $tabs = $style->getTabs(); if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) { @@ -133,10 +136,15 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } - if (!is_null($indent)) { + if (!is_null($indent) || !is_null($hanging)) { $objWriter->startElement('w:ind'); $objWriter->writeAttribute('w:firstLine', 0); - $objWriter->writeAttribute('w:left', $indent); + if (!is_null($indent)) { + $objWriter->writeAttribute('w:left', $indent); + } + if (!is_null($hanging)) { + $objWriter->writeAttribute('w:hanging', $hanging); + } $objWriter->endElement(); } @@ -320,6 +328,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $fgColor = $style->getFgColor(); $striketrough = $style->getStrikethrough(); $underline = $style->getUnderline(); + $superscript = $style->getSuperScript(); + $subscript = $style->getSubScript(); $objWriter->startElement('w:rPr'); @@ -379,6 +389,13 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } + // Superscript/subscript + if ($superscript || $subscript) { + $objWriter->startElement('w:vertAlign'); + $objWriter->writeAttribute('w:val', $superscript ? 'superscript' : 'subscript'); + $objWriter->endElement(); + } + $objWriter->endElement(); } diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index 6539dd21..32c69547 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -137,6 +137,9 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $borders = $_settings->getBorderSize(); + $colsNum = $_settings->getColsNum(); + $colsSpace = $_settings->getColsSpace(); + $objWriter->startElement('w:sectPr'); foreach ($_headers as &$_header) { @@ -227,7 +230,10 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $objWriter->startElement('w:cols'); - $objWriter->writeAttribute('w:space', '720'); + if($colsNum > 1){ + $objWriter->writeAttribute('w:num', $colsNum); + } + $objWriter->writeAttribute('w:space', $colsSpace); $objWriter->endElement(); diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php index 71aca637..fd4be1e1 100755 --- a/Classes/PHPWord/Writer/Word2007/Styles.php +++ b/Classes/PHPWord/Writer/Word2007/Styles.php @@ -56,6 +56,27 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base // Write DocDefaults $this->_writeDocDefaults($objWriter); + // Write Normal + // Start Hack + $objWriter->startElement('w:style'); + $objWriter->writeAttribute('w:type', 'paragraph'); + $objWriter->writeAttribute('w:default', '1'); + $objWriter->writeAttribute('w:styleId', 'Normal'); + + $objWriter->startElement('w:name'); + $objWriter->writeAttribute('w:val', 'Normal'); + $objWriter->endElement(); + + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:spacing'); + $objWriter->writeAttribute('w:before', 20 * 0); + $objWriter->writeAttribute('w:after', 20 * 0); + $objWriter->writeAttribute('w:line', 240 * 1); + $objWriter->endElement(); + $objWriter->endElement(); + + $objWriter->endElement(); + // End Hack // Write Style Definitions $styles = PHPWord_Style::getStyles(); @@ -92,6 +113,10 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); if (!is_null($paragraphStyle)) { + // 2013-12-31 11:34 IL + $objWriter->startElement('w:basedOn'); + $objWriter->writeAttribute('w:val', 'Normal'); + $objWriter->endElement(); $this->_writeParagraphStyle($objWriter, $paragraphStyle); } @@ -109,6 +134,11 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->writeAttribute('w:val', $styleName); $objWriter->endElement(); + // 2013-12-31 11:34 IL + $objWriter->startElement('w:basedOn'); + $objWriter->writeAttribute('w:val', 'Normal'); + $objWriter->endElement(); + $this->_writeParagraphStyle($objWriter, $style); $objWriter->endElement(); diff --git a/README.md b/README.md index d1d73199..8e57b9dd 100755 --- a/README.md +++ b/README.md @@ -1,9 +1,17 @@ # PHPWord - OpenXML - Read, Write and Create Word documents in PHP -PHPWord is a library written in PHP that create word documents. +PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software. +## Forked features by Ivan Lanin + +* Superscript/subscript `w:vertAlign` +* Hanging +* Section with column +* Softbreak (SHIFT + ENTER) in Text Run +* Redefine normal paragraph style and base all other style from it + ## Want to contribute? Fork us! From 3fa49ee53b2e031391f8c83fb63e459a4c792380 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 1 Jan 2014 20:12:51 +0700 Subject: [PATCH 03/18] section breakType --- Classes/PHPWord/Section/Settings.php | 12 ++- Classes/PHPWord/Writer/Word2007/Document.php | 8 ++ README.md | 90 +------------------- 3 files changed, 22 insertions(+), 88 deletions(-) diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index 87596a3e..c005ae40 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -152,6 +152,7 @@ class PHPWord_Section_Settings private $_colsNum; private $_colsSpace; + private $_breakType; /** * Create new Section Settings @@ -174,7 +175,8 @@ class PHPWord_Section_Settings $this->_borderBottomSize = null; $this->_borderBottomColor = null; $this->_colsNum = 1; - $this->_colsSpace = 360; + $this->_colsSpace = 0.5 * 1440 / 2.54; // in twips: 1 twip = 1/1440 in; 1 cm = 2.54 in + $this->_breakType = null; } /** @@ -562,4 +564,12 @@ class PHPWord_Section_Settings $this->_colsSpace = $pValue; return $this; } + + public function getBreakType() { + return $this->_breakType; + } + public function setBreakType($pValue = null) { + $this->_breakType = $pValue; + return $this; + } } diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index 32c69547..bcb5b756 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -139,6 +139,7 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $colsNum = $_settings->getColsNum(); $colsSpace = $_settings->getColsSpace(); + $breakType = $_settings->getBreakType(); $objWriter->startElement('w:sectPr'); @@ -155,6 +156,13 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); } + // http://www.schemacentral.com/sc/ooxml/a-w_val-43.html + if (!is_null($breakType)) { + $objWriter->startElement('w:type'); + $objWriter->writeAttribute('w:val', $breakType); + $objWriter->endElement(); + } + if (!is_null($_footer)) { $rId = $_footer->getRelationId(); $objWriter->startElement('w:footerReference'); diff --git a/README.md b/README.md index 8e57b9dd..ea995d2f 100755 --- a/README.md +++ b/README.md @@ -1,100 +1,16 @@ # PHPWord - OpenXML - Read, Write and Create Word documents in PHP PHPWord is a library written in PHP that create word documents. -No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be -opened by all major office software. -## Forked features by Ivan Lanin +## Add features in this fork * Superscript/subscript `w:vertAlign` * Hanging * Section with column * Softbreak (SHIFT + ENTER) in Text Run * Redefine normal paragraph style and base all other style from it - -## Want to contribute? -Fork us! - -## Requirements - -* PHP version 5.3.0 or higher +* Section `breakType`. Used especially for continuous column ## License + PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md) - -## Installation - -It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add -the following lines to your ``composer.json``. - -```json -{ - "require": { - "phpoffice/phpword": "dev-master" - } -} -``` - -## Usage - -The following is a basic example of the PHPWord library. - -```php -$PHPWord = new PHPWord(); - -// Every element you want to append to the word document is placed in a section. So you need a section: -$section = $PHPWord->createSection(); - -// After creating a section, you can append elements: -$section->addText('Hello world!'); - -// You can directly style your text by giving the addText function an array: -$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); - -// If you often need the same style again you can create a user defined style to the word document -// and give the addText function the name of the style: -$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); -$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); - -// You can also putthe appended element to local object an call functions like this: -$myTextElement = $section->addText('Hello World!'); -$myTextElement->setBold(); -$myTextElement->setName('Verdana'); -$myTextElement->setSize(22); - -// At least write the document to webspace: -$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); -$objWriter->save('helloWorld.docx'); -``` - -## Images - -You can add images easily using the following example. - -```php -$section = $PHPWord->createSection(); -$section->addImage('mars.jpg'); -``` - -Images settings include: - * ``width`` width in pixels - * ``height`` height in pixels - * ``align`` image alignment, __left__, __right__ or __center__ - * ``marginTop`` top margin in inches, can be negative - * ``marginLeft`` left margin in inches, can be negative - * ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__ - - To add an image with settings, consider the following example. - - ```php -$section->addImage( - 'mars.jpg', - array( - 'width' => 100, - 'height' => 100, - 'marginTop' => -1, - 'marginLeft' => -1, - wrappingStyle => 'behind' - ) -); - ``` From 0edaaaab500fa9fb7278d8b8f791dfba6a980eb3 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 2 Jan 2014 00:59:38 +0700 Subject: [PATCH 04/18] Tidy up for pull request --- Classes/PHPWord/Section/Settings.php | 67 ++++++++++++-- Classes/PHPWord/Style/Paragraph.php | 26 +++--- Classes/PHPWord/Writer/Word2007/Document.php | 4 +- Classes/PHPWord/Writer/Word2007/Styles.php | 23 ++--- README.md | 94 ++++++++++++++++++-- 5 files changed, 168 insertions(+), 46 deletions(-) diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index c005ae40..487a9d19 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -150,8 +150,25 @@ class PHPWord_Section_Settings */ private $_borderBottomColor; + /** + * Section Columns Count + * + * @var int + */ private $_colsNum; + + /** + * Section Spacing Between Columns + * + * @var int + */ private $_colsSpace; + + /** + * Section Break Type + * + * @var string + */ private $_breakType; /** @@ -175,7 +192,7 @@ class PHPWord_Section_Settings $this->_borderBottomSize = null; $this->_borderBottomColor = null; $this->_colsNum = 1; - $this->_colsSpace = 0.5 * 1440 / 2.54; // in twips: 1 twip = 1/1440 in; 1 cm = 2.54 in + $this->_colsSpace = 720; $this->_breakType = null; } @@ -550,26 +567,60 @@ class PHPWord_Section_Settings return $this->_borderBottomColor; } - public function getColsNum() { - return $this->_colsNum; - } + /** + * Set Section Columns Count + * + * @param in $pValue + */ public function setColsNum($pValue = '') { $this->_colsNum = $pValue; return $this; } - public function getColsSpace() { - return $this->_colsSpace; + + /** + * Get Section Columns Count + * + * @return int + */ + public function getColsNum() { + return $this->_colsNum; } + + /** + * Set Section Space Between Columns + * + * @param int $pValue + */ public function setColsSpace($pValue = '') { $this->_colsSpace = $pValue; return $this; } - public function getBreakType() { - return $this->_breakType; + /** + * Get Section Space Between Columns + * + * @return int + */ + public function getColsSpace() { + return $this->_colsSpace; } + + /** + * Set Break Type + * + * @param string $pValue + */ public function setBreakType($pValue = null) { $this->_breakType = $pValue; return $this; } + + /** + * Get Break Type + * + * @return string + */ + public function getBreakType() { + return $this->_breakType; + } } diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 3f367e5e..c5c0070b 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -103,10 +103,10 @@ class PHPWord_Style_Paragraph public function setStyleValue($key, $value) { if ($key == '_indent') { - $value = $value * 1440 / 2.54; // Measured by cm + $value = (int)$value * 720; // 720 twips per indent } if ($key == '_hanging') { - $value = $value * 1440 / 2.54; // Measured by cm + $value =(int)$value * 720; } if ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips @@ -231,16 +231,6 @@ class PHPWord_Style_Paragraph return $this; } - /** - * Get hanging - * - * @return int - */ - public function getHanging() - { - return $this->_hanging; - } - /** * Set hanging * @@ -253,10 +243,20 @@ class PHPWord_Style_Paragraph return $this; } + /** + * Get hanging + * + * @return int + */ + public function getHanging() + { + return $this->_hanging; + } + /** * Get tabs * - * @return PHPWord_Style_Tabs + * @return PHPWord_Style_Paragraph */ public function getTabs() { diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index bcb5b756..738476cd 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -240,8 +240,10 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $objWriter->startElement('w:cols'); if($colsNum > 1){ $objWriter->writeAttribute('w:num', $colsNum); + $objWriter->writeAttribute('w:space', $colsSpace); + } else { + $objWriter->writeAttribute('w:space', '720'); } - $objWriter->writeAttribute('w:space', $colsSpace); $objWriter->endElement(); diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php index fd4be1e1..fb39b57b 100755 --- a/Classes/PHPWord/Writer/Word2007/Styles.php +++ b/Classes/PHPWord/Writer/Word2007/Styles.php @@ -56,27 +56,22 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base // Write DocDefaults $this->_writeDocDefaults($objWriter); - // Write Normal - // Start Hack + // Reset Normal Paragraph Style $objWriter->startElement('w:style'); $objWriter->writeAttribute('w:type', 'paragraph'); $objWriter->writeAttribute('w:default', '1'); $objWriter->writeAttribute('w:styleId', 'Normal'); - $objWriter->startElement('w:name'); $objWriter->writeAttribute('w:val', 'Normal'); - $objWriter->endElement(); - + $objWriter->endElement(); // w:name $objWriter->startElement('w:pPr'); $objWriter->startElement('w:spacing'); - $objWriter->writeAttribute('w:before', 20 * 0); - $objWriter->writeAttribute('w:after', 20 * 0); - $objWriter->writeAttribute('w:line', 240 * 1); - $objWriter->endElement(); - $objWriter->endElement(); - - $objWriter->endElement(); - // End Hack + $objWriter->writeAttribute('w:before', 0); + $objWriter->writeAttribute('w:after', 0); + $objWriter->writeAttribute('w:line', 240); + $objWriter->endElement(); // w:spacing + $objWriter->endElement(); // w:pPr + $objWriter->endElement(); // w:style // Write Style Definitions $styles = PHPWord_Style::getStyles(); @@ -113,7 +108,6 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); if (!is_null($paragraphStyle)) { - // 2013-12-31 11:34 IL $objWriter->startElement('w:basedOn'); $objWriter->writeAttribute('w:val', 'Normal'); $objWriter->endElement(); @@ -134,7 +128,6 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->writeAttribute('w:val', $styleName); $objWriter->endElement(); - // 2013-12-31 11:34 IL $objWriter->startElement('w:basedOn'); $objWriter->writeAttribute('w:val', 'Normal'); $objWriter->endElement(); diff --git a/README.md b/README.md index ea995d2f..d1d73199 100755 --- a/README.md +++ b/README.md @@ -1,16 +1,92 @@ # PHPWord - OpenXML - Read, Write and Create Word documents in PHP -PHPWord is a library written in PHP that create word documents. +PHPWord is a library written in PHP that create word documents. +No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be +opened by all major office software. -## Add features in this fork +## Want to contribute? +Fork us! -* Superscript/subscript `w:vertAlign` -* Hanging -* Section with column -* Softbreak (SHIFT + ENTER) in Text Run -* Redefine normal paragraph style and base all other style from it -* Section `breakType`. Used especially for continuous column +## Requirements + +* PHP version 5.3.0 or higher ## License - PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md) + +## Installation + +It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add +the following lines to your ``composer.json``. + +```json +{ + "require": { + "phpoffice/phpword": "dev-master" + } +} +``` + +## Usage + +The following is a basic example of the PHPWord library. + +```php +$PHPWord = new PHPWord(); + +// Every element you want to append to the word document is placed in a section. So you need a section: +$section = $PHPWord->createSection(); + +// After creating a section, you can append elements: +$section->addText('Hello world!'); + +// You can directly style your text by giving the addText function an array: +$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); + +// If you often need the same style again you can create a user defined style to the word document +// and give the addText function the name of the style: +$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); +$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); + +// You can also putthe appended element to local object an call functions like this: +$myTextElement = $section->addText('Hello World!'); +$myTextElement->setBold(); +$myTextElement->setName('Verdana'); +$myTextElement->setSize(22); + +// At least write the document to webspace: +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save('helloWorld.docx'); +``` + +## Images + +You can add images easily using the following example. + +```php +$section = $PHPWord->createSection(); +$section->addImage('mars.jpg'); +``` + +Images settings include: + * ``width`` width in pixels + * ``height`` height in pixels + * ``align`` image alignment, __left__, __right__ or __center__ + * ``marginTop`` top margin in inches, can be negative + * ``marginLeft`` left margin in inches, can be negative + * ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__ + + To add an image with settings, consider the following example. + + ```php +$section->addImage( + 'mars.jpg', + array( + 'width' => 100, + 'height' => 100, + 'marginTop' => -1, + 'marginLeft' => -1, + wrappingStyle => 'behind' + ) +); + ``` From 72e8f3fccf40d702e050f8cb0aa261ba4c62f502 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 2 Jan 2014 01:02:01 +0700 Subject: [PATCH 05/18] Fix comment error --- Classes/PHPWord/Style/Paragraph.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index c5c0070b..628f9791 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -256,7 +256,7 @@ class PHPWord_Style_Paragraph /** * Get tabs * - * @return PHPWord_Style_Paragraph + * @return PHPWord_Style_Tabs */ public function getTabs() { From d3e8b69cc47dd67f163f87a08fd4c4f9beabbd01 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 2 Jan 2014 01:30:57 +0700 Subject: [PATCH 06/18] Remove (int) since there's a possibility for fraction --- Classes/PHPWord/Style/Paragraph.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index 628f9791..d7e7f508 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -103,10 +103,10 @@ class PHPWord_Style_Paragraph public function setStyleValue($key, $value) { if ($key == '_indent') { - $value = (int)$value * 720; // 720 twips per indent + $value = $value * 720; // 720 twips per indent } if ($key == '_hanging') { - $value =(int)$value * 720; + $value = $value * 720; } if ($key == '_spacing') { $value += 240; // because line height of 1 matches 240 twips From d55db9da83bdc13de17824093236b87bac3e46eb Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 9 Jan 2014 07:45:41 +0700 Subject: [PATCH 07/18] Patch branch --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1d73199..6ef55f31 100755 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # PHPWord - OpenXML - Read, Write and Create Word documents in PHP -PHPWord is a library written in PHP that create word documents. +PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software. +Test patch branch + ## Want to contribute? Fork us! From 4d94d5763cdd2bf6fa2a4db1985c46cf58c984a1 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Mon, 13 Jan 2014 23:14:05 +0700 Subject: [PATCH 08/18] (1) new width property for table; (2) allow table cell width to be null --- Classes/PHPWord/Section/Table.php | 30 +++++++++++++++++++++++- Classes/PHPWord/Style/Table.php | 4 ++++ Classes/PHPWord/Writer/Word2007/Base.php | 7 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 5f3e1ea5..02aeacb7 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -66,6 +66,13 @@ class PHPWord_Section_Table */ private $_pCount; + /** + * Table width + * + * @var int + */ + private $_width = null; + /** * Create a new table @@ -113,7 +120,7 @@ class PHPWord_Section_Table * @param mixed $style * @return PHPWord_Section_Table_Cell */ - public function addCell($width, $style = null) + public function addCell($width = null, $style = null) { $cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style); $i = count($this->_rows) - 1; @@ -150,4 +157,25 @@ class PHPWord_Section_Table { return $this->_style; } + + /** + * Set table width + * + * @var int $width + */ + public function setWidth($width) + { + $this->_width = $width; + } + + /** + * Get table width + * + * @return int + */ + public function getWidth() + { + return $this->_width; + } + } diff --git a/Classes/PHPWord/Style/Table.php b/Classes/PHPWord/Style/Table.php index c3b49ea3..02a794aa 100755 --- a/Classes/PHPWord/Style/Table.php +++ b/Classes/PHPWord/Style/Table.php @@ -30,6 +30,10 @@ */ class PHPWord_Style_Table { + const WIDTH_TYPE_NIL = 'nil'; // No Width + const WIDTH_TYPE_PERCENT = 'pct'; // Width in Fiftieths of a Percent + const WIDTH_TYPE_POINT = 'dxa'; // Width in Twentieths of a Point + const WIDTH_TYPE_AUTO = 'auto'; // Automatically Determined Width private $_cellMarginTop; private $_cellMarginLeft; diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index c9e81ab8..47efd73a 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -412,6 +412,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart if ($_cRows > 0) { $objWriter->startElement('w:tbl'); $tblStyle = $table->getStyle(); + $tblWidth = $table->getWidth(); if ($tblStyle instanceof PHPWord_Style_Table) { $this->_writeTableStyle($objWriter, $tblStyle); } else { @@ -420,6 +421,12 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:tblStyle'); $objWriter->writeAttribute('w:val', $tblStyle); $objWriter->endElement(); + if (!is_null($tblWidth)) { + $objWriter->startElement('w:tblW'); + $objWriter->writeAttribute('w:w', $tblWidth); + $objWriter->writeAttribute('w:type', 'pct'); + $objWriter->endElement(); + } $objWriter->endElement(); } } From 194940d47801afdd5e6acaf3ded32210e759d851 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 16 Jan 2014 14:33:38 +0700 Subject: [PATCH 09/18] Width: dxa > pct --- Classes/PHPWord/Writer/Word2007/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 47efd73a..cfaedc78 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -455,7 +455,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:tcPr'); $objWriter->startElement('w:tcW'); $objWriter->writeAttribute('w:w', $width); - $objWriter->writeAttribute('w:type', 'dxa'); + $objWriter->writeAttribute('w:type', 'pct'); $objWriter->endElement(); if ($cellStyle instanceof PHPWord_Style_Cell) { From b0a24703f307433c29d73718ecd48422ce682d60 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 22 Feb 2014 11:01:15 +0700 Subject: [PATCH 10/18] `Table Row` allows `tblHeader` and `cantSplit` --- Classes/PHPWord/Section/Table.php | 27 +---- Classes/PHPWord/Section/Table/Row.php | 141 +++++++++++++++++++++++ Classes/PHPWord/Style/Row.php | 85 ++++++++++++++ Classes/PHPWord/Writer/Word2007/Base.php | 28 +++-- 4 files changed, 252 insertions(+), 29 deletions(-) create mode 100644 Classes/PHPWord/Section/Table/Row.php create mode 100644 Classes/PHPWord/Style/Row.php diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 02aeacb7..157bf070 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -45,13 +45,6 @@ class PHPWord_Section_Table */ private $_rows = array(); - /** - * Row heights - * - * @var array - */ - private $_rowHeights = array(); - /** * Table holder * @@ -107,10 +100,11 @@ class PHPWord_Section_Table * * @param int $height */ - public function addRow($height = null) + public function addRow($height = null, $style = null) { - $this->_rows[] = array(); - $this->_rowHeights[] = $height; + $row = new PHPWord_Section_Table_Row($this->_insideOf, $this->_pCount, $height, $style); + $this->_rows[] = $row; + return $row; } /** @@ -122,9 +116,8 @@ class PHPWord_Section_Table */ public function addCell($width = null, $style = null) { - $cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style); $i = count($this->_rows) - 1; - $this->_rows[$i][] = $cell; + $cell = $this->_rows[$i]->addCell($width, $style); return $cell; } @@ -138,16 +131,6 @@ class PHPWord_Section_Table return $this->_rows; } - /** - * Get all row heights - * - * @return array - */ - public function getRowHeights() - { - return $this->_rowHeights; - } - /** * Get table style * diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php new file mode 100644 index 00000000..9f8085a2 --- /dev/null +++ b/Classes/PHPWord/Section/Table/Row.php @@ -0,0 +1,141 @@ +_insideOf = $insideOf; + $this->_pCount = $pCount; + $this->_height = $height; + $this->_style = new PHPWord_Style_Row(); + + if (!is_null($style)) { + if (is_array($style)) { + + foreach ($style as $key => $value) { + if (substr($key, 0, 1) != '_') { + $key = '_' . $key; + } + $this->_style->setStyleValue($key, $value); + } + } + } + } + + /** + * Add a cell + * + * @param int $width + * @param mixed $style + * @return PHPWord_Section_Table_Cell + */ + public function addCell($width = null, $style = null) + { + $cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style); + $this->_cells[] = $cell; + return $cell; + } + + /** + * Get all cells + * + * @return array + */ + public function getCells() + { + return $this->_cells; + } + + /** + * Get row style + * + * @return PHPWord_Style_Row + */ + public function getStyle() + { + return $this->_style; + } + + /** + * Get row height + * + * @return int + */ + public function getHeight() + { + return $this->_height; + } +} \ No newline at end of file diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php new file mode 100644 index 00000000..0bd99144 --- /dev/null +++ b/Classes/PHPWord/Style/Row.php @@ -0,0 +1,85 @@ +_tblHeader = null; + $this->_cantSplit = null; + } + + /** + * Set style value + */ + public function setStyleValue($key, $value) + { + $this->$key = $value; + } + + public function setTblHeader($pValue = null) + { + $this->_tblHeader = $pValue; + } + + public function getTblHeader() + { + return $this->_tblHeader ? 1 : 0; + } + + public function setCantSplit($pValue = null) + { + $this->_cantSplit = $pValue; + } + + public function getCantSplit() + { + return $this->_cantSplit ? 1 : 0; + } + +} \ No newline at end of file diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index cfaedc78..9ffbe59c 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -431,22 +431,36 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } } - $_heights = $table->getRowHeights(); for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; - $height = $_heights[$i]; + $height = $row->getHeight(); + $rowStyle = $row->getStyle(); + $tblHeader = $rowStyle->getTblHeader(); + $cantSplit = $rowStyle->getCantSplit(); $objWriter->startElement('w:tr'); - if (!is_null($height)) { + if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) { $objWriter->startElement('w:trPr'); - $objWriter->startElement('w:trHeight'); - $objWriter->writeAttribute('w:val', $height); - $objWriter->endElement(); + if (!is_null($height)) { + $objWriter->startElement('w:trHeight'); + $objWriter->writeAttribute('w:val', $height); + $objWriter->endElement(); + } + if (!is_null($tblHeader)) { + $objWriter->startElement('w:tblHeader'); + $objWriter->writeAttribute('w:val', $tblHeader); + $objWriter->endElement(); + } + if (!is_null($cantSplit)) { + $objWriter->startElement('w:cantSplit'); + $objWriter->writeAttribute('w:val', $cantSplit); + $objWriter->endElement(); + } $objWriter->endElement(); } - foreach ($row as $cell) { + foreach ($row->getCells() as $cell) { $objWriter->startElement('w:tc'); $cellStyle = $cell->getStyle(); From eb6b44ef85b87253ed629117814f6f2549a92236 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sat, 22 Feb 2014 22:52:19 +0700 Subject: [PATCH 11/18] Use constant instead of hardcoded 'Arial' name. --- Classes/PHPWord.php | 7 +++++-- Classes/PHPWord/Style/Font.php | 12 ++++++------ Classes/PHPWord/Writer/ODText/Content.php | 6 +++--- Classes/PHPWord/Writer/ODText/Styles.php | 8 ++++---- Classes/PHPWord/Writer/RTF.php | 4 ++-- Classes/PHPWord/Writer/Word2007/Base.php | 4 ++-- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 3879d7cd..51b37366 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -39,6 +39,9 @@ if (!defined('PHPWORD_BASE_PATH')) { class PHPWord { + const DEFAULT_FONT_NAME = 'Arial'; + const DEFAULT_FONT_SIZE = 20; + /** * Document properties * @@ -74,8 +77,8 @@ class PHPWord public function __construct() { $this->_properties = new PHPWord_DocumentProperties(); - $this->_defaultFontName = 'Arial'; - $this->_defaultFontSize = 20; + $this->_defaultFontName = PHPWord::DEFAULT_FONT_NAME; + $this->_defaultFontSize = PHPWord::DEFAULT_FONT_SIZE; } /** diff --git a/Classes/PHPWord/Style/Font.php b/Classes/PHPWord/Style/Font.php index eafc10b7..293d7e35 100755 --- a/Classes/PHPWord/Style/Font.php +++ b/Classes/PHPWord/Style/Font.php @@ -94,8 +94,8 @@ class PHPWord_Style_Font public function __construct($type = 'text', $styleParagraph = null) { $this->_type = $type; - $this->_name = 'Arial'; - $this->_size = 20; + $this->_name = PHPWord::DEFAULT_FONT_NAME; + $this->_size = PHPWord::DEFAULT_FONT_SIZE; $this->_bold = false; $this->_italic = false; $this->_superScript = false; @@ -132,10 +132,10 @@ class PHPWord_Style_Font $this->$key = $value; } - public function setName($pValue = 'Arial') + public function setName($pValue = PHPWord::DEFAULT_FONT_NAME) { if ($pValue == '') { - $pValue = 'Arial'; + $pValue = PHPWord::DEFAULT_FONT_NAME; } $this->_name = $pValue; return $this; @@ -146,10 +146,10 @@ class PHPWord_Style_Font return $this->_size; } - public function setSize($pValue = 20) + public function setSize($pValue = PHPWord::DEFAULT_FONT_SIZE) { if ($pValue == '') { - $pValue = 20; + $pValue = PHPWord::DEFAULT_FONT_SIZE; } $this->_size = ($pValue * 2); return $this; diff --git a/Classes/PHPWord/Writer/ODText/Content.php b/Classes/PHPWord/Writer/ODText/Content.php index 69ba3bef..f8784e10 100755 --- a/Classes/PHPWord/Writer/ODText/Content.php +++ b/Classes/PHPWord/Writer/ODText/Content.php @@ -145,10 +145,10 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart } } } - if (!in_array('Arial', $arrFonts)) { + if (!in_array(PHPWord::DEFAULT_FONT_NAME, $arrFonts)) { $objWriter->startElement('style:font-face'); - $objWriter->writeAttribute('style:name', 'Arial'); - $objWriter->writeAttribute('svg:font-family', 'Arial'); + $objWriter->writeAttribute('style:name', PHPWord::DEFAULT_FONT_NAME); + $objWriter->writeAttribute('svg:font-family', PHPWord::DEFAULT_FONT_NAME); $objWriter->endElement(); } } diff --git a/Classes/PHPWord/Writer/ODText/Styles.php b/Classes/PHPWord/Writer/ODText/Styles.php index 2ec591ae..7830db78 100755 --- a/Classes/PHPWord/Writer/ODText/Styles.php +++ b/Classes/PHPWord/Writer/ODText/Styles.php @@ -104,10 +104,10 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart } } } - if (!in_array('Arial', $arrFonts)) { + if (!in_array(PHPWord::DEFAULT_FONT_NAME, $arrFonts)) { $objWriter->startElement('style:font-face'); - $objWriter->writeAttribute('style:name', 'Arial'); - $objWriter->writeAttribute('svg:font-family', 'Arial'); + $objWriter->writeAttribute('style:name', PHPWord::DEFAULT_FONT_NAME); + $objWriter->writeAttribute('svg:font-family', PHPWord::DEFAULT_FONT_NAME); $objWriter->endElement(); } $objWriter->endElement(); @@ -132,7 +132,7 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart // style:text-properties $objWriter->startElement('style:text-properties'); $objWriter->writeAttribute('style:use-window-font-color', 'true'); - $objWriter->writeAttribute('style:font-name', 'Arial'); + $objWriter->writeAttribute('style:font-name', PHPWord::DEFAULT_FONT_NAME); $objWriter->writeAttribute('fo:font-size', '10pt'); $objWriter->writeAttribute('fo:language', 'fr'); $objWriter->writeAttribute('fo:country', 'FR'); diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index 24f0157e..3f5b73ee 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -191,8 +191,8 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $pPHPWord = $this->_document; $arrFonts = array(); - // Default font : Arial - $arrFonts[] = 'Arial'; + // Default font : PHPWord::DEFAULT_FONT_NAME + $arrFonts[] = PHPWord::DEFAULT_FONT_NAME; // PHPWord object : $this->_document // Browse styles diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 9ffbe59c..aedd4f50 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -334,7 +334,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:rPr'); // Font - if ($font != 'Arial') { + if ($font != PHPWord::DEFAULT_FONT_NAME) { $objWriter->startElement('w:rFonts'); $objWriter->writeAttribute('w:ascii', $font); $objWriter->writeAttribute('w:hAnsi', $font); @@ -350,7 +350,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } // Size - if ($size != 20) { + if ($size != PHPWord::DEFAULT_FONT_SIZE) { $objWriter->startElement('w:sz'); $objWriter->writeAttribute('w:val', $size); $objWriter->endElement(); From ab2b2deac371643fc473c3765d5fc9bac6d6756b Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 23 Feb 2014 11:40:32 +0700 Subject: [PATCH 12/18] Change default font to Times New Roman 12pt --- Classes/PHPWord.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 51b37366..b930bfb5 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -39,8 +39,8 @@ if (!defined('PHPWORD_BASE_PATH')) { class PHPWord { - const DEFAULT_FONT_NAME = 'Arial'; - const DEFAULT_FONT_SIZE = 20; + const DEFAULT_FONT_NAME = 'Times New Roman'; + const DEFAULT_FONT_SIZE = 24; /** * Document properties From 7533d30125426f207fb2ca47e4f4ef2149156542 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 6 Mar 2014 20:57:00 +0700 Subject: [PATCH 13/18] Changes: - Use constants instead of hardcoded name and size for font (Arial, 20) - Section: Multicolumn - Section: Different section break - Paragraph: Hanging paragraph - Font: Superscript and subscript - Table: Table width in percentage - Table: Repeat as header row - Table: Allow row to break across pages --- Classes/PHPWord.php | 4 +- Classes/PHPWord/Section/Settings.php | 89 ++++++++++++++++++++ Classes/PHPWord/Style/Table.php | 4 - Classes/PHPWord/Writer/Word2007/Base.php | 58 ++++++++++--- Classes/PHPWord/Writer/Word2007/Document.php | 15 ++-- Classes/PHPWord/Writer/Word2007/Styles.php | 23 ----- Classes/PHPWord/_staticDocParts/settings.xml | 2 +- 7 files changed, 144 insertions(+), 51 deletions(-) diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index 37fd24d8..658585b7 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -39,8 +39,8 @@ if (!defined('PHPWORD_BASE_PATH')) { class PHPWord { - const DEFAULT_FONT_NAME = 'Times New Roman'; - const DEFAULT_FONT_SIZE = 24; + const DEFAULT_FONT_NAME = 'Arial'; + const DEFAULT_FONT_SIZE = 20; /** * Document properties diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index fce8d7b1..ba595259 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -168,6 +168,34 @@ class PHPWord_Section_Settings */ private $footerHeight; + /** + * Section columns count + * + * @var int + */ + private $_colsNum; + + /** + * Section spacing between columns + * + * @var int + */ + private $_colsSpace; + + /** + * Section break type + * + * Options: + * - nextPage: Next page section break + * - nextColumn: Column section break + * - continuous: Continuous section break + * - evenPage: Even page section break + * - oddPage: Odd page section break + * + * @var string + */ + private $_breakType; + /** * Create new Section Settings */ @@ -190,6 +218,9 @@ class PHPWord_Section_Settings $this->_borderBottomColor = null; $this->headerHeight = 720; // set default header and footer to 720 twips (.5 inches) $this->footerHeight = 720; + $this->_colsNum = 1; + $this->_colsSpace = 720; + $this->_breakType = null; } /** @@ -618,4 +649,62 @@ class PHPWord_Section_Settings $this->footerHeight = $pValue; return $this; } + + /** + * Set Section Columns Count + * + * @param in $pValue + */ + public function setColsNum($pValue = '') { + $this->_colsNum = $pValue; + return $this; + } + + /** + * Get Section Columns Count + * + * @return int + */ + public function getColsNum() { + return $this->_colsNum; + } + + /** + * Set Section Space Between Columns + * + * @param int $pValue + */ + public function setColsSpace($pValue = '') { + $this->_colsSpace = $pValue; + return $this; + } + + /** + * Get Section Space Between Columns + * + * @return int + */ + public function getColsSpace() { + return $this->_colsSpace; + } + + /** + * Set Break Type + * + * @param string $pValue + */ + public function setBreakType($pValue = null) { + $this->_breakType = $pValue; + return $this; + } + + /** + * Get Break Type + * + * @return string + */ + public function getBreakType() { + return $this->_breakType; + } + } diff --git a/Classes/PHPWord/Style/Table.php b/Classes/PHPWord/Style/Table.php index 73def233..5f4f60c1 100755 --- a/Classes/PHPWord/Style/Table.php +++ b/Classes/PHPWord/Style/Table.php @@ -30,10 +30,6 @@ */ class PHPWord_Style_Table { - const WIDTH_TYPE_NIL = 'nil'; // No Width - const WIDTH_TYPE_PERCENT = 'pct'; // Width in Fiftieths of a Percent - const WIDTH_TYPE_POINT = 'dxa'; // Width in Twentieths of a Point - const WIDTH_TYPE_AUTO = 'auto'; // Automatically Determined Width private $_cellMarginTop; private $_cellMarginLeft; diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index a90126d9..d9d2cb09 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -122,6 +122,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $spaceAfter = $style->getSpaceAfter(); $spacing = $style->getSpacing(); $indent = $style->getIndent(); + $hanging = $style->getHanging(); $tabs = $style->getTabs(); if (!is_null($align) || !is_null($spacing) || !is_null($spaceBefore) || !is_null($spaceAfter) || !is_null($indent) || !is_null($tabs)) { @@ -135,10 +136,15 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } - if (!is_null($indent)) { + if (!is_null($indent) || !is_null($hanging)) { $objWriter->startElement('w:ind'); $objWriter->writeAttribute('w:firstLine', 0); - $objWriter->writeAttribute('w:left', $indent); + if (!is_null($indent)) { + $objWriter->writeAttribute('w:left', $indent); + } + if (!is_null($hanging)) { + $objWriter->writeAttribute('w:hanging', $hanging); + } $objWriter->endElement(); } @@ -322,11 +328,13 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $fgColor = $style->getFgColor(); $striketrough = $style->getStrikethrough(); $underline = $style->getUnderline(); + $superscript = $style->getSuperScript(); + $subscript = $style->getSubScript(); $objWriter->startElement('w:rPr'); // Font - if ($font != 'Arial') { + if ($font != PHPWord::DEFAULT_FONT_NAME) { $objWriter->startElement('w:rFonts'); $objWriter->writeAttribute('w:ascii', $font); $objWriter->writeAttribute('w:hAnsi', $font); @@ -342,7 +350,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart } // Size - if ($size != 20) { + if ($size != PHPWord::DEFAULT_FONT_SIZE) { $objWriter->startElement('w:sz'); $objWriter->writeAttribute('w:val', $size); $objWriter->endElement(); @@ -381,6 +389,13 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->endElement(); } + // Superscript/subscript + if ($superscript || $subscript) { + $objWriter->startElement('w:vertAlign'); + $objWriter->writeAttribute('w:val', $superscript ? 'superscript' : 'subscript'); + $objWriter->endElement(); + } + $objWriter->endElement(); } @@ -397,6 +412,7 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart if ($_cRows > 0) { $objWriter->startElement('w:tbl'); $tblStyle = $table->getStyle(); + $tblWidth = $table->getWidth(); if ($tblStyle instanceof PHPWord_Style_Table) { $this->_writeTableStyle($objWriter, $tblStyle); } else { @@ -405,26 +421,46 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $objWriter->startElement('w:tblStyle'); $objWriter->writeAttribute('w:val', $tblStyle); $objWriter->endElement(); + if (!is_null($tblWidth)) { + $objWriter->startElement('w:tblW'); + $objWriter->writeAttribute('w:w', $tblWidth); + $objWriter->writeAttribute('w:type', 'pct'); + $objWriter->endElement(); + } $objWriter->endElement(); } } - $_heights = $table->getRowHeights(); for ($i = 0; $i < $_cRows; $i++) { $row = $_rows[$i]; - $height = $_heights[$i]; + $height = $row->getHeight(); + $rowStyle = $row->getStyle(); + $tblHeader = $rowStyle->getTblHeader(); + $cantSplit = $rowStyle->getCantSplit(); $objWriter->startElement('w:tr'); - if (!is_null($height)) { + if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) { $objWriter->startElement('w:trPr'); - $objWriter->startElement('w:trHeight'); - $objWriter->writeAttribute('w:val', $height); - $objWriter->endElement(); + if (!is_null($height)) { + $objWriter->startElement('w:trHeight'); + $objWriter->writeAttribute('w:val', $height); + $objWriter->endElement(); + } + if (!is_null($tblHeader)) { + $objWriter->startElement('w:tblHeader'); + $objWriter->writeAttribute('w:val', $tblHeader); + $objWriter->endElement(); + } + if (!is_null($cantSplit)) { + $objWriter->startElement('w:cantSplit'); + $objWriter->writeAttribute('w:val', $cantSplit); + $objWriter->endElement(); + } $objWriter->endElement(); } - foreach ($row as $cell) { + foreach ($row->getCells() as $cell) { $objWriter->startElement('w:tc'); $cellStyle = $cell->getStyle(); diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index b49b534d..9fc2160a 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -140,9 +140,9 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $borders = $settings->getBorderSize(); - $colsNum = $_settings->getColsNum(); - $colsSpace = $_settings->getColsSpace(); - $breakType = $_settings->getBreakType(); + $colsNum = $settings->getColsNum(); + $colsSpace = $settings->getColsSpace(); + $breakType = $settings->getBreakType(); $objWriter->startElement('w:sectPr'); @@ -159,7 +159,6 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); } - // http://www.schemacentral.com/sc/ooxml/a-w_val-43.html if (!is_null($breakType)) { $objWriter->startElement('w:type'); $objWriter->writeAttribute('w:val', $breakType); @@ -247,12 +246,8 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base } $objWriter->startElement('w:cols'); - if($colsNum > 1){ - $objWriter->writeAttribute('w:num', $colsNum); - $objWriter->writeAttribute('w:space', $colsSpace); - } else { - $objWriter->writeAttribute('w:space', '720'); - } + $objWriter->writeAttribute('w:num', $colsNum); + $objWriter->writeAttribute('w:space', $colsSpace); $objWriter->endElement(); diff --git a/Classes/PHPWord/Writer/Word2007/Styles.php b/Classes/PHPWord/Writer/Word2007/Styles.php index ded0a161..d2e28539 100755 --- a/Classes/PHPWord/Writer/Word2007/Styles.php +++ b/Classes/PHPWord/Writer/Word2007/Styles.php @@ -56,22 +56,6 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base // Write DocDefaults $this->_writeDocDefaults($objWriter); - // Reset Normal Paragraph Style - $objWriter->startElement('w:style'); - $objWriter->writeAttribute('w:type', 'paragraph'); - $objWriter->writeAttribute('w:default', '1'); - $objWriter->writeAttribute('w:styleId', 'Normal'); - $objWriter->startElement('w:name'); - $objWriter->writeAttribute('w:val', 'Normal'); - $objWriter->endElement(); // w:name - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:spacing'); - $objWriter->writeAttribute('w:before', 0); - $objWriter->writeAttribute('w:after', 0); - $objWriter->writeAttribute('w:line', 240); - $objWriter->endElement(); // w:spacing - $objWriter->endElement(); // w:pPr - $objWriter->endElement(); // w:style // Write Style Definitions $styles = PHPWord_Style::getStyles(); @@ -108,9 +92,6 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); if (!is_null($paragraphStyle)) { - $objWriter->startElement('w:basedOn'); - $objWriter->writeAttribute('w:val', 'Normal'); - $objWriter->endElement(); $this->_writeParagraphStyle($objWriter, $paragraphStyle); } @@ -128,10 +109,6 @@ class PHPWord_Writer_Word2007_Styles extends PHPWord_Writer_Word2007_Base $objWriter->writeAttribute('w:val', $styleName); $objWriter->endElement(); - $objWriter->startElement('w:basedOn'); - $objWriter->writeAttribute('w:val', 'Normal'); - $objWriter->endElement(); - $this->_writeParagraphStyle($objWriter, $style); $objWriter->endElement(); diff --git a/Classes/PHPWord/_staticDocParts/settings.xml b/Classes/PHPWord/_staticDocParts/settings.xml index 56ca9860..5eb22891 100644 --- a/Classes/PHPWord/_staticDocParts/settings.xml +++ b/Classes/PHPWord/_staticDocParts/settings.xml @@ -1,4 +1,4 @@ - + From f1c2c2fe5ec87956bda5ca83767ffddeeeeab48d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 6 Mar 2014 22:06:01 +0700 Subject: [PATCH 14/18] Copyright year --- Classes/PHPWord/Section/Table/Row.php | 2 +- Classes/PHPWord/Style/Row.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index 9f8085a2..c2db614b 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -2,7 +2,7 @@ /** * PHPWord * - * Copyright (c) 2013 PHPWord + * Copyright (c) 2014 PHPWord * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php index 0bd99144..a0e79b3a 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -2,7 +2,7 @@ /** * PHPWord * - * Copyright (c) 2013 PHPWord + * Copyright (c) 2014 PHPWord * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public From 938d78f40b275b8a72ab357c45b004f64b364ae9 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 6 Mar 2014 22:07:09 +0700 Subject: [PATCH 15/18] Samples: (1) Superscript and subscript; (2) Multicolumn --- samples/Sample_04_Textrun.php | 5 +++ samples/Sample_05_Multicolumn.php | 66 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 samples/Sample_05_Multicolumn.php diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php index 80ad2d3c..164afc3c 100644 --- a/samples/Sample_04_Textrun.php +++ b/samples/Sample_04_Textrun.php @@ -30,6 +30,11 @@ $textrun = $section->createTextRun('pStyle'); $textrun->addText('Each textrun can contain native text, link elements or an image.'); $textrun->addText(' No break is placed after adding an element.', 'BoldText'); +$textrun->addText(' Both '); +$textrun->addText('superscript', array('superScript' => true)); +$textrun->addText(' and '); +$textrun->addText('subscript', array('subScript' => true)); +$textrun->addText(' are also available.'); $textrun->addText(' All elements are placed inside a paragraph with the optionally given p-Style.', 'ColoredText'); $textrun->addText(' Sample Link: '); $textrun->addLink('http://www.google.com', null, 'NLink'); diff --git a/samples/Sample_05_Multicolumn.php b/samples/Sample_05_Multicolumn.php new file mode 100644 index 00000000..a7889a85 --- /dev/null +++ b/samples/Sample_05_Multicolumn.php @@ -0,0 +1,66 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , " Create new PHPWord object" , EOL; +$PHPWord = new PHPWord(); +$filler = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' . + 'Nulla fermentum, tortor id adipiscing adipiscing, tortor turpis commodo. ' . + 'Donec vulputate iaculis metus, vel luctus dolor hendrerit ac. ' . + 'Suspendisse congue congue leo sed pellentesque.'; + +// Normal +$section = $PHPWord->createSection(); +$section->addText('Normal paragraph. ' . $filler); + +// Two columns +$section = $PHPWord->createSection(array( + 'colsNum' => 2, + 'colsSpace' => 1440, + 'breakType' => 'continuous')); +$section->addText('Three columns, one inch (1440 twips) spacing. ' . $filler); + +// Normal +$section = $PHPWord->createSection(array('breakType' => 'continuous')); +$section->addText('Normal paragraph again. ' . $filler); + +// Three columns +$section = $PHPWord->createSection(array( + 'colsNum' => 3, + 'colsSpace' => 720, + 'breakType' => 'continuous')); +$section->addText('Three columns, half inch (720 twips) spacing. ' . $filler); + +// Normal +$section = $PHPWord->createSection(array('breakType' => 'continuous')); +$section->addText('Normal paragraph again.'); + +// Save File +echo date('H:i:s') , " Write to Word2007 format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save(str_replace('.php', '.docx', __FILE__)); + +// echo date('H:i:s') , " Write to OpenDocumentText format" , EOL; +// $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +// $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 done +echo date('H:i:s') , " Done writing file" , EOL; From 136127894a5f8be733afd8a8a1055f42256d22ff Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 00:32:00 +0700 Subject: [PATCH 16/18] Revert --- README.md | 286 +++++++++++++++++++++++++++--------------------------- 1 file changed, 143 insertions(+), 143 deletions(-) diff --git a/README.md b/README.md index a9f92603..dc1a611a 100755 --- a/README.md +++ b/README.md @@ -1,143 +1,143 @@ -# PHPWord - -[![Build Status](https://travis-ci.org/PHPOffice/PHPWord.png?branch=master)](https://travis-ci.org/PHPOffice/PHPWord) -[![Latest Stable Version](https://poser.pugx.org/phpoffice/phpword/v/stable.png)](https://packagist.org/packages/phpoffice/phpword) [![Total Downloads](https://poser.pugx.org/phpoffice/phpword/downloads.png)](https://packagist.org/packages/phpoffice/phpword) [![Latest Unstable Version](https://poser.pugx.org/phpoffice/phpword/v/unstable.png)](https://packagist.org/packages/phpoffice/phpword) [![License](https://poser.pugx.org/phpoffice/phpword/license.png)](https://packagist.org/packages/phpoffice/phpword) - -__OpenXML - Read, Write and Create Word documents in PHP.__ - -PHPWord is a library written in PHP that create word documents. - -No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be -opened by all major office software. - -__Want to contribute?__ Fork us! - -## Requirements - -* PHP version 5.3.0 or higher - -## Installation - -It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add -the following lines to your ``composer.json``. - -```json -{ - "require": { - "phpoffice/phpword": "dev-master" - } -} -``` - -## Documentation - -### Table of contents - -1. [Basic usage](#basic-usage) -2. [Sections](#sections) - * [Change Section Page Numbering](#sections-page-numbering) -3. [Tables](#tables) - * [Cell Style](#tables-cell-style) -4. [Images](#images) - - -#### Basic usage - -The following is a basic example of the PHPWord library. - -```php -$PHPWord = new PHPWord(); - -// Every element you want to append to the word document is placed in a section. So you need a section: -$section = $PHPWord->createSection(); - -// After creating a section, you can append elements: -$section->addText('Hello world!'); - -// You can directly style your text by giving the addText function an array: -$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); - -// If you often need the same style again you can create a user defined style to the word document -// and give the addText function the name of the style: -$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); -$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); - -// You can also putthe appended element to local object an call functions like this: -$myTextElement = $section->addText('Hello World!'); -$myTextElement->setBold(); -$myTextElement->setName('Verdana'); -$myTextElement->setSize(22); - -// At least write the document to webspace: -$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); -$objWriter->save('helloWorld.docx'); -``` - - -#### Sections - - -##### Change Section Page Numbering - -You can change a section page numbering. - -```php -$section = $PHPWord->createSection(); -$section->getSettings()->setPageNumberingStart(1); -``` - - -#### Tables - -The following illustrates how to create a table. - -```php -$table = $section->addTable(); -$table->addRow(); -$table->addCell(); -``` - - -##### Cell Style - -###### Cell Span - -You can span a cell on multiple columms. - -```php -$cell = $table->addCell(200); -$cell->getStyle()->setGridSpan(5); -``` - - -#### Images - -You can add images easily using the following example. - -```php -$section = $PHPWord->createSection(); -$section->addImage('mars.jpg'); -``` - -Images settings include: -* ``width`` width in pixels -* ``height`` height in pixels -* ``align`` image alignment, _left_, _right_ or _center_ -* ``marginTop`` top margin in inches, can be negative -* ``marginLeft`` left margin in inches, can be negative -* ``wrappingStyle`` can be _inline_, _square_, _tight_, _behind_, _infront_ - -To add an image with settings, consider the following example. - -```php -$section->addImage( - 'mars.jpg', - array( - 'width' => 100, - 'height' => 100, - 'marginTop' => -1, - 'marginLeft' => -1, - 'wrappingStyle' => 'behind' - ) -); - ``` +# PHPWord + +[![Build Status](https://travis-ci.org/PHPOffice/PHPWord.png?branch=master)](https://travis-ci.org/PHPOffice/PHPWord) +[![Latest Stable Version](https://poser.pugx.org/phpoffice/phpword/v/stable.png)](https://packagist.org/packages/phpoffice/phpword) [![Total Downloads](https://poser.pugx.org/phpoffice/phpword/downloads.png)](https://packagist.org/packages/phpoffice/phpword) [![Latest Unstable Version](https://poser.pugx.org/phpoffice/phpword/v/unstable.png)](https://packagist.org/packages/phpoffice/phpword) [![License](https://poser.pugx.org/phpoffice/phpword/license.png)](https://packagist.org/packages/phpoffice/phpword) + +__OpenXML - Read, Write and Create Word documents in PHP.__ + +PHPWord is a library written in PHP that create word documents. + +No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be +opened by all major office software. + +__Want to contribute?__ Fork us! + +## Requirements + +* PHP version 5.3.0 or higher + +## Installation + +It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add +the following lines to your ``composer.json``. + +```json +{ + "require": { + "phpoffice/phpword": "dev-master" + } +} +``` + +## Documentation + +### Table of contents + +1. [Basic usage](#basic-usage) +2. [Sections](#sections) + * [Change Section Page Numbering](#sections-page-numbering) +3. [Tables](#tables) + * [Cell Style](#tables-cell-style) +4. [Images](#images) + + +#### Basic usage + +The following is a basic example of the PHPWord library. + +```php +$PHPWord = new PHPWord(); + +// Every element you want to append to the word document is placed in a section. So you need a section: +$section = $PHPWord->createSection(); + +// After creating a section, you can append elements: +$section->addText('Hello world!'); + +// You can directly style your text by giving the addText function an array: +$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); + +// If you often need the same style again you can create a user defined style to the word document +// and give the addText function the name of the style: +$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); +$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); + +// You can also putthe appended element to local object an call functions like this: +$myTextElement = $section->addText('Hello World!'); +$myTextElement->setBold(); +$myTextElement->setName('Verdana'); +$myTextElement->setSize(22); + +// At least write the document to webspace: +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save('helloWorld.docx'); +``` + + +#### Sections + + +##### Change Section Page Numbering + +You can change a section page numbering. + +```php +$section = $PHPWord->createSection(); +$section->getSettings()->setPageNumberingStart(1); +``` + + +#### Tables + +The following illustrates how to create a table. + +```php +$table = $section->addTable(); +$table->addRow(); +$table->addCell(); +``` + + +##### Cell Style + +###### Cell Span + +You can span a cell on multiple columms. + +```php +$cell = $table->addCell(200); +$cell->getStyle()->setGridSpan(5); +``` + + +#### Images + +You can add images easily using the following example. + +```php +$section = $PHPWord->createSection(); +$section->addImage('mars.jpg'); +``` + +Images settings include: +* ``width`` width in pixels +* ``height`` height in pixels +* ``align`` image alignment, _left_, _right_ or _center_ +* ``marginTop`` top margin in inches, can be negative +* ``marginLeft`` left margin in inches, can be negative +* ``wrappingStyle`` can be _inline_, _square_, _tight_, _behind_, _infront_ + +To add an image with settings, consider the following example. + +```php +$section->addImage( + 'mars.jpg', + array( + 'width' => 100, + 'height' => 100, + 'marginTop' => -1, + 'marginLeft' => -1, + 'wrappingStyle' => 'behind' + ) +); + ``` From 22afd142811803d9b5aaeb6b192bfb67fb222e71 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 00:42:33 +0700 Subject: [PATCH 17/18] Update change log --- changelog.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.txt b/changelog.txt index 27ee52bd..1533a052 100755 --- a/changelog.txt +++ b/changelog.txt @@ -23,6 +23,11 @@ ************************************************************************************** Changes in branch for release 0.7.1 : +- Feature: (ivanlanin) - Table row: Repeat as header row & allow row to break across pages +- Feature: (ivanlanin) - Table: Table width in percentage +- Feature: (ivanlanin) - Font: Superscript and subscript +- Feature: (ivanlanin) - Paragraph: Hanging paragraph +- Feature: (ivanlanin) - Section: Multicolumn and section break - Bugfix: (gabrielbull) - Fixed bug with cell styling - Bugfix: (gabrielbull) - Fixed bug list items inside of cells - Feature: (RomanSyroeshko) GH-56 GH-57 - Template : Permit to save a template generated as a file (PHPWord_Template::saveAs()) From 350e7a61b6da614dd5dcc3f8a971383e2603527e Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 7 Mar 2014 02:07:24 +0700 Subject: [PATCH 18/18] Revert Tests/bootstrap.php and samples/Sample_03_Sections.php --- Tests/bootstrap.php | 0 samples/Sample_03_Sections.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 Tests/bootstrap.php mode change 100644 => 100755 samples/Sample_03_Sections.php diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php old mode 100644 new mode 100755 diff --git a/samples/Sample_03_Sections.php b/samples/Sample_03_Sections.php old mode 100644 new mode 100755