Skip to content

Commit c116afb

Browse files
author
Roman Syroeshko
committed
PHPOffice#51 (updated samples).
1 parent d4d498d commit c116afb

31 files changed

+579
-455
lines changed

samples/Sample_01_SimpleText.php

+37-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
include_once 'Sample_Header.php';
33

44
// New Word Document
5-
echo date('H:i:s') , " Create new PhpWord object" , EOL;
5+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
77
$phpWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
88
$phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
@@ -12,16 +12,16 @@
1212
$section = $phpWord->addSection();
1313

1414
// Simple text
15-
$section->addTitle('Welcome to PhpWord', 1);
16-
$section->addText('Hello World!');
15+
$section->addTitle(htmlspecialchars('Welcome to PhpWord'), 1);
16+
$section->addText(htmlspecialchars('Hello World!'));
1717

1818
// Two text break
1919
$section->addTextBreak(2);
2020

2121
// Defined style
22-
$section->addText('I am styled by a font style definition.', 'rStyle');
23-
$section->addText('I am styled by a paragraph style definition.', null, 'pStyle');
24-
$section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle');
22+
$section->addText(htmlspecialchars('I am styled by a font style definition.'), 'rStyle');
23+
$section->addText(htmlspecialchars('I am styled by a paragraph style definition.'), null, 'pStyle');
24+
$section->addText(htmlspecialchars('I am styled by both font and paragraph style.'), 'rStyle', 'pStyle');
2525

2626
$section->addTextBreak();
2727

@@ -30,39 +30,39 @@
3030
$fontStyle['size'] = 20;
3131

3232
$textrun = $section->addTextRun();
33-
$textrun->addText('I am inline styled ', $fontStyle);
34-
$textrun->addText('with ');
35-
$textrun->addText('color', array('color' => '996699'));
36-
$textrun->addText(', ');
37-
$textrun->addText('bold', array('bold' => true));
38-
$textrun->addText(', ');
39-
$textrun->addText('italic', array('italic' => true));
40-
$textrun->addText(', ');
41-
$textrun->addText('underline', array('underline' => 'dash'));
42-
$textrun->addText(', ');
43-
$textrun->addText('strikethrough', array('strikethrough' => true));
44-
$textrun->addText(', ');
45-
$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));
46-
$textrun->addText(', ');
47-
$textrun->addText('superScript', array('superScript' => true));
48-
$textrun->addText(', ');
49-
$textrun->addText('subScript', array('subScript' => true));
50-
$textrun->addText(', ');
51-
$textrun->addText('smallCaps', array('smallCaps' => true));
52-
$textrun->addText(', ');
53-
$textrun->addText('allCaps', array('allCaps' => true));
54-
$textrun->addText(', ');
55-
$textrun->addText('fgColor', array('fgColor' => 'yellow'));
56-
$textrun->addText(', ');
57-
$textrun->addText('scale', array('scale' => 200));
58-
$textrun->addText(', ');
59-
$textrun->addText('spacing', array('spacing' => 120));
60-
$textrun->addText(', ');
61-
$textrun->addText('kerning', array('kerning' => 10));
62-
$textrun->addText('. ');
33+
$textrun->addText(htmlspecialchars('I am inline styled '), $fontStyle);
34+
$textrun->addText(htmlspecialchars('with '));
35+
$textrun->addText(htmlspecialchars('color'), array('color' => '996699'));
36+
$textrun->addText(htmlspecialchars(', '));
37+
$textrun->addText(htmlspecialchars('bold'), array('bold' => true));
38+
$textrun->addText(htmlspecialchars(', '));
39+
$textrun->addText(htmlspecialchars('italic'), array('italic' => true));
40+
$textrun->addText(htmlspecialchars(', '));
41+
$textrun->addText(htmlspecialchars('underline'), array('underline' => 'dash'));
42+
$textrun->addText(htmlspecialchars(', '));
43+
$textrun->addText(htmlspecialchars('strikethrough'), array('strikethrough' => true));
44+
$textrun->addText(htmlspecialchars(', '));
45+
$textrun->addText(htmlspecialchars('doubleStrikethrough'), array('doubleStrikethrough' => true));
46+
$textrun->addText(htmlspecialchars(', '));
47+
$textrun->addText(htmlspecialchars('superScript'), array('superScript' => true));
48+
$textrun->addText(htmlspecialchars(', '));
49+
$textrun->addText(htmlspecialchars('subScript'), array('subScript' => true));
50+
$textrun->addText(htmlspecialchars(', '));
51+
$textrun->addText(htmlspecialchars('smallCaps'), array('smallCaps' => true));
52+
$textrun->addText(htmlspecialchars(', '));
53+
$textrun->addText(htmlspecialchars('allCaps'), array('allCaps' => true));
54+
$textrun->addText(htmlspecialchars(', '));
55+
$textrun->addText(htmlspecialchars('fgColor'), array('fgColor' => 'yellow'));
56+
$textrun->addText(htmlspecialchars(', '));
57+
$textrun->addText(htmlspecialchars('scale'), array('scale' => 200));
58+
$textrun->addText(htmlspecialchars(', '));
59+
$textrun->addText(htmlspecialchars('spacing'), array('spacing' => 120));
60+
$textrun->addText(htmlspecialchars(', '));
61+
$textrun->addText(htmlspecialchars('kerning'), array('kerning' => 10));
62+
$textrun->addText(htmlspecialchars('. '));
6363

6464
// Link
65-
$section->addLink('http://www.google.com', 'Google');
65+
$section->addLink('http://www.google.com', htmlspecialchars('Google'));
6666
$section->addTextBreak();
6767

6868
// Image

samples/Sample_02_TabStops.php

+22-21
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@
22
include_once 'Sample_Header.php';
33

44
// New Word Document
5-
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
5+
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
77

88
// Ads styles
9-
$phpWord->addParagraphStyle('multipleTab', array(
10-
'tabs' => array(
11-
new \PhpOffice\PhpWord\Style\Tab('left', 1550),
12-
new \PhpOffice\PhpWord\Style\Tab('center', 3200),
13-
new \PhpOffice\PhpWord\Style\Tab('right', 5300)
14-
)
15-
));
16-
$phpWord->addParagraphStyle('rightTab', array(
17-
'tabs' => array(
18-
new \PhpOffice\PhpWord\Style\Tab('right', 9090)
19-
)
20-
));
21-
$phpWord->addParagraphStyle('centerTab', array(
22-
'tabs' => array(
23-
new \PhpOffice\PhpWord\Style\Tab('center', 4680)
24-
)
25-
));
9+
$phpWord->addParagraphStyle(
10+
'multipleTab',
11+
array(
12+
'tabs' => array(
13+
new \PhpOffice\PhpWord\Style\Tab('left', 1550),
14+
new \PhpOffice\PhpWord\Style\Tab('center', 3200),
15+
new \PhpOffice\PhpWord\Style\Tab('right', 5300),
16+
)
17+
)
18+
);
19+
$phpWord->addParagraphStyle(
20+
'rightTab',
21+
array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090)))
22+
);
23+
$phpWord->addParagraphStyle(
24+
'centerTab',
25+
array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680)))
26+
);
2627

2728
// New portrait section
2829
$section = $phpWord->addSection();
2930

3031
// Add listitem elements
31-
$section->addText("Multiple Tabs:\tOne\tTwo\tThree", null, 'multipleTab');
32-
$section->addText("Left Aligned\tRight Aligned", null, 'rightTab');
33-
$section->addText("\tCenter Aligned", null, 'centerTab');
32+
$section->addText(htmlspecialchars("Multiple Tabs:\tOne\tTwo\tThree"), null, 'multipleTab');
33+
$section->addText(htmlspecialchars("Left Aligned\tRight Aligned"), null, 'rightTab');
34+
$section->addText(htmlspecialchars("\tCenter Aligned"), null, 'centerTab');
3435

3536
// Save file
3637
echo write($phpWord, basename(__FILE__, '.php'), $writers);

samples/Sample_03_Sections.php

+24-9
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,43 @@
22
include_once 'Sample_Header.php';
33

44
// New Word Document
5-
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
5+
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
77

88
// New portrait section
99
$section = $phpWord->addSection(array('borderColor' => '00FF00', 'borderSize' => 12));
10-
$section->addText('I am placed on a default section.');
10+
$section->addText(htmlspecialchars('I am placed on a default section.'));
1111

1212
// New landscape section
1313
$section = $phpWord->addSection(array('orientation' => 'landscape'));
14-
$section->addText('I am placed on a landscape section. Every page starting from this section will be landscape style.');
14+
$section->addText(
15+
htmlspecialchars(
16+
'I am placed on a landscape section. Every page starting from this section will be landscape style.'
17+
)
18+
);
1519
$section->addPageBreak();
1620
$section->addPageBreak();
1721

1822
// New portrait section
19-
$section = $phpWord->addSection(array('paperSize' => 'Folio', 'marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600));
20-
$section->addText('This section uses other margins with folio papersize.');
23+
$section = $phpWord->addSection(
24+
array('paperSize' => 'Folio', 'marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)
25+
);
26+
$section->addText(htmlspecialchars('This section uses other margins with folio papersize.'));
2127

2228
// New portrait section with Header & Footer
23-
$section = $phpWord->addSection(array('marginLeft' => 200, 'marginRight' => 200, 'marginTop' => 200, 'marginBottom' => 200, 'headerHeight' => 50, 'footerHeight' => 50,));
24-
$section->addText('This section and we play with header/footer height.');
25-
$section->addHeader()->addText('Header');
26-
$section->addFooter()->addText('Footer');
29+
$section = $phpWord->addSection(
30+
array(
31+
'marginLeft' => 200,
32+
'marginRight' => 200,
33+
'marginTop' => 200,
34+
'marginBottom' => 200,
35+
'headerHeight' => 50,
36+
'footerHeight' => 50,
37+
)
38+
);
39+
$section->addText(htmlspecialchars('This section and we play with header/footer height.'));
40+
$section->addHeader()->addText(htmlspecialchars('Header'));
41+
$section->addFooter()->addText(htmlspecialchars('Footer'));
2742

2843
// Save file
2944
echo write($phpWord, basename(__FILE__, '.php'), $writers);

samples/Sample_04_Textrun.php

+23-17
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,42 @@
22
include_once 'Sample_Header.php';
33

44
// New Word Document
5-
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
5+
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
77

88
// Ads styles
9-
$phpWord->addParagraphStyle('pStyle', array('spacing'=>100));
10-
$phpWord->addFontStyle('BoldText', array('bold'=>true));
11-
$phpWord->addFontStyle('ColoredText', array('color'=>'FF8080', 'bgColor' => 'FFFFCC'));
12-
$phpWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
9+
$phpWord->addParagraphStyle('pStyle', array('spacing' => 100));
10+
$phpWord->addFontStyle('BoldText', array('bold' => true));
11+
$phpWord->addFontStyle('ColoredText', array('color' => 'FF8080', 'bgColor' => 'FFFFCC'));
12+
$phpWord->addLinkStyle(
13+
'NLink',
14+
array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)
15+
);
1316

1417
// New portrait section
1518
$section = $phpWord->addSection();
1619

1720
// Add text run
1821
$textrun = $section->addTextRun('pStyle');
1922

20-
$textrun->addText('Each textrun can contain native text, link elements or an image.');
21-
$textrun->addText(' No break is placed after adding an element.', 'BoldText');
22-
$textrun->addText(' Both ');
23-
$textrun->addText('superscript', array('superScript' => true));
24-
$textrun->addText(' and ');
25-
$textrun->addText('subscript', array('subScript' => true));
26-
$textrun->addText(' are also available.');
27-
$textrun->addText(' All elements are placed inside a paragraph with the optionally given p-Style.', 'ColoredText');
28-
$textrun->addText(' Sample Link: ');
23+
$textrun->addText(htmlspecialchars('Each textrun can contain native text, link elements or an image.'));
24+
$textrun->addText(htmlspecialchars(' No break is placed after adding an element.'), 'BoldText');
25+
$textrun->addText(htmlspecialchars(' Both '));
26+
$textrun->addText(htmlspecialchars('superscript'), array('superScript' => true));
27+
$textrun->addText(htmlspecialchars(' and '));
28+
$textrun->addText(htmlspecialchars('subscript'), array('subScript' => true));
29+
$textrun->addText(htmlspecialchars(' are also available.'));
30+
$textrun->addText(
31+
htmlspecialchars(' All elements are placed inside a paragraph with the optionally given p-Style.'),
32+
'ColoredText'
33+
);
34+
$textrun->addText(htmlspecialchars(' Sample Link: '));
2935
$textrun->addLink('http://www.google.com', null, 'NLink');
30-
$textrun->addText(' Sample Image: ');
36+
$textrun->addText(htmlspecialchars(' Sample Image: '));
3137
$textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
32-
$textrun->addText(' Sample Object: ');
38+
$textrun->addText(htmlspecialchars(' Sample Object: '));
3339
$textrun->addObject('resources/_sheet.xls');
34-
$textrun->addText(' Here is some more text. ');
40+
$textrun->addText(htmlspecialchars(' Here is some more text. '));
3541

3642
// Save file
3743
echo write($phpWord, basename(__FILE__, '.php'), $writers);

samples/Sample_05_Multicolumn.php

+24-18
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,44 @@
22
include_once 'Sample_Header.php';
33

44
// New Word Document
5-
echo date('H:i:s') , " Create new PhpWord object" , EOL;
5+
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7-
$filler = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' .
8-
'Nulla fermentum, tortor id adipiscing adipiscing, tortor turpis commodo. ' .
9-
'Donec vulputate iaculis metus, vel luctus dolor hendrerit ac. ' .
10-
'Suspendisse congue congue leo sed pellentesque.';
7+
$filler = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
8+
. 'Nulla fermentum, tortor id adipiscing adipiscing, tortor turpis commodo. '
9+
. 'Donec vulputate iaculis metus, vel luctus dolor hendrerit ac. '
10+
. 'Suspendisse congue congue leo sed pellentesque.';
1111

1212
// Normal
1313
$section = $phpWord->addSection();
14-
$section->addText('Normal paragraph. ' . $filler);
14+
$section->addText(htmlspecialchars("Normal paragraph. {$filler}"));
1515

1616
// Two columns
17-
$section = $phpWord->addSection(array(
18-
'colsNum' => 2,
19-
'colsSpace' => 1440,
20-
'breakType' => 'continuous'));
21-
$section->addText('Two columns, one inch (1440 twips) spacing. ' . $filler);
17+
$section = $phpWord->addSection(
18+
array(
19+
'colsNum' => 2,
20+
'colsSpace' => 1440,
21+
'breakType' => 'continuous',
22+
)
23+
);
24+
$section->addText(htmlspecialchars("Two columns, one inch (1440 twips) spacing. {$filler}"));
2225

2326
// Normal
2427
$section = $phpWord->addSection(array('breakType' => 'continuous'));
25-
$section->addText('Normal paragraph again. ' . $filler);
28+
$section->addText(htmlspecialchars("Normal paragraph again. {$filler}"));
2629

2730
// Three columns
28-
$section = $phpWord->addSection(array(
29-
'colsNum' => 3,
30-
'colsSpace' => 720,
31-
'breakType' => 'continuous'));
32-
$section->addText('Three columns, half inch (720 twips) spacing. ' . $filler);
31+
$section = $phpWord->addSection(
32+
array(
33+
'colsNum' => 3,
34+
'colsSpace' => 720,
35+
'breakType' => 'continuous',
36+
)
37+
);
38+
$section->addText(htmlspecialchars("Three columns, half inch (720 twips) spacing. {$filler}"));
3339

3440
// Normal
3541
$section = $phpWord->addSection(array('breakType' => 'continuous'));
36-
$section->addText('Normal paragraph again.');
42+
$section->addText(htmlspecialchars('Normal paragraph again.'));
3743

3844
// Save file
3945
echo write($phpWord, basename(__FILE__, '.php'), $writers);

0 commit comments

Comments
 (0)