Skip to content

Commit 2cb124f

Browse files
author
Roman Syroeshko
committed
https://github.com/PHPOffice/PHPWord/issues/51
1 parent f8ee230 commit 2cb124f

File tree

7 files changed

+154
-85
lines changed

7 files changed

+154
-85
lines changed

README.md

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,67 @@ require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
8585
The following is a basic usage example of the PHPWord library.
8686

8787
```php
88+
<?php
8889
require_once 'src/PhpWord/Autoloader.php';
8990
\PhpOffice\PhpWord\Autoloader::register();
9091

92+
// Creating the new document...
9193
$phpWord = new \PhpOffice\PhpWord\PhpWord();
9294

93-
// Every element you want to append to the word document is placed in a section.
94-
// To create a basic section:
95+
/* Note: any element you append to a document must reside inside of a Section. */
96+
97+
// Adding an empty Section to the document...
9598
$section = $phpWord->addSection();
99+
// Adding Text element to the Section having font styled by default...
100+
$section->addText(
101+
htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)')
102+
);
103+
104+
/*
105+
* Note: it is possible to customize font style of the Text element you add in three ways:
106+
* - inline;
107+
* - using named font style (new font style object will be implicitly created);
108+
* - using explicitly created font style object.
109+
*/
110+
111+
// Adding Text element having font customized inline...
112+
$section->addText(
113+
htmlspecialchars('"Great achievement is usually born of great sacrifice, and is never the result of selfishness." (Napoleon Hill)'),
114+
array('name' => 'Tahoma', 'size' => 10)
115+
);
96116

97-
// After creating a section, you can append elements:
98-
$section->addText('Hello world!');
99-
100-
// You can directly style your text by giving the addText function an array:
101-
$section->addText('Hello world! I am formatted.',
102-
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
103-
104-
// If you often need the same style again you can create a user defined style
105-
// to the word document and give the addText function the name of the style:
106-
$phpWord->addFontStyle('myOwnStyle',
107-
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
108-
$section->addText('Hello world! I am formatted by a user defined style',
109-
'myOwnStyle');
110-
111-
// You can also put the appended element to local object like this:
112-
$fontStyle = array(
113-
'name' => 'Verdana',
114-
'size' => 22,
115-
'bold' => true,
117+
// Adding Text element having font customized using named font style...
118+
$fontStyleName = 'oneUserDefinedStyle';
119+
$phpWord->addFontStyle($fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
120+
$section->addText(
121+
htmlspecialchars('"The greatest accomplishment is not in never falling, but in rising again after you fall." (Vince Lombardi)'),
122+
$fontStyleName
116123
);
117-
$myTextElement = $section->addText('Hello World!');
124+
125+
// Adding Text element having font customized using explicitly created font style object...
126+
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
127+
$fontStyle->setBold(true);
128+
$fontStyle->setName('Tahoma');
129+
$fontStyle->setSize(13);
130+
$myTextElement = $section->addText(htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'));
118131
$myTextElement->setFontStyle($fontStyle);
119132

120-
// Finally, save the document:
121-
$phpWord->save('helloWorld.docx');
122-
$phpWord->save('helloWorld.odt', 'ODText');
123-
$phpWord->save('helloWorld.rtf', 'RTF');
133+
// Saving the document as OOXML file...
134+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
135+
$objWriter->save('helloWorld.docx');
136+
137+
// Saving the document as ODF file...
138+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
139+
$objWriter->save('helloWorld.odt');
140+
141+
// Saving the document as HTML file...
142+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
143+
$objWriter->save('helloWorld.html');
144+
145+
/* Note: RTF was skipped here, because the format is not XML-based and requires a bit different example. */
146+
/* Note: PDF was skipped here, because we use "HTML-to-PDF" approach to create PDF documents. */
124147
```
125-
:warning: Escape any string you pass to your document, otherwise it may get broken.
148+
:warning: Escape any string you pass to OOXML/ODF/HTML document, otherwise it may get broken.
126149

127150
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.
128151

docs/general.rst

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,65 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
1212

1313
.. code-block:: php
1414
15+
<?php
1516
require_once 'src/PhpWord/Autoloader.php';
1617
\PhpOffice\PhpWord\Autoloader::register();
1718
19+
// Creating the new document...
1820
$phpWord = new \PhpOffice\PhpWord\PhpWord();
1921
20-
// Every element you want to append to the word document is placed in a section.
21-
// To create a basic section:
22+
/* Note: any element you append to a document must reside inside of a Section. */
23+
24+
// Adding an empty Section to the document...
2225
$section = $phpWord->addSection();
26+
// Adding Text element to the Section having font styled by default...
27+
$section->addText(
28+
htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)')
29+
);
30+
31+
/*
32+
* Note: it is possible to customize font style of the Text element you add in three ways:
33+
* - inline;
34+
* - using named font style (new font style object will be implicitly created);
35+
* - using explicitly created font style object.
36+
*/
37+
38+
// Adding Text element having font customized inline...
39+
$section->addText(
40+
htmlspecialchars('"Great achievement is usually born of great sacrifice, and is never the result of selfishness." (Napoleon Hill)'),
41+
array('name' => 'Tahoma', 'size' => 10)
42+
);
2343
24-
// After creating a section, you can append elements:
25-
$section->addText('Hello world!');
26-
27-
// You can directly style your text by giving the addText function an array:
28-
$section->addText('Hello world! I am formatted.',
29-
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
30-
31-
// If you often need the same style again you can create a user defined style
32-
// to the word document and give the addText function the name of the style:
33-
$phpWord->addFontStyle('myOwnStyle',
34-
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
35-
$section->addText('Hello world! I am formatted by a user defined style',
36-
'myOwnStyle');
37-
38-
// You can also put the appended element to local object like this:
39-
$fontStyle = array(
40-
'name' => 'Verdana',
41-
'size' => 22,
42-
'bold' => true,
44+
// Adding Text element having font customized using named font style...
45+
$fontStyleName = 'oneUserDefinedStyle';
46+
$phpWord->addFontStyle($fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
47+
$section->addText(
48+
htmlspecialchars('"The greatest accomplishment is not in never falling, but in rising again after you fall." (Vince Lombardi)'),
49+
$fontStyleName
4350
);
44-
$myTextElement = $section->addText('Hello World!');
51+
52+
// Adding Text element having font customized using explicitly created font style object...
53+
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
54+
$fontStyle->setBold(true);
55+
$fontStyle->setName('Tahoma');
56+
$fontStyle->setSize(13);
57+
$myTextElement = $section->addText(htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'));
4558
$myTextElement->setFontStyle($fontStyle);
4659
47-
// Finally, save the document:
48-
$phpWord->save('helloWorld.docx');
49-
$phpWord->save('helloWorld.odt', 'ODText');
50-
$phpWord->save('helloWorld.rtf', 'RTF');
60+
// Saving the document as OOXML file...
61+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
62+
$objWriter->save('helloWorld.docx');
63+
64+
// Saving the document as ODF file...
65+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
66+
$objWriter->save('helloWorld.odt');
67+
68+
// Saving the document as HTML file...
69+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
70+
$objWriter->save('helloWorld.html');
71+
72+
/* Note: RTF was skipped here, because the format is not XML-based and requires a bit different example. */
73+
/* Note: PDF was skipped here, because we use "HTML-to-PDF" approach to create PDF documents. */
5174
5275
Settings
5376
--------

docs/src/documentation.md

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -212,42 +212,65 @@ After installation, you can browse and use the samples that we've provided, eith
212212
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/).
213213

214214
```php
215+
<?php
215216
require_once 'src/PhpWord/Autoloader.php';
216217
\PhpOffice\PhpWord\Autoloader::register();
217218

219+
// Creating the new document...
218220
$phpWord = new \PhpOffice\PhpWord\PhpWord();
219221

220-
// Every element you want to append to the word document is placed in a section.
221-
// To create a basic section:
222+
/* Note: any element you append to a document must reside inside of a Section. */
223+
224+
// Adding an empty Section to the document...
222225
$section = $phpWord->addSection();
226+
// Adding Text element to the Section having font styled by default...
227+
$section->addText(
228+
htmlspecialchars('"Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning." (Albert Einstein)')
229+
);
230+
231+
/*
232+
* Note: it is possible to customize font style of the Text element you add in three ways:
233+
* - inline;
234+
* - using named font style (new font style object will be implicitly created);
235+
* - using explicitly created font style object.
236+
*/
237+
238+
// Adding Text element having font customized inline...
239+
$section->addText(
240+
htmlspecialchars('"Great achievement is usually born of great sacrifice, and is never the result of selfishness." (Napoleon Hill)'),
241+
array('name' => 'Tahoma', 'size' => 10)
242+
);
223243

224-
// After creating a section, you can append elements:
225-
$section->addText('Hello world!');
226-
227-
// You can directly style your text by giving the addText function an array:
228-
$section->addText('Hello world! I am formatted.',
229-
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
230-
231-
// If you often need the same style again you can create a user defined style
232-
// to the word document and give the addText function the name of the style:
233-
$phpWord->addFontStyle('myOwnStyle',
234-
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
235-
$section->addText('Hello world! I am formatted by a user defined style',
236-
'myOwnStyle');
237-
238-
// You can also put the appended element to local object like this:
239-
$fontStyle = array(
240-
'name' => 'Verdana',
241-
'size' => 22,
242-
'bold' => true,
244+
// Adding Text element having font customized using named font style...
245+
$fontStyleName = 'oneUserDefinedStyle';
246+
$phpWord->addFontStyle($fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true));
247+
$section->addText(
248+
htmlspecialchars('"The greatest accomplishment is not in never falling, but in rising again after you fall." (Vince Lombardi)'),
249+
$fontStyleName
243250
);
244-
$myTextElement = $section->addText('Hello World!');
251+
252+
// Adding Text element having font customized using explicitly created font style object...
253+
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
254+
$fontStyle->setBold(true);
255+
$fontStyle->setName('Tahoma');
256+
$fontStyle->setSize(13);
257+
$myTextElement = $section->addText(htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'));
245258
$myTextElement->setFontStyle($fontStyle);
246259

247-
// Finally, save the document:
248-
$phpWord->save('helloWorld.docx');
249-
$phpWord->save('helloWorld.odt', 'ODText');
250-
$phpWord->save('helloWorld.rtf', 'RTF');
260+
// Saving the document as OOXML file...
261+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
262+
$objWriter->save('helloWorld.docx');
263+
264+
// Saving the document as ODF file...
265+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
266+
$objWriter->save('helloWorld.odt');
267+
268+
// Saving the document as HTML file...
269+
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
270+
$objWriter->save('helloWorld.html');
271+
272+
/* Note: RTF was skipped here, because the format is not XML-based and requires a bit different example. */
273+
/* Note: PDF was skipped here, because we use "HTML-to-PDF" approach to create PDF documents. */
251274
```
252275

253276
## Settings

src/PhpWord/Writer/HTML/Element/ListItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function write()
3535
return '';
3636
}
3737

38-
$text = htmlspecialchars($this->element->getTextObject()->getText());
38+
$text = $this->element->getTextObject()->getText();
3939
$content = '<p>' . $text . '</p>' . PHP_EOL;
4040

4141
return $content;

src/PhpWord/Writer/HTML/Element/Text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function write()
7272
$content .= $this->writeOpening();
7373
$content .= $this->openingText;
7474
$content .= $this->openingTags;
75-
$content .= htmlspecialchars($element->getText());
75+
$content .= $element->getText();
7676
$content .= $this->closingTags;
7777
$content .= $this->closingText;
7878
$content .= $this->writeClosing();

src/PhpWord/Writer/HTML/Element/Title.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function write()
3636
}
3737

3838
$tag = 'h' . $this->element->getDepth();
39-
$text = htmlspecialchars($this->element->getText());
39+
$text = $this->element->getText();
4040
$content = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;
4141

4242
return $content;

src/PhpWord/Writer/HTML/Part/Head.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
namespace PhpOffice\PhpWord\Writer\HTML\Part;
1919

2020
use PhpOffice\PhpWord\Settings;
21+
use PhpOffice\PhpWord\Style;
2122
use PhpOffice\PhpWord\Style\Font;
2223
use PhpOffice\PhpWord\Style\Paragraph;
23-
use PhpOffice\PhpWord\Style;
2424
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
2525
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
2626
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
@@ -57,13 +57,13 @@ public function write()
5757

5858
$content .= '<head>' . PHP_EOL;
5959
$content .= '<meta charset="UTF-8" />' . PHP_EOL;
60-
$content .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
60+
$content .= '<title>' . $title . '</title>' . PHP_EOL;
6161
foreach ($propertiesMapping as $key => $value) {
6262
$value = ($value == '') ? $key : $value;
6363
$method = "get" . $key;
6464
if ($docProps->$method() != '') {
6565
$content .= '<meta name="' . $value . '" content="' .
66-
htmlspecialchars($docProps->$method()) . '" />' . PHP_EOL;
66+
$docProps->$method() . '" />' . PHP_EOL;
6767
}
6868
}
6969
$content .= $this->writeStyles();

0 commit comments

Comments
 (0)