diff --git a/README.md b/README.md index d796dba3..d46ee8d1 100755 --- a/README.md +++ b/README.md @@ -29,7 +29,73 @@ With PHPWord, you can create DOCX, ODT, or RTF documents dynamically using your * Use XSL 1.0 style sheets to transform main document part of OOXML template * ... and many more features on progress +## Requirements +* PHP 5.3+ +* PHP [Zip](http://php.net/manual/en/book.zip.php) extension +* PHP [XML Parser](http://www.php.net/manual/en/xml.installation.php) extension + +### Optional PHP extensions +* PHP [GD](http://php.net/manual/en/book.image.php) extension +* PHP [XMLWriter](http://php.net/manual/en/book.xmlwriter.php) extension +* PHP [XSL](http://php.net/manual/en/book.xsl.php) extension + +## 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" + } +} +``` + +## Basic usage + +The following is a basic example of the PHPWord library. More examples are provided in the [samples folder](samples/). + +```php +$PHPWord = new 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 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 = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save('helloWorld.docx'); + +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save('helloWorld.odt'); + +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save('helloWorld.rtf'); +``` + __Want to contribute?__ [Fork us](https://github.com/PHPOffice/PHPWord/fork) or [submit](https://github.com/PHPOffice/PHPWord/issues) your bug reports or feature requests to us. __Want to know more?__ Read the full documentation of PHPWord on [Read The Docs](http://phpword.readthedocs.org/en/develop/). - diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 4eca69db..ecd9017d 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -28,7 +28,6 @@ namespace PhpOffice\PhpWord\Reader; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\DocumentProperties; use PhpOffice\PhpWord\Exceptions\Exception; -use PhpOffice\PhpWord\Shared\File; /** * Reader for Word2007 @@ -84,10 +83,10 @@ class Word2007 extends AbstractReader implements IReader public function getFromZipArchive($archive, $fileName = '', $removeNamespace = false) { // Root-relative paths - if (strpos($fileName, '//') !== false) { - $fileName = substr($fileName, strpos($fileName, '//') + 1); - } - $fileName = File::realpath($fileName); + // if (strpos($fileName, '//') !== false) { + // $fileName = substr($fileName, strpos($fileName, '//') + 1); + // } + // $fileName = realpath($fileName); // Apache POI fixes $contents = $archive->getFromName($fileName); diff --git a/src/PhpWord/Shared/File.php b/src/PhpWord/Shared/File.php deleted file mode 100755 index 90309a1b..00000000 --- a/src/PhpWord/Shared/File.php +++ /dev/null @@ -1,77 +0,0 @@ - 0) { - unset($pathArray[$i]); - unset($pathArray[$i - 1]); - break; - } - } - } - $returnValue = implode('/', $pathArray); - } - - // Return - return $returnValue; - } -} diff --git a/src/PhpWord/Writer/ODText/Manifest.php b/src/PhpWord/Writer/ODText/Manifest.php index a7071810..e19bcb18 100755 --- a/src/PhpWord/Writer/ODText/Manifest.php +++ b/src/PhpWord/Writer/ODText/Manifest.php @@ -27,7 +27,6 @@ namespace PhpOffice\PhpWord\Writer\ODText; use PhpOffice\PhpWord\Exceptions\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\File; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -123,7 +122,7 @@ class Manifest extends WriterPart */ private function _getImageMimeType($pFile = '') { - if (File::fileExists($pFile)) { + if (file_exists($pFile)) { $image = getimagesize($pFile); return image_type_to_mime_type($image[2]); } else { diff --git a/src/PhpWord/Writer/Word2007/ContentTypes.php b/src/PhpWord/Writer/Word2007/ContentTypes.php index 2f6ab4e1..0d61dfcb 100755 --- a/src/PhpWord/Writer/Word2007/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/ContentTypes.php @@ -26,7 +26,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007; use PhpOffice\PhpWord\Exceptions\Exception; -use PhpOffice\PhpWord\Shared\File; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -189,7 +188,7 @@ class ContentTypes extends WriterPart */ private function _getImageMimeType($pFile = '') { - if (File::fileExists($pFile)) { + if (file_exists($pFile)) { $image = getimagesize($pFile); return image_type_to_mime_type($image[2]); } else { diff --git a/tests/PhpWord/Tests/PhpWordTest.php b/tests/PhpWord/Tests/PhpWordTest.php index 199e96b9..bcabcfb3 100644 --- a/tests/PhpWord/Tests/PhpWordTest.php +++ b/tests/PhpWord/Tests/PhpWordTest.php @@ -147,7 +147,7 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase { $templateFqfn = \join( \DIRECTORY_SEPARATOR, - array(\PHPWORD_TESTS_BASE_DIR, 'data', 'templates', 'blanks.docx') + array(\PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', 'data', 'templates', 'blanks.docx') ); $phpWord = new PhpWord(); $phpWord->loadTemplate($templateFqfn); diff --git a/tests/PhpWord/Tests/Reader/Word2007Test.php b/tests/PhpWord/Tests/Reader/Word2007Test.php index 1afbb1b1..f372a9e2 100644 --- a/tests/PhpWord/Tests/Reader/Word2007Test.php +++ b/tests/PhpWord/Tests/Reader/Word2007Test.php @@ -18,10 +18,12 @@ class Word2007Test extends \PHPUnit_Framework_TestCase */ public function testCanRead() { - $dir = __DIR__ . "/../_files/documents"; - $object = new Word2007; - $file = $dir . \DIRECTORY_SEPARATOR . 'reader.docx'; - $this->assertTrue($object->canRead($file)); + $object = new Word2007(); + $fqFilename = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', 'documents', 'reader.docx') + ); + $this->assertTrue($object->canRead($fqFilename)); } /** @@ -29,21 +31,22 @@ class Word2007Test extends \PHPUnit_Framework_TestCase */ public function testCanReadFailed() { - $dir = __DIR__ . "/../_files/documents"; - $object = new Word2007; - $file = $dir . \DIRECTORY_SEPARATOR . 'foo.docx'; - $this->assertFalse($object->canRead($file)); - $object = IOFactory::load($file); + $object = new Word2007(); + $fqFilename = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', 'documents', 'foo.docx') + ); + $this->assertFalse($object->canRead($fqFilename)); + $object = IOFactory::load($fqFilename); } - /** - * Test load document - */ public function testLoad() { - $dir = __DIR__ . "/../_files/documents"; - $file = $dir . \DIRECTORY_SEPARATOR . 'reader.docx'; - $object = IOFactory::load($file); + $fqFilename = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', 'documents', 'reader.docx') + ); + $object = IOFactory::load($fqFilename); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object); } } diff --git a/tests/PhpWord/Tests/Shared/FileTest.php b/tests/PhpWord/Tests/Shared/FileTest.php deleted file mode 100644 index 83764748..00000000 --- a/tests/PhpWord/Tests/Shared/FileTest.php +++ /dev/null @@ -1,42 +0,0 @@ -assertTrue(File::fileExists('blank.docx')); - } - /** - * Test file_exists() - */ - public function testNoFileExists() - { - $dir = __DIR__ . "/../_files/templates"; - chdir($dir); - $this->assertFalse(File::fileExists('404.docx')); - } - - /** - * Test realpath() - */ - public function testRealpath() - { - $dir = realpath(__DIR__ . "/../_files/templates"); - chdir($dir); - $file = 'blank.docx'; - $expected = $dir . \DIRECTORY_SEPARATOR . $file; - $this->assertEquals($expected, File::realpath($file)); - } -} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d3351272..1e7de79f 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,7 +3,7 @@ date_default_timezone_set('UTC'); // defining base dir for tests if (!defined('PHPWORD_TESTS_BASE_DIR')) { - define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__ . '/..')); + define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__)); } $vendor = realpath(__DIR__ . '/../vendor');