diff --git a/README.md b/README.md index f243f05b..e6e0bf77 100644 --- a/README.md +++ b/README.md @@ -85,44 +85,67 @@ require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php'; The following is a basic usage example of the PHPWord library. ```php +addSection(); - -// 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 = array( - 'name' => 'Verdana', - 'size' => 22, - 'bold' => true, +// Adding Text element to the Section having font styled by default... +$section->addText( + htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)') ); -$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); -// Finally, save the document: -$phpWord->save('helloWorld.docx'); -$phpWord->save('helloWorld.odt', 'ODText'); -$phpWord->save('helloWorld.rtf', 'RTF'); +// Saving the document as OOXML file... +$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); +$objWriter->save('helloWorld.docx'); + +// 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. diff --git a/docs/general.rst b/docs/general.rst index 8fc7130e..b0edb38d 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -12,42 +12,65 @@ folder `__. .. code-block:: php + addSection(); - - // 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 = array( - 'name' => 'Verdana', - 'size' => 22, - 'bold' => true, + // Adding Text element to the Section having font styled by default... + $section->addText( + htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)') ); - $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); - // Finally, save the document: - $phpWord->save('helloWorld.docx'); - $phpWord->save('helloWorld.odt', 'ODText'); - $phpWord->save('helloWorld.rtf', 'RTF'); + // Saving the document as OOXML file... + $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); + $objWriter->save('helloWorld.docx'); + + // 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 -------- diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 3e7d41b0..43436048 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -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/). ```php +addSection(); - -// 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 = array( - 'name' => 'Verdana', - 'size' => 22, - 'bold' => true, +// Adding Text element to the Section having font styled by default... +$section->addText( + htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)') ); -$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); -// Finally, save the document: -$phpWord->save('helloWorld.docx'); -$phpWord->save('helloWorld.odt', 'ODText'); -$phpWord->save('helloWorld.rtf', 'RTF'); +// Saving the document as OOXML file... +$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); +$objWriter->save('helloWorld.docx'); + +// 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 diff --git a/src/PhpWord/Writer/HTML/Element/ListItem.php b/src/PhpWord/Writer/HTML/Element/ListItem.php index 79a3b393..ef8eb34d 100644 --- a/src/PhpWord/Writer/HTML/Element/ListItem.php +++ b/src/PhpWord/Writer/HTML/Element/ListItem.php @@ -35,7 +35,7 @@ class ListItem extends AbstractElement return ''; } - $text = htmlspecialchars($this->element->getTextObject()->getText()); + $text = $this->element->getTextObject()->getText(); $content = '

' . $text . '

' . PHP_EOL; return $content; diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index c2d4134a..0c31df36 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -72,7 +72,7 @@ class Text extends AbstractElement $content .= $this->writeOpening(); $content .= $this->openingText; $content .= $this->openingTags; - $content .= htmlspecialchars($element->getText()); + $content .= $element->getText(); $content .= $this->closingTags; $content .= $this->closingText; $content .= $this->writeClosing(); diff --git a/src/PhpWord/Writer/HTML/Element/Title.php b/src/PhpWord/Writer/HTML/Element/Title.php index 20747bf9..c054ccf9 100644 --- a/src/PhpWord/Writer/HTML/Element/Title.php +++ b/src/PhpWord/Writer/HTML/Element/Title.php @@ -36,7 +36,7 @@ class Title extends AbstractElement } $tag = 'h' . $this->element->getDepth(); - $text = htmlspecialchars($this->element->getText()); + $text = $this->element->getText(); $content = "<{$tag}>{$text}" . PHP_EOL; return $content; diff --git a/src/PhpWord/Writer/HTML/Part/Head.php b/src/PhpWord/Writer/HTML/Part/Head.php index 7339c74c..503f75b8 100644 --- a/src/PhpWord/Writer/HTML/Part/Head.php +++ b/src/PhpWord/Writer/HTML/Part/Head.php @@ -18,9 +18,9 @@ namespace PhpOffice\PhpWord\Writer\HTML\Part; use PhpOffice\PhpWord\Settings; +use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Paragraph; -use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter; use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter; @@ -57,13 +57,13 @@ class Head extends AbstractPart $content .= '' . PHP_EOL; $content .= '' . PHP_EOL; - $content .= '' . htmlspecialchars($title) . '' . PHP_EOL; + $content .= '' . $title . '' . PHP_EOL; foreach ($propertiesMapping as $key => $value) { $value = ($value == '') ? $key : $value; $method = "get" . $key; if ($docProps->$method() != '') { $content .= '' . PHP_EOL; + $docProps->$method() . '" />' . PHP_EOL; } } $content .= $this->writeStyles();