This commit is contained in:
Ivan Lanin 2014-03-07 00:32:00 +07:00
parent 938d78f40b
commit 136127894a

286
README.md
View File

@ -1,143 +1,143 @@
# PHPWord # PHPWord
[![Build Status](https://travis-ci.org/PHPOffice/PHPWord.png?branch=master)](https://travis-ci.org/PHPOffice/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) [![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.__ __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 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. opened by all major office software.
__Want to contribute?__ Fork us! __Want to contribute?__ Fork us!
## Requirements ## Requirements
* PHP version 5.3.0 or higher * PHP version 5.3.0 or higher
## Installation ## Installation
It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add 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``. the following lines to your ``composer.json``.
```json ```json
{ {
"require": { "require": {
"phpoffice/phpword": "dev-master" "phpoffice/phpword": "dev-master"
} }
} }
``` ```
## Documentation ## Documentation
### Table of contents ### Table of contents
1. [Basic usage](#basic-usage) 1. [Basic usage](#basic-usage)
2. [Sections](#sections) 2. [Sections](#sections)
* [Change Section Page Numbering](#sections-page-numbering) * [Change Section Page Numbering](#sections-page-numbering)
3. [Tables](#tables) 3. [Tables](#tables)
* [Cell Style](#tables-cell-style) * [Cell Style](#tables-cell-style)
4. [Images](#images) 4. [Images](#images)
<a name="basic-usage"></a> <a name="basic-usage"></a>
#### Basic usage #### Basic usage
The following is a basic example of the PHPWord library. The following is a basic example of the PHPWord library.
```php ```php
$PHPWord = new PHPWord(); $PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section: // Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
// After creating a section, you can append elements: // After creating a section, you can append elements:
$section->addText('Hello world!'); $section->addText('Hello world!');
// You can directly style your text by giving the addText function an array: // 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)); $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 // 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: // and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); $PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); $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: // You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!'); $myTextElement = $section->addText('Hello World!');
$myTextElement->setBold(); $myTextElement->setBold();
$myTextElement->setName('Verdana'); $myTextElement->setName('Verdana');
$myTextElement->setSize(22); $myTextElement->setSize(22);
// At least write the document to webspace: // At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx'); $objWriter->save('helloWorld.docx');
``` ```
<a name="sections"></a> <a name="sections"></a>
#### Sections #### Sections
<a name="sections-page-numbering"></a> <a name="sections-page-numbering"></a>
##### Change Section Page Numbering ##### Change Section Page Numbering
You can change a section page numbering. You can change a section page numbering.
```php ```php
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
$section->getSettings()->setPageNumberingStart(1); $section->getSettings()->setPageNumberingStart(1);
``` ```
<a name="tables"></a> <a name="tables"></a>
#### Tables #### Tables
The following illustrates how to create a table. The following illustrates how to create a table.
```php ```php
$table = $section->addTable(); $table = $section->addTable();
$table->addRow(); $table->addRow();
$table->addCell(); $table->addCell();
``` ```
<a name="tables-cell-style"></a> <a name="tables-cell-style"></a>
##### Cell Style ##### Cell Style
###### Cell Span ###### Cell Span
You can span a cell on multiple columms. You can span a cell on multiple columms.
```php ```php
$cell = $table->addCell(200); $cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5); $cell->getStyle()->setGridSpan(5);
``` ```
<a name="images"></a> <a name="images"></a>
#### Images #### Images
You can add images easily using the following example. You can add images easily using the following example.
```php ```php
$section = $PHPWord->createSection(); $section = $PHPWord->createSection();
$section->addImage('mars.jpg'); $section->addImage('mars.jpg');
``` ```
Images settings include: Images settings include:
* ``width`` width in pixels * ``width`` width in pixels
* ``height`` height in pixels * ``height`` height in pixels
* ``align`` image alignment, _left_, _right_ or _center_ * ``align`` image alignment, _left_, _right_ or _center_
* ``marginTop`` top margin in inches, can be negative * ``marginTop`` top margin in inches, can be negative
* ``marginLeft`` left margin in inches, can be negative * ``marginLeft`` left margin in inches, can be negative
* ``wrappingStyle`` can be _inline_, _square_, _tight_, _behind_, _infront_ * ``wrappingStyle`` can be _inline_, _square_, _tight_, _behind_, _infront_
To add an image with settings, consider the following example. To add an image with settings, consider the following example.
```php ```php
$section->addImage( $section->addImage(
'mars.jpg', 'mars.jpg',
array( array(
'width' => 100, 'width' => 100,
'height' => 100, 'height' => 100,
'marginTop' => -1, 'marginTop' => -1,
'marginLeft' => -1, 'marginLeft' => -1,
'wrappingStyle' => 'behind' 'wrappingStyle' => 'behind'
) )
); );
``` ```