From 0edaaaab500fa9fb7278d8b8f791dfba6a980eb3 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 2 Jan 2014 00:59:38 +0700 Subject: [PATCH] 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' + ) +); + ```