Skip to content

Commit 0edaaaa

Browse files
committed
Tidy up for pull request
1 parent 3fa49ee commit 0edaaaa

File tree

5 files changed

+168
-46
lines changed

5 files changed

+168
-46
lines changed

Classes/PHPWord/Section/Settings.php

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,25 @@ class PHPWord_Section_Settings
150150
*/
151151
private $_borderBottomColor;
152152

153+
/**
154+
* Section Columns Count
155+
*
156+
* @var int
157+
*/
153158
private $_colsNum;
159+
160+
/**
161+
* Section Spacing Between Columns
162+
*
163+
* @var int
164+
*/
154165
private $_colsSpace;
166+
167+
/**
168+
* Section Break Type
169+
*
170+
* @var string
171+
*/
155172
private $_breakType;
156173

157174
/**
@@ -175,7 +192,7 @@ public function __construct()
175192
$this->_borderBottomSize = null;
176193
$this->_borderBottomColor = null;
177194
$this->_colsNum = 1;
178-
$this->_colsSpace = 0.5 * 1440 / 2.54; // in twips: 1 twip = 1/1440 in; 1 cm = 2.54 in
195+
$this->_colsSpace = 720;
179196
$this->_breakType = null;
180197
}
181198

@@ -550,26 +567,60 @@ public function getBorderBottomColor()
550567
return $this->_borderBottomColor;
551568
}
552569

553-
public function getColsNum() {
554-
return $this->_colsNum;
555-
}
570+
/**
571+
* Set Section Columns Count
572+
*
573+
* @param in $pValue
574+
*/
556575
public function setColsNum($pValue = '') {
557576
$this->_colsNum = $pValue;
558577
return $this;
559578
}
560-
public function getColsSpace() {
561-
return $this->_colsSpace;
579+
580+
/**
581+
* Get Section Columns Count
582+
*
583+
* @return int
584+
*/
585+
public function getColsNum() {
586+
return $this->_colsNum;
562587
}
588+
589+
/**
590+
* Set Section Space Between Columns
591+
*
592+
* @param int $pValue
593+
*/
563594
public function setColsSpace($pValue = '') {
564595
$this->_colsSpace = $pValue;
565596
return $this;
566597
}
567598

568-
public function getBreakType() {
569-
return $this->_breakType;
599+
/**
600+
* Get Section Space Between Columns
601+
*
602+
* @return int
603+
*/
604+
public function getColsSpace() {
605+
return $this->_colsSpace;
570606
}
607+
608+
/**
609+
* Set Break Type
610+
*
611+
* @param string $pValue
612+
*/
571613
public function setBreakType($pValue = null) {
572614
$this->_breakType = $pValue;
573615
return $this;
574616
}
617+
618+
/**
619+
* Get Break Type
620+
*
621+
* @return string
622+
*/
623+
public function getBreakType() {
624+
return $this->_breakType;
625+
}
575626
}

Classes/PHPWord/Style/Paragraph.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public function __construct()
103103
public function setStyleValue($key, $value)
104104
{
105105
if ($key == '_indent') {
106-
$value = $value * 1440 / 2.54; // Measured by cm
106+
$value = (int)$value * 720; // 720 twips per indent
107107
}
108108
if ($key == '_hanging') {
109-
$value = $value * 1440 / 2.54; // Measured by cm
109+
$value =(int)$value * 720;
110110
}
111111
if ($key == '_spacing') {
112112
$value += 240; // because line height of 1 matches 240 twips
@@ -231,16 +231,6 @@ public function setIndent($pValue = null)
231231
return $this;
232232
}
233233

234-
/**
235-
* Get hanging
236-
*
237-
* @return int
238-
*/
239-
public function getHanging()
240-
{
241-
return $this->_hanging;
242-
}
243-
244234
/**
245235
* Set hanging
246236
*
@@ -253,10 +243,20 @@ public function setHanging($pValue = null)
253243
return $this;
254244
}
255245

246+
/**
247+
* Get hanging
248+
*
249+
* @return int
250+
*/
251+
public function getHanging()
252+
{
253+
return $this->_hanging;
254+
}
255+
256256
/**
257257
* Get tabs
258258
*
259-
* @return PHPWord_Style_Tabs
259+
* @return PHPWord_Style_Paragraph
260260
*/
261261
public function getTabs()
262262
{

Classes/PHPWord/Writer/Word2007/Document.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,10 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
240240
$objWriter->startElement('w:cols');
241241
if($colsNum > 1){
242242
$objWriter->writeAttribute('w:num', $colsNum);
243+
$objWriter->writeAttribute('w:space', $colsSpace);
244+
} else {
245+
$objWriter->writeAttribute('w:space', '720');
243246
}
244-
$objWriter->writeAttribute('w:space', $colsSpace);
245247
$objWriter->endElement();
246248

247249

Classes/PHPWord/Writer/Word2007/Styles.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,22 @@ public function writeStyles(PHPWord $pPHPWord = null)
5656
// Write DocDefaults
5757
$this->_writeDocDefaults($objWriter);
5858

59-
// Write Normal
60-
// Start Hack
59+
// Reset Normal Paragraph Style
6160
$objWriter->startElement('w:style');
6261
$objWriter->writeAttribute('w:type', 'paragraph');
6362
$objWriter->writeAttribute('w:default', '1');
6463
$objWriter->writeAttribute('w:styleId', 'Normal');
65-
6664
$objWriter->startElement('w:name');
6765
$objWriter->writeAttribute('w:val', 'Normal');
68-
$objWriter->endElement();
69-
66+
$objWriter->endElement(); // w:name
7067
$objWriter->startElement('w:pPr');
7168
$objWriter->startElement('w:spacing');
72-
$objWriter->writeAttribute('w:before', 20 * 0);
73-
$objWriter->writeAttribute('w:after', 20 * 0);
74-
$objWriter->writeAttribute('w:line', 240 * 1);
75-
$objWriter->endElement();
76-
$objWriter->endElement();
77-
78-
$objWriter->endElement();
79-
// End Hack
69+
$objWriter->writeAttribute('w:before', 0);
70+
$objWriter->writeAttribute('w:after', 0);
71+
$objWriter->writeAttribute('w:line', 240);
72+
$objWriter->endElement(); // w:spacing
73+
$objWriter->endElement(); // w:pPr
74+
$objWriter->endElement(); // w:style
8075

8176
// Write Style Definitions
8277
$styles = PHPWord_Style::getStyles();
@@ -113,7 +108,6 @@ public function writeStyles(PHPWord $pPHPWord = null)
113108
$objWriter->endElement();
114109

115110
if (!is_null($paragraphStyle)) {
116-
// 2013-12-31 11:34 IL
117111
$objWriter->startElement('w:basedOn');
118112
$objWriter->writeAttribute('w:val', 'Normal');
119113
$objWriter->endElement();
@@ -134,7 +128,6 @@ public function writeStyles(PHPWord $pPHPWord = null)
134128
$objWriter->writeAttribute('w:val', $styleName);
135129
$objWriter->endElement();
136130

137-
// 2013-12-31 11:34 IL
138131
$objWriter->startElement('w:basedOn');
139132
$objWriter->writeAttribute('w:val', 'Normal');
140133
$objWriter->endElement();

README.md

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,92 @@
11
# PHPWord - OpenXML - Read, Write and Create Word documents in PHP
22

3-
PHPWord is a library written in PHP that create word documents.
3+
PHPWord is a library written in PHP that create word documents.
4+
No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be
5+
opened by all major office software.
46

5-
## Add features in this fork
7+
## Want to contribute?
8+
Fork us!
69

7-
* Superscript/subscript `w:vertAlign`
8-
* Hanging
9-
* Section with column
10-
* Softbreak (SHIFT + ENTER) in Text Run
11-
* Redefine normal paragraph style and base all other style from it
12-
* Section `breakType`. Used especially for continuous column
10+
## Requirements
1311

14-
## License
12+
* PHP version 5.3.0 or higher
1513

14+
## License
1615
PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md)
16+
17+
## Installation
18+
19+
It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add
20+
the following lines to your ``composer.json``.
21+
22+
```json
23+
{
24+
"require": {
25+
"phpoffice/phpword": "dev-master"
26+
}
27+
}
28+
```
29+
30+
## Usage
31+
32+
The following is a basic example of the PHPWord library.
33+
34+
```php
35+
$PHPWord = new PHPWord();
36+
37+
// Every element you want to append to the word document is placed in a section. So you need a section:
38+
$section = $PHPWord->createSection();
39+
40+
// After creating a section, you can append elements:
41+
$section->addText('Hello world!');
42+
43+
// You can directly style your text by giving the addText function an array:
44+
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
45+
46+
// If you often need the same style again you can create a user defined style to the word document
47+
// and give the addText function the name of the style:
48+
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
49+
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');
50+
51+
// You can also putthe appended element to local object an call functions like this:
52+
$myTextElement = $section->addText('Hello World!');
53+
$myTextElement->setBold();
54+
$myTextElement->setName('Verdana');
55+
$myTextElement->setSize(22);
56+
57+
// At least write the document to webspace:
58+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
59+
$objWriter->save('helloWorld.docx');
60+
```
61+
62+
## Images
63+
64+
You can add images easily using the following example.
65+
66+
```php
67+
$section = $PHPWord->createSection();
68+
$section->addImage('mars.jpg');
69+
```
70+
71+
Images settings include:
72+
* ``width`` width in pixels
73+
* ``height`` height in pixels
74+
* ``align`` image alignment, __left__, __right__ or __center__
75+
* ``marginTop`` top margin in inches, can be negative
76+
* ``marginLeft`` left margin in inches, can be negative
77+
* ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__
78+
79+
To add an image with settings, consider the following example.
80+
81+
```php
82+
$section->addImage(
83+
'mars.jpg',
84+
array(
85+
'width' => 100,
86+
'height' => 100,
87+
'marginTop' => -1,
88+
'marginLeft' => -1,
89+
wrappingStyle => 'behind'
90+
)
91+
);
92+
```

0 commit comments

Comments
 (0)