Merge remote-tracking branch 'upstream/develop' into #160-refactoring
This commit is contained in:
commit
3055a0e6fc
68
README.md
68
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
|
* Use XSL 1.0 style sheets to transform main document part of OOXML template
|
||||||
* ... and many more features on progress
|
* ... 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 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/).
|
__Want to know more?__ Read the full documentation of PHPWord on [Read The Docs](http://phpword.readthedocs.org/en/develop/).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ namespace PhpOffice\PhpWord\Reader;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
use PhpOffice\PhpWord\DocumentProperties;
|
use PhpOffice\PhpWord\DocumentProperties;
|
||||||
use PhpOffice\PhpWord\Exceptions\Exception;
|
use PhpOffice\PhpWord\Exceptions\Exception;
|
||||||
use PhpOffice\PhpWord\Shared\File;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reader for Word2007
|
* Reader for Word2007
|
||||||
|
|
@ -84,10 +83,10 @@ class Word2007 extends AbstractReader implements IReader
|
||||||
public function getFromZipArchive($archive, $fileName = '', $removeNamespace = false)
|
public function getFromZipArchive($archive, $fileName = '', $removeNamespace = false)
|
||||||
{
|
{
|
||||||
// Root-relative paths
|
// Root-relative paths
|
||||||
if (strpos($fileName, '//') !== false) {
|
// if (strpos($fileName, '//') !== false) {
|
||||||
$fileName = substr($fileName, strpos($fileName, '//') + 1);
|
// $fileName = substr($fileName, strpos($fileName, '//') + 1);
|
||||||
}
|
// }
|
||||||
$fileName = File::realpath($fileName);
|
// $fileName = realpath($fileName);
|
||||||
|
|
||||||
// Apache POI fixes
|
// Apache POI fixes
|
||||||
$contents = $archive->getFromName($fileName);
|
$contents = $archive->getFromName($fileName);
|
||||||
|
|
|
||||||
|
|
@ -1,77 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* PhpWord
|
|
||||||
*
|
|
||||||
* Copyright (c) 2014 PhpWord
|
|
||||||
*
|
|
||||||
* This library is free software; you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
|
||||||
* License as published by the Free Software Foundation; either
|
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This library is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with this library; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2014 PhpWord
|
|
||||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
|
||||||
* @version 0.8.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Shared;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Common file functions
|
|
||||||
*/
|
|
||||||
class File
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Verify if a file exists
|
|
||||||
*
|
|
||||||
* @param string $pFilename Filename
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function fileExists($pFilename)
|
|
||||||
{
|
|
||||||
// Regular file_exists
|
|
||||||
return \file_exists($pFilename);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns canonicalized absolute pathname, also for ZIP archives
|
|
||||||
*
|
|
||||||
* @param string $pFilename
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function realpath($pFilename)
|
|
||||||
{
|
|
||||||
// Returnvalue
|
|
||||||
$returnValue = '';
|
|
||||||
|
|
||||||
// Try using realpath()
|
|
||||||
$returnValue = realpath($pFilename);
|
|
||||||
|
|
||||||
// Found something?
|
|
||||||
if ($returnValue == '' || is_null($returnValue)) {
|
|
||||||
$pathArray = explode('/', $pFilename);
|
|
||||||
while (in_array('..', $pathArray) && $pathArray[0] != '..') {
|
|
||||||
for ($i = 0; $i < count($pathArray); ++$i) {
|
|
||||||
if ($pathArray[$i] == '..' && $i > 0) {
|
|
||||||
unset($pathArray[$i]);
|
|
||||||
unset($pathArray[$i - 1]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$returnValue = implode('/', $pathArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return
|
|
||||||
return $returnValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -27,7 +27,6 @@ namespace PhpOffice\PhpWord\Writer\ODText;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exceptions\Exception;
|
use PhpOffice\PhpWord\Exceptions\Exception;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
use PhpOffice\PhpWord\Shared\File;
|
|
||||||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -123,7 +122,7 @@ class Manifest extends WriterPart
|
||||||
*/
|
*/
|
||||||
private function _getImageMimeType($pFile = '')
|
private function _getImageMimeType($pFile = '')
|
||||||
{
|
{
|
||||||
if (File::fileExists($pFile)) {
|
if (file_exists($pFile)) {
|
||||||
$image = getimagesize($pFile);
|
$image = getimagesize($pFile);
|
||||||
return image_type_to_mime_type($image[2]);
|
return image_type_to_mime_type($image[2]);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@
|
||||||
namespace PhpOffice\PhpWord\Writer\Word2007;
|
namespace PhpOffice\PhpWord\Writer\Word2007;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exceptions\Exception;
|
use PhpOffice\PhpWord\Exceptions\Exception;
|
||||||
use PhpOffice\PhpWord\Shared\File;
|
|
||||||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -189,7 +188,7 @@ class ContentTypes extends WriterPart
|
||||||
*/
|
*/
|
||||||
private function _getImageMimeType($pFile = '')
|
private function _getImageMimeType($pFile = '')
|
||||||
{
|
{
|
||||||
if (File::fileExists($pFile)) {
|
if (file_exists($pFile)) {
|
||||||
$image = getimagesize($pFile);
|
$image = getimagesize($pFile);
|
||||||
return image_type_to_mime_type($image[2]);
|
return image_type_to_mime_type($image[2]);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -147,7 +147,7 @@ class PhpWordTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
$templateFqfn = \join(
|
$templateFqfn = \join(
|
||||||
\DIRECTORY_SEPARATOR,
|
\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 = new PhpWord();
|
||||||
$phpWord->loadTemplate($templateFqfn);
|
$phpWord->loadTemplate($templateFqfn);
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,12 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
public function testCanRead()
|
public function testCanRead()
|
||||||
{
|
{
|
||||||
$dir = __DIR__ . "/../_files/documents";
|
$object = new Word2007();
|
||||||
$object = new Word2007;
|
$fqFilename = join(
|
||||||
$file = $dir . \DIRECTORY_SEPARATOR . 'reader.docx';
|
DIRECTORY_SEPARATOR,
|
||||||
$this->assertTrue($object->canRead($file));
|
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()
|
public function testCanReadFailed()
|
||||||
{
|
{
|
||||||
$dir = __DIR__ . "/../_files/documents";
|
$object = new Word2007();
|
||||||
$object = new Word2007;
|
$fqFilename = join(
|
||||||
$file = $dir . \DIRECTORY_SEPARATOR . 'foo.docx';
|
DIRECTORY_SEPARATOR,
|
||||||
$this->assertFalse($object->canRead($file));
|
array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', 'documents', 'foo.docx')
|
||||||
$object = IOFactory::load($file);
|
);
|
||||||
|
$this->assertFalse($object->canRead($fqFilename));
|
||||||
|
$object = IOFactory::load($fqFilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test load document
|
|
||||||
*/
|
|
||||||
public function testLoad()
|
public function testLoad()
|
||||||
{
|
{
|
||||||
$dir = __DIR__ . "/../_files/documents";
|
$fqFilename = join(
|
||||||
$file = $dir . \DIRECTORY_SEPARATOR . 'reader.docx';
|
DIRECTORY_SEPARATOR,
|
||||||
$object = IOFactory::load($file);
|
array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', 'documents', 'reader.docx')
|
||||||
|
);
|
||||||
|
$object = IOFactory::load($fqFilename);
|
||||||
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object);
|
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace PhpOffice\PhpWord\Tests\Shared;
|
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Shared\File;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @coversDefaultClass \PhpOffice\PhpWord\Shared\File
|
|
||||||
* @runTestsInSeparateProcesses
|
|
||||||
*/
|
|
||||||
class FileTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Test file_exists()
|
|
||||||
*/
|
|
||||||
public function testFileExists()
|
|
||||||
{
|
|
||||||
$dir = __DIR__ . "/../_files/templates";
|
|
||||||
chdir($dir);
|
|
||||||
$this->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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -3,7 +3,7 @@ date_default_timezone_set('UTC');
|
||||||
|
|
||||||
// defining base dir for tests
|
// defining base dir for tests
|
||||||
if (!defined('PHPWORD_TESTS_BASE_DIR')) {
|
if (!defined('PHPWORD_TESTS_BASE_DIR')) {
|
||||||
define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__ . '/..'));
|
define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__));
|
||||||
}
|
}
|
||||||
|
|
||||||
$vendor = realpath(__DIR__ . '/../vendor');
|
$vendor = realpath(__DIR__ . '/../vendor');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue