Roman Syroeshko 2014-12-09 21:35:26 +04:00
parent f8ee230495
commit 2cb124f5b1
7 changed files with 157 additions and 88 deletions

View File

@ -85,44 +85,67 @@ require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
The following is a basic usage example of the PHPWord library. The following is a basic usage example of the PHPWord library.
```php ```php
<?php
require_once 'src/PhpWord/Autoloader.php'; require_once 'src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); \PhpOffice\PhpWord\Autoloader::register();
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
// Every element you want to append to the word document is placed in a section. /* Note: any element you append to a document must reside inside of a Section. */
// To create a basic section:
// Adding an empty Section to the document...
$section = $phpWord->addSection(); $section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
// After creating a section, you can append elements: $section->addText(
$section->addText('Hello world!'); htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)')
// 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 = array(
'name' => 'Verdana',
'size' => 22,
'bold' => true,
); );
$myTextElement = $section->addText('Hello World!');
/*
* Note: it is possible to customize font style of the Text element you add in three ways:
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
// Adding Text element having font customized inline...
$section->addText(
htmlspecialchars('"Great achievement is usually born of great sacrifice, and is never the result of selfishness." (Napoleon Hill)'),
array('name' => 'Tahoma', 'size' => 10)
);
// Adding Text element having font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle($fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
$section->addText(
htmlspecialchars('"The greatest accomplishment is not in never falling, but in rising again after you fall." (Vince Lombardi)'),
$fontStyleName
);
// Adding Text element having font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText(htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'));
$myTextElement->setFontStyle($fontStyle); $myTextElement->setFontStyle($fontStyle);
// Finally, save the document: // Saving the document as OOXML file...
$phpWord->save('helloWorld.docx'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$phpWord->save('helloWorld.odt', 'ODText'); $objWriter->save('helloWorld.docx');
$phpWord->save('helloWorld.rtf', 'RTF');
// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');
/* Note: RTF was skipped here, because the format is not XML-based and requires a bit different example. */
/* Note: PDF was skipped here, because we use "HTML-to-PDF" approach to create PDF documents. */
``` ```
:warning: Escape any string you pass to your document, otherwise it may get broken. :warning: Escape any string you pass to OOXML/ODF/HTML document, otherwise it may get broken.
More examples are provided in the [samples folder](samples/). You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) and the [API Documentation](http://phpoffice.github.io/PHPWord/docs/master/) for more detail. More examples are provided in the [samples folder](samples/). You can also read the [Developers' Documentation](http://phpword.readthedocs.org/) and the [API Documentation](http://phpoffice.github.io/PHPWord/docs/master/) for more detail.

View File

@ -12,42 +12,65 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
.. code-block:: php .. code-block:: php
<?php
require_once 'src/PhpWord/Autoloader.php'; require_once 'src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); \PhpOffice\PhpWord\Autoloader::register();
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
// Every element you want to append to the word document is placed in a section. /* Note: any element you append to a document must reside inside of a Section. */
// To create a basic section:
// Adding an empty Section to the document...
$section = $phpWord->addSection(); $section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
// After creating a section, you can append elements: $section->addText(
$section->addText('Hello world!'); htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)')
// 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 = array(
'name' => 'Verdana',
'size' => 22,
'bold' => true,
); );
$myTextElement = $section->addText('Hello World!');
/*
* Note: it is possible to customize font style of the Text element you add in three ways:
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
// Adding Text element having font customized inline...
$section->addText(
htmlspecialchars('"Great achievement is usually born of great sacrifice, and is never the result of selfishness." (Napoleon Hill)'),
array('name' => 'Tahoma', 'size' => 10)
);
// Adding Text element having font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle($fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
$section->addText(
htmlspecialchars('"The greatest accomplishment is not in never falling, but in rising again after you fall." (Vince Lombardi)'),
$fontStyleName
);
// Adding Text element having font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText(htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'));
$myTextElement->setFontStyle($fontStyle); $myTextElement->setFontStyle($fontStyle);
// Finally, save the document: // Saving the document as OOXML file...
$phpWord->save('helloWorld.docx'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$phpWord->save('helloWorld.odt', 'ODText'); $objWriter->save('helloWorld.docx');
$phpWord->save('helloWorld.rtf', 'RTF');
// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');
/* Note: RTF was skipped here, because the format is not XML-based and requires a bit different example. */
/* Note: PDF was skipped here, because we use "HTML-to-PDF" approach to create PDF documents. */
Settings Settings
-------- --------

View File

@ -212,42 +212,65 @@ After installation, you can browse and use the samples that we've provided, eith
The following is a basic example of the PHPWord library. More examples are provided in the [samples folder](https://github.com/PHPOffice/PHPWord/tree/master/samples/). The following is a basic example of the PHPWord library. More examples are provided in the [samples folder](https://github.com/PHPOffice/PHPWord/tree/master/samples/).
```php ```php
<?php
require_once 'src/PhpWord/Autoloader.php'; require_once 'src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); \PhpOffice\PhpWord\Autoloader::register();
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
// Every element you want to append to the word document is placed in a section. /* Note: any element you append to a document must reside inside of a Section. */
// To create a basic section:
// Adding an empty Section to the document...
$section = $phpWord->addSection(); $section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
// After creating a section, you can append elements: $section->addText(
$section->addText('Hello world!'); htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)')
// 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 = array(
'name' => 'Verdana',
'size' => 22,
'bold' => true,
); );
$myTextElement = $section->addText('Hello World!');
/*
* Note: it is possible to customize font style of the Text element you add in three ways:
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
// Adding Text element having font customized inline...
$section->addText(
htmlspecialchars('"Great achievement is usually born of great sacrifice, and is never the result of selfishness." (Napoleon Hill)'),
array('name' => 'Tahoma', 'size' => 10)
);
// Adding Text element having font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle($fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
$section->addText(
htmlspecialchars('"The greatest accomplishment is not in never falling, but in rising again after you fall." (Vince Lombardi)'),
$fontStyleName
);
// Adding Text element having font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText(htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'));
$myTextElement->setFontStyle($fontStyle); $myTextElement->setFontStyle($fontStyle);
// Finally, save the document: // Saving the document as OOXML file...
$phpWord->save('helloWorld.docx'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$phpWord->save('helloWorld.odt', 'ODText'); $objWriter->save('helloWorld.docx');
$phpWord->save('helloWorld.rtf', 'RTF');
// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');
/* Note: RTF was skipped here, because the format is not XML-based and requires a bit different example. */
/* Note: PDF was skipped here, because we use "HTML-to-PDF" approach to create PDF documents. */
``` ```
## Settings ## Settings

View File

@ -35,7 +35,7 @@ class ListItem extends AbstractElement
return ''; return '';
} }
$text = htmlspecialchars($this->element->getTextObject()->getText()); $text = $this->element->getTextObject()->getText();
$content = '<p>' . $text . '</p>' . PHP_EOL; $content = '<p>' . $text . '</p>' . PHP_EOL;
return $content; return $content;

View File

@ -72,7 +72,7 @@ class Text extends AbstractElement
$content .= $this->writeOpening(); $content .= $this->writeOpening();
$content .= $this->openingText; $content .= $this->openingText;
$content .= $this->openingTags; $content .= $this->openingTags;
$content .= htmlspecialchars($element->getText()); $content .= $element->getText();
$content .= $this->closingTags; $content .= $this->closingTags;
$content .= $this->closingText; $content .= $this->closingText;
$content .= $this->writeClosing(); $content .= $this->writeClosing();

View File

@ -36,7 +36,7 @@ class Title extends AbstractElement
} }
$tag = 'h' . $this->element->getDepth(); $tag = 'h' . $this->element->getDepth();
$text = htmlspecialchars($this->element->getText()); $text = $this->element->getText();
$content = "<{$tag}>{$text}</{$tag}>" . PHP_EOL; $content = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;
return $content; return $content;

View File

@ -18,9 +18,9 @@
namespace PhpOffice\PhpWord\Writer\HTML\Part; namespace PhpOffice\PhpWord\Writer\HTML\Part;
use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph; use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
@ -57,13 +57,13 @@ class Head extends AbstractPart
$content .= '<head>' . PHP_EOL; $content .= '<head>' . PHP_EOL;
$content .= '<meta charset="UTF-8" />' . PHP_EOL; $content .= '<meta charset="UTF-8" />' . PHP_EOL;
$content .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL; $content .= '<title>' . $title . '</title>' . PHP_EOL;
foreach ($propertiesMapping as $key => $value) { foreach ($propertiesMapping as $key => $value) {
$value = ($value == '') ? $key : $value; $value = ($value == '') ? $key : $value;
$method = "get" . $key; $method = "get" . $key;
if ($docProps->$method() != '') { if ($docProps->$method() != '') {
$content .= '<meta name="' . $value . '" content="' . $content .= '<meta name="' . $value . '" content="' .
htmlspecialchars($docProps->$method()) . '" />' . PHP_EOL; $docProps->$method() . '" />' . PHP_EOL;
} }
} }
$content .= $this->writeStyles(); $content .= $this->writeStyles();