Skip to content

Commit e22671e

Browse files
committed
Add shading, also fix exposed error in HTML writer.
1 parent 1e7485e commit e22671e

File tree

2 files changed

+139
-4
lines changed

2 files changed

+139
-4
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
use PhpOffice\PhpWord\Style\Font as FontStyle;
4+
use PhpOffice\PhpWord\Style\Shading as ShadingStyle;
5+
6+
include_once 'Sample_Header.php';
7+
8+
// New Word Document
9+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
10+
11+
$languageEnGb = new PhpOffice\PhpWord\Style\Language(PhpOffice\PhpWord\Style\Language::EN_GB);
12+
13+
$phpWord = new PhpOffice\PhpWord\PhpWord();
14+
$phpWord->getSettings()->setThemeFontLang($languageEnGb);
15+
$phpWord->addTitleStyle(1, ['bold' => true, 'underline' => 'single', 'size' => 18]);
16+
$phpWord->addTitleStyle(2, ['bold' => true, 'underline' => 'single']);
17+
$section = $phpWord->addSection();
18+
19+
$section->addTitle('Testing All Shading Styles', 1);
20+
$section->addTextBreak();
21+
22+
// Fill.
23+
$section->addTitle('Fill', 2);
24+
$section->addText("Paragraph. Color Yellow, Fill Red.", null, ['shading' => ['fill' => FontStyle::FGCOLOR_RED]]);
25+
$section->addText("Font. Color Yellow, Fill Red.", ['shading' => ['fill' => FontStyle::FGCOLOR_RED]]);
26+
$section->addTextBreak();
27+
28+
// Color.
29+
$section->addTitle('Color using Solid Pattern', 2);
30+
$section->addText("Paragraph. Color Yellow, Fill Red.", null, ['shading' => ['color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_SOLID]]);
31+
$section->addText("Font. Color Yellow, Fill Red.", ['shading' => ['color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_SOLID]]);
32+
$section->addTextBreak();
33+
34+
// Color and Fill.
35+
$section->addTitle('Color using Solid Pattern and Fill. Which will win?', 2);
36+
$section->addText("Paragraph. Color Yellow, Fill Red.", null, ['shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_SOLID]]);
37+
$section->addText("Font. Color Yellow, Fill Red.", ['shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_SOLID]]);
38+
$section->addTextBreak();
39+
40+
// Color and Fill.
41+
$section->addTitle('Color using Diagonal Cross Pattern and Fill', 2);
42+
$section->addText("Paragraph. Color Yellow, Fill Red.", null, ['shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_DCROSS]]);
43+
$section->addText("Font. Color Yellow, Fill Red.", ['shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_DCROSS]]);
44+
$section->addTextBreak();
45+
46+
// Pattern.
47+
$section->addTitle('Patterns', 2);
48+
$textrun = $section->addTextRun();
49+
$textrun->addText("This text uses the patterns: ");
50+
$textrun->addText("Solid, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_SOLID]]);
51+
$textrun->addText("Clear, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_CLEAR]]);
52+
$textrun->addText("Horizontal Stripe, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_HSTRIPE]]);
53+
$textrun->addText("Vertical Stripe, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_VSTRIPE]]);
54+
$textrun->addText("Diagonal Stripe, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_DSTRIPE]]);
55+
$textrun->addText("Horizontal Cross, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_HCROSS]]);
56+
$textrun->addText("Diagonal Cross, ", ['size' => 24, 'shading' => ['color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_DCROSS]]);
57+
$section->addTextBreak();
58+
59+
// Pattern.
60+
$section->addTitle('Patterns with Fill', 2);
61+
$textrun = $section->addTextRun();
62+
$textrun->addText("Same as previous, with red fill: ");
63+
$textrun->addText("Solid, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_SOLID]]);
64+
$textrun->addText("Clear, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_CLEAR]]);
65+
$textrun->addText("Horizontal Stripe, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_HSTRIPE]]);
66+
$textrun->addText("Vertical Stripe, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_VSTRIPE]]);
67+
$textrun->addText("Diagonal Stripe, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_DSTRIPE]]);
68+
$textrun->addText("Horizontal Cross, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_HCROSS]]);
69+
$textrun->addText("Diagonal Cross, ", ['size' => 24, 'shading' => ['fill' => FontStyle::FGCOLOR_RED, 'color' => FontStyle::FGCOLOR_BLUE, 'pattern' => ShadingStyle::PATTERN_DCROSS]]);
70+
$section->addTextBreak();
71+
72+
// Table
73+
$rows = 3;
74+
$cols = 3;
75+
76+
$section->addTitle('Table Fill Yellow', 2);
77+
$tableFill = ['shading' => ['fill' => FontStyle::FGCOLOR_YELLOW]];
78+
$phpWord->addTableStyle('fillYellow', $tableFill);
79+
$table = $section->addTable('fillYellow');
80+
for ($r = 1; $r <= $rows; ++$r) {
81+
$table->addRow();
82+
for ($c = 1; $c <= $cols; ++$c) {
83+
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
84+
}
85+
}
86+
$section->addTextBreak();
87+
88+
$section->addTitle('Table Color Light Green, Pattern Solid', 2);
89+
$tableColor = ['shading' => ['color' => FontStyle::FGCOLOR_LIGHTGREEN, 'pattern' => ShadingStyle::PATTERN_SOLID]];
90+
$phpWord->addTableStyle('colorYellow', $tableColor);
91+
$table = $section->addTable('colorYellow');
92+
for ($r = 1; $r <= $rows; ++$r) {
93+
$table->addRow();
94+
for ($c = 1; $c <= $cols; ++$c) {
95+
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
96+
}
97+
}
98+
$section->addTextBreak();
99+
100+
$section->addTitle('Table Color Yellow, Pattern vStripe', 2);
101+
$tablePattern = ['shading' => ['color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_VSTRIPE]];
102+
$phpWord->addTableStyle('patternYellow', $tablePattern);
103+
$table = $section->addTable('patternYellow');
104+
for ($r = 1; $r <= $rows; ++$r) {
105+
$table->addRow();
106+
for ($c = 1; $c <= $cols; ++$c) {
107+
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
108+
}
109+
}
110+
$section->addTextBreak();
111+
112+
// Cell
113+
$rows = 3;
114+
$cols = 3;
115+
116+
$section->addTitle('Cell Shading, using the same options as the previous tables', 2);
117+
$cellStyle1 = ['shading' => ['fill' => FontStyle::FGCOLOR_YELLOW]];
118+
$cellStyle2 = ['shading' => ['color' => FontStyle::FGCOLOR_LIGHTGREEN, 'pattern' => ShadingStyle::PATTERN_SOLID]];
119+
$cellStyle3 = ['shading' => ['color' => FontStyle::FGCOLOR_YELLOW, 'pattern' => ShadingStyle::PATTERN_VSTRIPE]];
120+
$table = $section->addTable();
121+
for ($r = 1; $r <= $rows; ++$r) {
122+
$table->addRow();
123+
for ($c = 1; $c <= $cols; ++$c) {
124+
$table->addCell(1750, ${'cellStyle' . $c})->addText("Row {$r}, Cell {$c}");
125+
}
126+
}
127+
128+
// Save file
129+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
130+
if (!CLI) {
131+
include_once 'Sample_Footer.php';
132+
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ public function write()
5858
$cellBgColor = $cellStyle->getBgColor();
5959
$cellFgColor = null;
6060
if ($cellBgColor && $cellBgColor !== 'auto') {
61-
$red = hexdec(substr($cellBgColor, 0, 2));
62-
$green = hexdec(substr($cellBgColor, 2, 2));
63-
$blue = hexdec(substr($cellBgColor, 4, 2));
64-
$cellFgColor = (($red * 0.299 + $green * 0.587 + $blue * 0.114) > 186) ? null : 'ffffff';
61+
$cellBgColor = \PhpOffice\PhpWord\Shared\Converter::stringToRgb($cellBgColor);
62+
if ($cellBgColor !== false) {
63+
$red = hexdec(substr($cellBgColor, 0, 2));
64+
$green = hexdec(substr($cellBgColor, 2, 2));
65+
$blue = hexdec(substr($cellBgColor, 4, 2));
66+
$cellFgColor = (($red * 0.299 + $green * 0.587 + $blue * 0.114) > 186) ? null : 'ffffff';
67+
}
6568
}
6669
$cellColSpan = $cellStyle->getGridSpan();
6770
$cellRowSpan = 1;

0 commit comments

Comments
 (0)