A pure PHP library for reading and writing word processing documents
Go to file
Ivan Lanin 92588ccfd9 Fix variable for MediaTest 2014-03-28 19:25:12 +07:00
docs Revert project name capitalization #176 2014-03-26 16:33:20 +07:00
samples Method name & code format for PSR/phpdoc compliance 2014-03-24 03:20:18 +07:00
src/PhpWord Unit tests enhancement 2014-03-28 19:00:41 +07:00
tests Fix variable for MediaTest 2014-03-28 19:25:12 +07:00
.gitignore Removed files from gitignore 2014-03-23 12:55:37 -04:00
.travis.yml Changed test directory in travis config 2014-03-24 08:31:58 -04:00
CHANGELOG.md Update CHANGELOG.md 2014-03-28 10:06:11 +07:00
CONTRIBUTING.md (1) Change CHANGELOG format to simplify release notes creation (2) Add CONTRIBUTING guideline 2014-03-23 11:21:59 +07:00
LICENSE.md Unit tests enhancement 2014-03-28 19:00:41 +07:00
README.md Moved the autoloader in the basic usage to the installation section in the read me 2014-03-27 07:55:03 -04:00
composer.json Fix psr-4 base dir in composer.json 2014-03-27 10:04:34 +03:00
phpunit.xml.dist Renamed test directory to tests 2014-03-24 08:23:59 -04:00

README.md

PHPWord

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), and Rich Text Format (RTF).

With PHPWord, you can create DOCX, ODT, or RTF documents dynamically using your PHP 5.3+ scripts. Below are some of the things that you can do with PHPWord library:

  • Set document properties, e.g. title, subject, and creator.
  • Create document sections with different settings, e.g. portrait/landscape, page size, and page numbering
  • Create header and footer for each sections
  • Set default font type, font size, and paragraph style
  • Use UTF-8 and East Asia fonts/characters
  • Define custom font styles (e.g. bold, italic, color) and paragraph styles (e.g. centered, multicolumns, spacing) either as named style or inline in text
  • Insert paragraphs, either as a simple text or complex one (a text run) that contains other elements
  • Insert titles (headers) and table of contents
  • Insert text breaks and page breaks
  • Insert and format images, either local, remote, or as page watermarks
  • Insert binary OLE Objects such as Excel or Visio
  • Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
  • Insert list items as bulleted, numbered, or multilevel
  • Insert hyperlinks
  • Create document from templates
  • Use XSL 1.0 style sheets to transform main document part of OOXML template
  • ... and many more features on progress

Want to contribute? Fork us or submit your bug reports or feature requests to us.

Requirements

Optional PHP extensions

Installation

It is recommended that you install the PHPWord library through composer. To do so, add the following lines to your composer.json.

{
    "require": {
       "phpoffice/phpword": "dev-master"
    }
}

Alternatively, you can download the latest release from the releases page. In this case, you will have to register the autoloader.

require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
PhpOffice\PhpWord\Autoloader::register();

Basic usage

The following is a basic example of the PHPWord library. More examples are provided in the samples folder.

$phpWord = new \PhpOffice\PhpWord\PhpWord();

// Every element you want to append to the word document is placed in a section.
// To create a basic 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 put the appended element to local object like this:
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
$fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);

// Finally, write the document:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF');
$objWriter->save('helloWorld.rtf');

Documentation

Want to know more? Read the full documentation of PHPWord on Read The Docs.