1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer; |
4
|
|
|
|
5
|
|
|
use Composer\Pcre\Preg; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Chart\Chart; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Comment; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Document\Properties; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\Run; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Settings; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
18
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Drawing as SharedDrawing; |
19
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
20
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Font as SharedFont; |
21
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
22
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
23
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
24
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
25
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Borders; |
26
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
27
|
|
|
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\CellStyleAssessor; |
28
|
|
|
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\StyleMerger; |
29
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
30
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
31
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
32
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
33
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing; |
34
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
35
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; |
36
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
37
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table; |
38
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableDxfsStyle; |
39
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
40
|
|
|
|
41
|
|
|
class Html extends BaseWriter |
42
|
|
|
{ |
43
|
|
|
private const DEFAULT_CELL_WIDTH_POINTS = 42; |
44
|
|
|
|
45
|
|
|
private const DEFAULT_CELL_WIDTH_PIXELS = 56; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Migration aid to tell if html tags will be treated as plaintext in comments. |
49
|
|
|
* if ( |
50
|
|
|
* defined( |
51
|
|
|
* \PhpOffice\PhpSpreadsheet\Writer\Html::class |
52
|
|
|
* . '::COMMENT_HTML_TAGS_PLAINTEXT' |
53
|
|
|
* ) |
54
|
|
|
* ) { |
55
|
|
|
* new logic with styling in TextRun elements |
56
|
|
|
* } else { |
57
|
|
|
* old logic with styling via Html tags |
58
|
|
|
* }. |
59
|
|
|
*/ |
60
|
|
|
public const COMMENT_HTML_TAGS_PLAINTEXT = true; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Spreadsheet object. |
64
|
|
|
*/ |
65
|
|
|
protected Spreadsheet $spreadsheet; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sheet index to write. |
69
|
|
|
*/ |
70
|
|
|
private ?int $sheetIndex = 0; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Images root. |
74
|
|
|
*/ |
75
|
|
|
private string $imagesRoot = ''; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* embed images, or link to images. |
79
|
|
|
*/ |
80
|
|
|
protected bool $embedImages = false; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Use inline CSS? |
84
|
|
|
*/ |
85
|
|
|
private bool $useInlineCss = false; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Array of CSS styles. |
89
|
|
|
* |
90
|
|
|
* @var string[][] |
91
|
|
|
*/ |
92
|
|
|
private ?array $cssStyles = null; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Array of column widths in points. |
96
|
|
|
* |
97
|
|
|
* @var array<array<float|int>> |
98
|
|
|
*/ |
99
|
|
|
private array $columnWidths; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Default font. |
103
|
|
|
*/ |
104
|
|
|
private Font $defaultFont; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Flag whether spans have been calculated. |
108
|
|
|
*/ |
109
|
|
|
private bool $spansAreCalculated = false; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Excel cells that should not be written as HTML cells. |
113
|
|
|
* |
114
|
|
|
* @var mixed[][][][] |
115
|
|
|
*/ |
116
|
|
|
private array $isSpannedCell = []; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Excel cells that are upper-left corner in a cell merge. |
120
|
|
|
* |
121
|
|
|
* @var int[][][][] |
122
|
|
|
*/ |
123
|
|
|
private array $isBaseCell = []; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Excel rows that should not be written as HTML rows. |
127
|
|
|
* |
128
|
|
|
* @var mixed[][] |
129
|
|
|
*/ |
130
|
|
|
private array $isSpannedRow = []; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Is the current writer creating PDF? |
134
|
|
|
*/ |
135
|
|
|
protected bool $isPdf = false; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generate the Navigation block. |
139
|
|
|
*/ |
140
|
|
|
private bool $generateSheetNavigationBlock = true; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Callback for editing generated html. |
144
|
|
|
* |
145
|
|
|
* @var null|callable(string): string |
146
|
|
|
*/ |
147
|
|
|
private $editHtmlCallback; |
148
|
|
|
|
149
|
|
|
/** @var BaseDrawing[] */ |
150
|
|
|
private $sheetDrawings; |
151
|
|
|
|
152
|
|
|
/** @var Chart[] */ |
153
|
|
|
private $sheetCharts; |
154
|
|
|
|
155
|
|
|
private bool $betterBoolean = true; |
156
|
|
|
|
157
|
|
|
private string $getTrue = 'TRUE'; |
158
|
|
|
|
159
|
|
|
private string $getFalse = 'FALSE'; |
160
|
|
|
|
161
|
|
|
protected bool $rtlSheets = false; |
162
|
|
|
|
163
|
|
|
protected bool $ltrSheets = false; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Create a new HTML. |
167
|
|
|
*/ |
168
|
562 |
|
public function __construct(Spreadsheet $spreadsheet) |
169
|
|
|
{ |
170
|
562 |
|
$this->spreadsheet = $spreadsheet; |
171
|
562 |
|
$this->defaultFont = $this->spreadsheet->getDefaultStyle()->getFont(); |
172
|
562 |
|
$calc = Calculation::getInstance($this->spreadsheet); |
173
|
562 |
|
$this->getTrue = $calc->getTRUE(); |
174
|
562 |
|
$this->getFalse = $calc->getFALSE(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Save Spreadsheet to file. |
179
|
|
|
* |
180
|
|
|
* @param resource|string $filename |
181
|
|
|
*/ |
182
|
466 |
|
public function save($filename, int $flags = 0): void |
183
|
|
|
{ |
184
|
466 |
|
$this->processFlags($flags); |
185
|
|
|
// Open file |
186
|
466 |
|
$this->openFileHandle($filename); |
187
|
|
|
// Write html |
188
|
465 |
|
fwrite($this->fileHandle, $this->generateHTMLAll()); |
189
|
|
|
// Close file |
190
|
465 |
|
$this->maybeCloseFileHandle(); |
191
|
|
|
} |
192
|
|
|
|
193
|
549 |
|
protected function checkRtlAndLtr(): void |
194
|
|
|
{ |
195
|
549 |
|
$this->rtlSheets = false; |
196
|
549 |
|
$this->ltrSheets = false; |
197
|
549 |
|
if ($this->sheetIndex === null) { |
198
|
18 |
|
foreach ($this->spreadsheet->getAllSheets() as $sheet) { |
199
|
18 |
|
if ($sheet->getRightToLeft()) { |
200
|
3 |
|
$this->rtlSheets = true; |
201
|
|
|
} else { |
202
|
17 |
|
$this->ltrSheets = true; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
538 |
|
if ($this->spreadsheet->getSheet($this->sheetIndex)->getRightToLeft()) { |
207
|
2 |
|
$this->rtlSheets = true; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Save Spreadsheet as html to variable. |
214
|
|
|
*/ |
215
|
549 |
|
public function generateHtmlAll(): string |
216
|
|
|
{ |
217
|
549 |
|
$this->checkRtlAndLtr(); |
218
|
549 |
|
$sheets = $this->generateSheetPrep(); |
219
|
549 |
|
foreach ($sheets as $sheet) { |
220
|
549 |
|
$sheet->calculateArrays($this->preCalculateFormulas); |
221
|
|
|
} |
222
|
|
|
// garbage collect |
223
|
549 |
|
$this->spreadsheet->garbageCollect(); |
224
|
|
|
|
225
|
549 |
|
$saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog(); |
226
|
549 |
|
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false); |
227
|
|
|
|
228
|
|
|
// Build CSS |
229
|
549 |
|
$this->buildCSS(!$this->useInlineCss); |
230
|
|
|
|
231
|
549 |
|
$html = ''; |
232
|
|
|
|
233
|
|
|
// Write headers |
234
|
549 |
|
$html .= $this->generateHTMLHeader(!$this->useInlineCss); |
235
|
|
|
|
236
|
|
|
// Write navigation (tabs) |
237
|
549 |
|
if ((!$this->isPdf) && ($this->generateSheetNavigationBlock)) { |
238
|
518 |
|
$html .= $this->generateNavigation(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// Write data |
242
|
549 |
|
$html .= $this->generateSheetData(); |
243
|
|
|
|
244
|
|
|
// Write footer |
245
|
549 |
|
$html .= $this->generateHTMLFooter(); |
246
|
549 |
|
$callback = $this->editHtmlCallback; |
247
|
549 |
|
if ($callback) { |
248
|
6 |
|
$html = $callback($html); |
249
|
|
|
} |
250
|
|
|
|
251
|
549 |
|
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); |
252
|
|
|
|
253
|
549 |
|
return $html; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set a callback to edit the entire HTML. |
258
|
|
|
* |
259
|
|
|
* The callback must accept the HTML as string as first parameter, |
260
|
|
|
* and it must return the edited HTML as string. |
261
|
|
|
*/ |
262
|
6 |
|
public function setEditHtmlCallback(?callable $callback): void |
263
|
|
|
{ |
264
|
6 |
|
$this->editHtmlCallback = $callback; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Map VAlign. |
269
|
|
|
* |
270
|
|
|
* @param string $vAlign Vertical alignment |
271
|
|
|
*/ |
272
|
551 |
|
private function mapVAlign(string $vAlign): string |
273
|
|
|
{ |
274
|
551 |
|
return Alignment::VERTICAL_ALIGNMENT_FOR_HTML[$vAlign] ?? ''; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Map HAlign. |
279
|
|
|
* |
280
|
|
|
* @param string $hAlign Horizontal alignment |
281
|
|
|
*/ |
282
|
551 |
|
private function mapHAlign(string $hAlign): string |
283
|
|
|
{ |
284
|
551 |
|
return Alignment::HORIZONTAL_ALIGNMENT_FOR_HTML[$hAlign] ?? ''; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
const BORDER_NONE = 'none'; |
288
|
|
|
const BORDER_ARR = [ |
289
|
|
|
Border::BORDER_NONE => self::BORDER_NONE, |
290
|
|
|
Border::BORDER_DASHDOT => '1px dashed', |
291
|
|
|
Border::BORDER_DASHDOTDOT => '1px dotted', |
292
|
|
|
Border::BORDER_DASHED => '1px dashed', |
293
|
|
|
Border::BORDER_DOTTED => '1px dotted', |
294
|
|
|
Border::BORDER_DOUBLE => '3px double', |
295
|
|
|
Border::BORDER_HAIR => '1px solid', |
296
|
|
|
Border::BORDER_MEDIUM => '2px solid', |
297
|
|
|
Border::BORDER_MEDIUMDASHDOT => '2px dashed', |
298
|
|
|
Border::BORDER_MEDIUMDASHDOTDOT => '2px dotted', |
299
|
|
|
Border::BORDER_SLANTDASHDOT => '2px dashed', |
300
|
|
|
Border::BORDER_THICK => '3px solid', |
301
|
|
|
]; |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Map border style. |
305
|
|
|
* |
306
|
|
|
* @param int|string $borderStyle Sheet index |
307
|
|
|
*/ |
308
|
539 |
|
private function mapBorderStyle($borderStyle): string |
309
|
|
|
{ |
310
|
539 |
|
return self::BORDER_ARR[$borderStyle] ?? '1px solid'; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get sheet index. |
315
|
|
|
*/ |
316
|
20 |
|
public function getSheetIndex(): ?int |
317
|
|
|
{ |
318
|
20 |
|
return $this->sheetIndex; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Set sheet index. |
323
|
|
|
* |
324
|
|
|
* @param int $sheetIndex Sheet index |
325
|
|
|
* |
326
|
|
|
* @return $this |
327
|
|
|
*/ |
328
|
1 |
|
public function setSheetIndex(int $sheetIndex): static |
329
|
|
|
{ |
330
|
1 |
|
$this->sheetIndex = $sheetIndex; |
331
|
|
|
|
332
|
1 |
|
return $this; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Get sheet index. |
337
|
|
|
*/ |
338
|
1 |
|
public function getGenerateSheetNavigationBlock(): bool |
339
|
|
|
{ |
340
|
1 |
|
return $this->generateSheetNavigationBlock; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set sheet index. |
345
|
|
|
* |
346
|
|
|
* @param bool $generateSheetNavigationBlock Flag indicating whether the sheet navigation block should be generated or not |
347
|
|
|
* |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
1 |
|
public function setGenerateSheetNavigationBlock(bool $generateSheetNavigationBlock): static |
351
|
|
|
{ |
352
|
1 |
|
$this->generateSheetNavigationBlock = (bool) $generateSheetNavigationBlock; |
353
|
|
|
|
354
|
1 |
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Write all sheets (resets sheetIndex to NULL). |
359
|
|
|
* |
360
|
|
|
* @return $this |
361
|
|
|
*/ |
362
|
19 |
|
public function writeAllSheets(): static |
363
|
|
|
{ |
364
|
19 |
|
$this->sheetIndex = null; |
365
|
|
|
|
366
|
19 |
|
return $this; |
367
|
|
|
} |
368
|
|
|
|
369
|
551 |
|
private static function generateMeta(?string $val, string $desc): string |
370
|
|
|
{ |
371
|
551 |
|
return ($val || $val === '0') |
372
|
551 |
|
? (' <meta name="' . $desc . '" content="' . htmlspecialchars($val, Settings::htmlEntityFlags()) . '" />' . PHP_EOL) |
373
|
551 |
|
: ''; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public const BODY_LINE = ' <body>' . PHP_EOL; |
377
|
|
|
|
378
|
|
|
private const CUSTOM_TO_META = [ |
379
|
|
|
Properties::PROPERTY_TYPE_BOOLEAN => 'bool', |
380
|
|
|
Properties::PROPERTY_TYPE_DATE => 'date', |
381
|
|
|
Properties::PROPERTY_TYPE_FLOAT => 'float', |
382
|
|
|
Properties::PROPERTY_TYPE_INTEGER => 'int', |
383
|
|
|
Properties::PROPERTY_TYPE_STRING => 'string', |
384
|
|
|
]; |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Generate HTML header. |
388
|
|
|
* |
389
|
|
|
* @param bool $includeStyles Include styles? |
390
|
|
|
*/ |
391
|
551 |
|
public function generateHTMLHeader(bool $includeStyles = false): string |
392
|
|
|
{ |
393
|
|
|
// Construct HTML |
394
|
551 |
|
$properties = $this->spreadsheet->getProperties(); |
395
|
551 |
|
$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . PHP_EOL; |
396
|
551 |
|
$rtl = ($this->rtlSheets && !$this->ltrSheets) ? " dir='rtl'" : ''; |
397
|
551 |
|
$html .= '<html xmlns="http://www.w3.org/1999/xhtml"' . $rtl . '>' . PHP_EOL; |
398
|
551 |
|
$html .= ' <head>' . PHP_EOL; |
399
|
551 |
|
$html .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . PHP_EOL; |
400
|
551 |
|
$html .= ' <meta name="generator" content="PhpSpreadsheet, https://github.com/PHPOffice/PhpSpreadsheet" />' . PHP_EOL; |
401
|
551 |
|
$title = $properties->getTitle(); |
402
|
551 |
|
if ($title === '') { |
403
|
20 |
|
$title = $this->spreadsheet->getActiveSheet()->getTitle(); |
404
|
|
|
} |
405
|
551 |
|
$html .= ' <title>' . htmlspecialchars($title, Settings::htmlEntityFlags()) . '</title>' . PHP_EOL; |
406
|
551 |
|
$html .= self::generateMeta($properties->getCreator(), 'author'); |
407
|
551 |
|
$html .= self::generateMeta($properties->getTitle(), 'title'); |
408
|
551 |
|
$html .= self::generateMeta($properties->getDescription(), 'description'); |
409
|
551 |
|
$html .= self::generateMeta($properties->getSubject(), 'subject'); |
410
|
551 |
|
$html .= self::generateMeta($properties->getKeywords(), 'keywords'); |
411
|
551 |
|
$html .= self::generateMeta($properties->getCategory(), 'category'); |
412
|
551 |
|
$html .= self::generateMeta($properties->getCompany(), 'company'); |
413
|
551 |
|
$html .= self::generateMeta($properties->getManager(), 'manager'); |
414
|
551 |
|
$html .= self::generateMeta($properties->getLastModifiedBy(), 'lastModifiedBy'); |
415
|
551 |
|
$html .= self::generateMeta($properties->getViewport(), 'viewport'); |
416
|
551 |
|
$date = Date::dateTimeFromTimestamp((string) $properties->getCreated()); |
417
|
551 |
|
$date->setTimeZone(Date::getDefaultOrLocalTimeZone()); |
418
|
551 |
|
$html .= self::generateMeta($date->format(DATE_W3C), 'created'); |
419
|
551 |
|
$date = Date::dateTimeFromTimestamp((string) $properties->getModified()); |
420
|
551 |
|
$date->setTimeZone(Date::getDefaultOrLocalTimeZone()); |
421
|
551 |
|
$html .= self::generateMeta($date->format(DATE_W3C), 'modified'); |
422
|
|
|
|
423
|
551 |
|
$customProperties = $properties->getCustomProperties(); |
424
|
551 |
|
foreach ($customProperties as $customProperty) { |
425
|
4 |
|
$propertyValue = $properties->getCustomPropertyValue($customProperty); |
426
|
4 |
|
$propertyType = $properties->getCustomPropertyType($customProperty); |
427
|
4 |
|
$propertyQualifier = self::CUSTOM_TO_META[$propertyType] ?? null; |
428
|
4 |
|
if ($propertyQualifier !== null) { |
429
|
4 |
|
if ($propertyType === Properties::PROPERTY_TYPE_BOOLEAN) { |
430
|
1 |
|
$propertyValue = $propertyValue ? '1' : '0'; |
431
|
4 |
|
} elseif ($propertyType === Properties::PROPERTY_TYPE_DATE) { |
432
|
1 |
|
$date = Date::dateTimeFromTimestamp((string) $propertyValue); |
433
|
1 |
|
$date->setTimeZone(Date::getDefaultOrLocalTimeZone()); |
434
|
1 |
|
$propertyValue = $date->format(DATE_W3C); |
435
|
|
|
} else { |
436
|
4 |
|
$propertyValue = (string) $propertyValue; |
437
|
|
|
} |
438
|
4 |
|
$html .= self::generateMeta($propertyValue, htmlspecialchars("custom.$propertyQualifier.$customProperty")); |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
442
|
551 |
|
if (!empty($properties->getHyperlinkBase())) { |
443
|
2 |
|
$html .= ' <base href="' . htmlspecialchars($properties->getHyperlinkBase()) . '" />' . PHP_EOL; |
444
|
|
|
} |
445
|
|
|
|
446
|
551 |
|
$html .= $includeStyles ? $this->generateStyles(true) : $this->generatePageDeclarations(true); |
447
|
|
|
|
448
|
551 |
|
$html .= ' </head>' . PHP_EOL; |
449
|
551 |
|
$html .= '' . PHP_EOL; |
450
|
551 |
|
$html .= self::BODY_LINE; |
451
|
|
|
|
452
|
551 |
|
return $html; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** @return Worksheet[] */ |
456
|
549 |
|
private function generateSheetPrep(): array |
457
|
|
|
{ |
458
|
|
|
// Fetch sheets |
459
|
549 |
|
if ($this->sheetIndex === null) { |
460
|
18 |
|
$sheets = $this->spreadsheet->getAllSheets(); |
461
|
|
|
} else { |
462
|
538 |
|
$sheets = [$this->spreadsheet->getSheet($this->sheetIndex)]; |
463
|
|
|
} |
464
|
|
|
|
465
|
549 |
|
return $sheets; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** @return array{int, int, int} */ |
469
|
549 |
|
private function generateSheetStarts(Worksheet $sheet, int $rowMin): array |
470
|
|
|
{ |
471
|
|
|
// calculate start of <tbody>, <thead> |
472
|
549 |
|
$tbodyStart = $rowMin; |
473
|
549 |
|
$theadStart = $theadEnd = 0; // default: no <thead> no </thead> |
474
|
549 |
|
if ($sheet->getPageSetup()->isRowsToRepeatAtTopSet()) { |
475
|
2 |
|
$rowsToRepeatAtTop = $sheet->getPageSetup()->getRowsToRepeatAtTop(); |
476
|
|
|
|
477
|
|
|
// we can only support repeating rows that start at top row |
478
|
2 |
|
if ($rowsToRepeatAtTop[0] == 1) { |
479
|
2 |
|
$theadStart = $rowsToRepeatAtTop[0]; |
480
|
2 |
|
$theadEnd = $rowsToRepeatAtTop[1]; |
481
|
2 |
|
$tbodyStart = $rowsToRepeatAtTop[1] + 1; |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
549 |
|
return [$theadStart, $theadEnd, $tbodyStart]; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** @return array{string, string, string} */ |
489
|
549 |
|
private function generateSheetTags(int $row, int $theadStart, int $theadEnd, int $tbodyStart): array |
490
|
|
|
{ |
491
|
|
|
// <thead> ? |
492
|
549 |
|
$startTag = ($row == $theadStart) ? (' <thead>' . PHP_EOL) : ''; |
493
|
549 |
|
if (!$startTag) { |
494
|
549 |
|
$startTag = ($row == $tbodyStart) ? (' <tbody>' . PHP_EOL) : ''; |
495
|
|
|
} |
496
|
549 |
|
$endTag = ($row == $theadEnd) ? (' </thead>' . PHP_EOL) : ''; |
497
|
549 |
|
$cellType = ($row >= $tbodyStart) ? 'td' : 'th'; |
498
|
|
|
|
499
|
549 |
|
return [$cellType, $startTag, $endTag]; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* Generate sheet data. |
504
|
|
|
*/ |
505
|
549 |
|
public function generateSheetData(): string |
506
|
|
|
{ |
507
|
|
|
// Ensure that Spans have been calculated? |
508
|
549 |
|
$this->calculateSpans(); |
509
|
549 |
|
$sheets = $this->generateSheetPrep(); |
510
|
|
|
|
511
|
|
|
// Construct HTML |
512
|
549 |
|
$html = ''; |
513
|
|
|
|
514
|
|
|
// Loop all sheets |
515
|
549 |
|
$sheetId = 0; |
516
|
|
|
|
517
|
549 |
|
$activeSheet = $this->spreadsheet->getActiveSheetIndex(); |
518
|
|
|
|
519
|
549 |
|
foreach ($sheets as $sheet) { |
520
|
|
|
// save active cells |
521
|
549 |
|
$selectedCells = $sheet->getSelectedCells(); |
522
|
|
|
// Write table header |
523
|
549 |
|
$html .= $this->generateTableHeader($sheet); |
524
|
549 |
|
$this->sheetCharts = []; |
525
|
549 |
|
$this->sheetDrawings = []; |
526
|
549 |
|
$condStylesCollection = $sheet->getConditionalStylesCollection(); |
527
|
549 |
|
foreach ($condStylesCollection as $condStyles) { |
528
|
12 |
|
foreach ($condStyles as $key => $cs) { |
529
|
12 |
|
if ($cs->getConditionType() === Conditional::CONDITION_COLORSCALE) { |
530
|
3 |
|
$cs->getColorScale()?->setScaleArray(); |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
// Get worksheet dimension |
535
|
549 |
|
[$min, $max] = explode(':', $sheet->calculateWorksheetDataDimension()); |
536
|
549 |
|
[$minCol, $minRow, $minColString] = Coordinate::indexesFromString($min); |
537
|
549 |
|
[$maxCol, $maxRow] = Coordinate::indexesFromString($max); |
538
|
549 |
|
$this->extendRowsAndColumns($sheet, $maxCol, $maxRow); |
539
|
|
|
|
540
|
549 |
|
[$theadStart, $theadEnd, $tbodyStart] = $this->generateSheetStarts($sheet, $minRow); |
541
|
|
|
// Loop through cells |
542
|
549 |
|
$row = $minRow - 1; |
543
|
549 |
|
while ($row++ < $maxRow) { |
544
|
549 |
|
[$cellType, $startTag, $endTag] = $this->generateSheetTags($row, $theadStart, $theadEnd, $tbodyStart); |
545
|
549 |
|
$html .= StringHelper::convertToString($startTag); |
546
|
|
|
|
547
|
|
|
// Write row if there are HTML table cells in it |
548
|
549 |
|
if ($this->shouldGenerateRow($sheet, $row) && !isset($this->isSpannedRow[$sheet->getParentOrThrow()->getIndex($sheet)][$row])) { |
549
|
|
|
// Start a new rowData |
550
|
549 |
|
$rowData = []; |
551
|
|
|
// Loop through columns |
552
|
549 |
|
$column = $minCol; |
553
|
549 |
|
$colStr = $minColString; |
554
|
549 |
|
while ($column <= $maxCol) { |
555
|
|
|
// Cell exists? |
556
|
549 |
|
$cellAddress = Coordinate::stringFromColumnIndex($column) . $row; |
557
|
549 |
|
if ($this->shouldGenerateColumn($sheet, $colStr)) { |
558
|
549 |
|
$rowData[$column] = ($sheet->getCellCollection()->has($cellAddress)) ? $cellAddress : ''; |
559
|
|
|
} |
560
|
549 |
|
++$column; |
561
|
|
|
/** @var string $colStr */ |
562
|
549 |
|
++$colStr; |
563
|
|
|
} |
564
|
549 |
|
$html .= $this->generateRow($sheet, $rowData, $row - 1, $cellType); |
565
|
|
|
} |
566
|
|
|
|
567
|
549 |
|
$html .= StringHelper::convertToString($endTag); |
568
|
|
|
} |
569
|
|
|
// Write table footer |
570
|
549 |
|
$html .= $this->generateTableFooter(); |
571
|
|
|
// Writing PDF? |
572
|
549 |
|
if ($this->isPdf && $this->useInlineCss) { |
573
|
8 |
|
if ($this->sheetIndex === null && $sheetId + 1 < $this->spreadsheet->getSheetCount()) { |
574
|
1 |
|
$html .= '<div style="page-break-before:always" ></div>'; |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
// Next sheet |
579
|
549 |
|
++$sheetId; |
580
|
549 |
|
$sheet->setSelectedCells($selectedCells); |
581
|
|
|
} |
582
|
549 |
|
$this->spreadsheet->setActiveSheetIndex($activeSheet); |
583
|
|
|
|
584
|
549 |
|
return $html; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Generate sheet tabs. |
589
|
|
|
*/ |
590
|
518 |
|
public function generateNavigation(): string |
591
|
|
|
{ |
592
|
|
|
// Fetch sheets |
593
|
518 |
|
$sheets = []; |
594
|
518 |
|
if ($this->sheetIndex === null) { |
595
|
12 |
|
$sheets = $this->spreadsheet->getAllSheets(); |
596
|
|
|
} else { |
597
|
512 |
|
$sheets[] = $this->spreadsheet->getSheet($this->sheetIndex); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
// Construct HTML |
601
|
518 |
|
$html = ''; |
602
|
|
|
|
603
|
|
|
// Only if there are more than 1 sheets |
604
|
518 |
|
if (count($sheets) > 1) { |
605
|
|
|
// Loop all sheets |
606
|
12 |
|
$sheetId = 0; |
607
|
|
|
|
608
|
12 |
|
$html .= '<ul class="navigation">' . PHP_EOL; |
609
|
|
|
|
610
|
12 |
|
foreach ($sheets as $sheet) { |
611
|
12 |
|
$html .= ' <li class="sheet' . $sheetId . '"><a href="#sheet' . $sheetId . '">' . htmlspecialchars($sheet->getTitle()) . '</a></li>' . PHP_EOL; |
612
|
12 |
|
++$sheetId; |
613
|
|
|
} |
614
|
|
|
|
615
|
12 |
|
$html .= '</ul>' . PHP_EOL; |
616
|
|
|
} |
617
|
|
|
|
618
|
518 |
|
return $html; |
619
|
|
|
} |
620
|
|
|
|
621
|
549 |
|
private function extendRowsAndColumns(Worksheet $worksheet, int &$colMax, int &$rowMax): void |
622
|
|
|
{ |
623
|
549 |
|
if ($this->includeCharts) { |
624
|
4 |
|
foreach ($worksheet->getChartCollection() as $chart) { |
625
|
4 |
|
$chartCoordinates = $chart->getTopLeftPosition(); |
626
|
4 |
|
$this->sheetCharts[$chartCoordinates['cell']] = $chart; |
627
|
4 |
|
$chartTL = Coordinate::indexesFromString($chartCoordinates['cell']); |
628
|
4 |
|
if ($chartTL[1] > $rowMax) { |
629
|
1 |
|
$rowMax = $chartTL[1]; |
630
|
|
|
} |
631
|
4 |
|
if ($chartTL[0] > $colMax) { |
632
|
2 |
|
$colMax = $chartTL[0]; |
633
|
|
|
} |
634
|
|
|
} |
635
|
|
|
} |
636
|
549 |
|
foreach ($worksheet->getDrawingCollection() as $drawing) { |
637
|
27 |
|
if ($drawing instanceof Drawing && $drawing->getPath() === '') { |
638
|
2 |
|
continue; |
639
|
|
|
} |
640
|
26 |
|
$imageTL = Coordinate::indexesFromString($drawing->getCoordinates()); |
641
|
26 |
|
$this->sheetDrawings[$drawing->getCoordinates()] = $drawing; |
642
|
26 |
|
if ($imageTL[1] > $rowMax) { |
643
|
|
|
$rowMax = $imageTL[1]; |
644
|
|
|
} |
645
|
26 |
|
if ($imageTL[0] > $colMax) { |
646
|
|
|
$colMax = $imageTL[0]; |
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* Convert Windows file name to file protocol URL. |
653
|
|
|
* |
654
|
|
|
* @param string $filename file name on local system |
655
|
|
|
*/ |
656
|
20 |
|
public static function winFileToUrl(string $filename, bool $mpdf = false): string |
657
|
|
|
{ |
658
|
|
|
// Windows filename |
659
|
20 |
|
if (substr($filename, 1, 2) === ':\\') { |
660
|
1 |
|
$protocol = $mpdf ? '' : 'file:///'; |
661
|
1 |
|
$filename = $protocol . str_replace('\\', '/', $filename); |
662
|
|
|
} |
663
|
|
|
|
664
|
20 |
|
return $filename; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Generate image tag in cell. |
669
|
|
|
* |
670
|
|
|
* @param string $coordinates Cell coordinates |
671
|
|
|
*/ |
672
|
549 |
|
private function writeImageInCell(string $coordinates): string |
673
|
|
|
{ |
674
|
|
|
// Construct HTML |
675
|
549 |
|
$html = ''; |
676
|
|
|
|
677
|
|
|
// Write images |
678
|
549 |
|
$drawing = $this->sheetDrawings[$coordinates] ?? null; |
679
|
549 |
|
if ($drawing !== null) { |
680
|
26 |
|
$opacity = ''; |
681
|
26 |
|
$opacityValue = $drawing->getOpacity(); |
682
|
26 |
|
if ($opacityValue !== null) { |
683
|
3 |
|
$opacityValue = $opacityValue / 100000; |
684
|
3 |
|
if ($opacityValue >= 0.0 && $opacityValue <= 1.0) { |
685
|
3 |
|
$opacity = "opacity:$opacityValue; "; |
686
|
|
|
} |
687
|
|
|
} |
688
|
26 |
|
$filedesc = $drawing->getDescription(); |
689
|
26 |
|
$filedesc = $filedesc ? htmlspecialchars($filedesc, ENT_QUOTES) : 'Embedded image'; |
690
|
26 |
|
if ($drawing instanceof Drawing && $drawing->getPath() !== '') { |
691
|
19 |
|
$filename = $drawing->getPath(); |
692
|
|
|
|
693
|
|
|
// Strip off eventual '.' |
694
|
19 |
|
$filename = Preg::replace('/^[.]/', '', $filename); |
695
|
|
|
|
696
|
|
|
// Prepend images root |
697
|
19 |
|
$filename = $this->getImagesRoot() . $filename; |
698
|
|
|
|
699
|
|
|
// Strip off eventual '.' if followed by non-/ |
700
|
19 |
|
$filename = Preg::replace('@^[.]([^/])@', '$1', $filename); |
701
|
|
|
|
702
|
|
|
// Convert UTF8 data to PCDATA |
703
|
19 |
|
$filename = htmlspecialchars($filename, Settings::htmlEntityFlags()); |
704
|
|
|
|
705
|
19 |
|
$html .= PHP_EOL; |
706
|
19 |
|
$imageData = self::winFileToUrl($filename, $this instanceof Pdf\Mpdf); |
707
|
|
|
|
708
|
19 |
|
if ($this->embedImages || str_starts_with($imageData, 'zip://')) { |
709
|
11 |
|
$imageData = 'data:,'; |
710
|
11 |
|
$picture = @file_get_contents($filename); |
711
|
11 |
|
if ($picture !== false) { |
712
|
11 |
|
$mimeContentType = (string) @mime_content_type($filename); |
713
|
11 |
|
if (str_starts_with($mimeContentType, 'image/')) { |
714
|
|
|
// base64 encode the binary data |
715
|
11 |
|
$base64 = base64_encode($picture); |
716
|
11 |
|
$imageData = 'data:' . $mimeContentType . ';base64,' . $base64; |
717
|
|
|
} |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
|
721
|
19 |
|
$html .= '<img style="' . $opacity . 'position: absolute; z-index: 1; left: ' |
722
|
19 |
|
. $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px; width: ' |
723
|
19 |
|
. $drawing->getWidth() . 'px; height: ' . $drawing->getHeight() . 'px;" src="' |
724
|
19 |
|
. $imageData . '" alt="' . $filedesc . '" />'; |
725
|
7 |
|
} elseif ($drawing instanceof MemoryDrawing) { |
726
|
7 |
|
$imageResource = $drawing->getImageResource(); |
727
|
7 |
|
if ($imageResource) { |
728
|
7 |
|
ob_start(); // Let's start output buffering. |
729
|
7 |
|
imagepng($imageResource); // This will normally output the image, but because of ob_start(), it won't. |
730
|
7 |
|
$contents = (string) ob_get_contents(); // Instead, output above is saved to $contents |
731
|
7 |
|
ob_end_clean(); // End the output buffer. |
732
|
|
|
|
733
|
7 |
|
$dataUri = 'data:image/png;base64,' . base64_encode($contents); |
734
|
|
|
|
735
|
|
|
// Because of the nature of tables, width is more important than height. |
736
|
|
|
// max-width: 100% ensures that image doesnt overflow containing cell |
737
|
|
|
// However, PR #3535 broke test |
738
|
|
|
// 25_In_memory_image, apparently because |
739
|
|
|
// of the use of max-with. In addition, |
740
|
|
|
// non-memory-drawings don't use max-width. |
741
|
|
|
// Its use here is suspect and is being eliminated. |
742
|
|
|
// width: X sets width of supplied image. |
743
|
|
|
// As a result, images bigger than cell will be contained and images smaller will not get stretched |
744
|
7 |
|
$html .= '<img alt="' . $filedesc . '" src="' . $dataUri . '" style="' . $opacity . 'width:' . $drawing->getWidth() . 'px;left: ' |
745
|
7 |
|
. $drawing->getOffsetX() . 'px; top: ' . $drawing->getOffsetY() . 'px;position: absolute; z-index: 1;" />'; |
746
|
|
|
} |
747
|
|
|
} |
748
|
|
|
} |
749
|
|
|
|
750
|
549 |
|
return $html; |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Generate chart tag in cell. |
755
|
|
|
* This code should be exercised by sample: |
756
|
|
|
* Chart/32_Chart_read_write_PDF.php. |
757
|
|
|
*/ |
758
|
4 |
|
private function writeChartInCell(Worksheet $worksheet, string $coordinates): string |
759
|
|
|
{ |
760
|
|
|
// Construct HTML |
761
|
4 |
|
$html = ''; |
762
|
|
|
|
763
|
|
|
// Write charts |
764
|
4 |
|
$chart = $this->sheetCharts[$coordinates] ?? null; |
765
|
4 |
|
if ($chart !== null) { |
766
|
4 |
|
$chartCoordinates = $chart->getTopLeftPosition(); |
767
|
4 |
|
$chartFileName = File::sysGetTempDir() . '/' . uniqid('', true) . '.png'; |
768
|
4 |
|
$renderedWidth = $chart->getRenderedWidth(); |
769
|
4 |
|
$renderedHeight = $chart->getRenderedHeight(); |
770
|
4 |
|
if ($renderedWidth === null || $renderedHeight === null) { |
771
|
4 |
|
$this->adjustRendererPositions($chart, $worksheet); |
772
|
|
|
} |
773
|
4 |
|
$title = $chart->getTitle(); |
774
|
4 |
|
$caption = null; |
775
|
4 |
|
$filedesc = ''; |
776
|
4 |
|
if ($title !== null) { |
777
|
4 |
|
$calculatedTitle = $title->getCalculatedTitle($worksheet->getParent()); |
778
|
4 |
|
if ($calculatedTitle !== null) { |
779
|
2 |
|
$caption = $title->getCaption(); |
780
|
2 |
|
$title->setCaption($calculatedTitle); |
781
|
|
|
} |
782
|
4 |
|
$filedesc = $title->getCaptionText($worksheet->getParent()); |
783
|
|
|
} |
784
|
4 |
|
$renderSuccessful = $chart->render($chartFileName); |
785
|
4 |
|
$chart->setRenderedWidth($renderedWidth); |
786
|
4 |
|
$chart->setRenderedHeight($renderedHeight); |
787
|
4 |
|
if (isset($title, $caption)) { |
788
|
2 |
|
$title->setCaption($caption); |
789
|
|
|
} |
790
|
4 |
|
if (!$renderSuccessful) { |
791
|
|
|
return ''; |
792
|
|
|
} |
793
|
|
|
|
794
|
4 |
|
$html .= PHP_EOL; |
795
|
4 |
|
$imageDetails = getimagesize($chartFileName) ?: ['', '', 'mime' => '']; |
796
|
|
|
|
797
|
4 |
|
$filedesc = $filedesc ? htmlspecialchars($filedesc, ENT_QUOTES) : 'Embedded chart'; |
798
|
4 |
|
$picture = file_get_contents($chartFileName); |
799
|
4 |
|
unlink($chartFileName); |
800
|
4 |
|
if ($picture !== false) { |
801
|
4 |
|
$base64 = base64_encode($picture); |
802
|
4 |
|
$imageData = 'data:' . $imageDetails['mime'] . ';base64,' . $base64; |
803
|
|
|
|
804
|
4 |
|
$html .= '<img style="position: absolute; z-index: 1; left: ' . $chartCoordinates['xOffset'] . 'px; top: ' . $chartCoordinates['yOffset'] . 'px; width: ' . $imageDetails[0] . 'px; height: ' . $imageDetails[1] . 'px;" src="' . $imageData . '" alt="' . $filedesc . '" />' . PHP_EOL; |
805
|
|
|
} |
806
|
|
|
} |
807
|
|
|
|
808
|
|
|
// Return |
809
|
4 |
|
return $html; |
810
|
|
|
} |
811
|
|
|
|
812
|
4 |
|
private function adjustRendererPositions(Chart $chart, Worksheet $sheet): void |
813
|
|
|
{ |
814
|
4 |
|
$topLeft = $chart->getTopLeftPosition(); |
815
|
4 |
|
$bottomRight = $chart->getBottomRightPosition(); |
816
|
4 |
|
$tlCell = $topLeft['cell']; |
817
|
|
|
/** @var string */ |
818
|
4 |
|
$brCell = $bottomRight['cell']; |
819
|
4 |
|
if ($tlCell !== '' && $brCell !== '') { |
820
|
4 |
|
$tlCoordinate = Coordinate::indexesFromString($tlCell); |
821
|
4 |
|
$brCoordinate = Coordinate::indexesFromString($brCell); |
822
|
4 |
|
$totalHeight = 0.0; |
823
|
4 |
|
$totalWidth = 0.0; |
824
|
4 |
|
$defaultRowHeight = $sheet->getDefaultRowDimension()->getRowHeight(); |
825
|
4 |
|
$defaultRowHeight = SharedDrawing::pointsToPixels(($defaultRowHeight >= 0) ? $defaultRowHeight : SharedFont::getDefaultRowHeightByFont($this->defaultFont)); |
826
|
4 |
|
if ($tlCoordinate[1] <= $brCoordinate[1] && $tlCoordinate[0] <= $brCoordinate[0]) { |
827
|
4 |
|
for ($row = $tlCoordinate[1]; $row <= $brCoordinate[1]; ++$row) { |
828
|
4 |
|
$height = $sheet->getRowDimension($row)->getRowHeight('pt'); |
829
|
4 |
|
$totalHeight += ($height >= 0) ? $height : $defaultRowHeight; |
830
|
|
|
} |
831
|
4 |
|
$rightEdge = $brCoordinate[2]; |
832
|
4 |
|
++$rightEdge; |
833
|
4 |
|
for ($column = $tlCoordinate[2]; $column !== $rightEdge;) { |
834
|
4 |
|
$width = $sheet->getColumnDimension($column)->getWidth(); |
835
|
4 |
|
$width = ($width < 0) ? self::DEFAULT_CELL_WIDTH_PIXELS : SharedDrawing::cellDimensionToPixels($sheet->getColumnDimension($column)->getWidth(), $this->defaultFont); |
836
|
4 |
|
$totalWidth += $width; |
837
|
|
|
/** @var string $column */ |
838
|
4 |
|
++$column; |
839
|
|
|
} |
840
|
4 |
|
$chart->setRenderedWidth($totalWidth); |
841
|
4 |
|
$chart->setRenderedHeight($totalHeight); |
842
|
|
|
} |
843
|
|
|
} |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Generate CSS styles. |
848
|
|
|
* |
849
|
|
|
* @param bool $generateSurroundingHTML Generate surrounding HTML tags? (<style> and </style>) |
850
|
|
|
*/ |
851
|
543 |
|
public function generateStyles(bool $generateSurroundingHTML = true): string |
852
|
|
|
{ |
853
|
|
|
// Build CSS |
854
|
543 |
|
$css = $this->buildCSS($generateSurroundingHTML); |
855
|
|
|
|
856
|
|
|
// Construct HTML |
857
|
543 |
|
$html = ''; |
858
|
|
|
|
859
|
|
|
// Start styles |
860
|
543 |
|
if ($generateSurroundingHTML) { |
861
|
543 |
|
$html .= ' <style type="text/css">' . PHP_EOL; |
862
|
543 |
|
$html .= (array_key_exists('html', $css)) ? (' html { ' . $this->assembleCSS($css['html']) . ' }' . PHP_EOL) : ''; |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
// Write all other styles |
866
|
543 |
|
foreach ($css as $styleName => $styleDefinition) { |
867
|
543 |
|
if ($styleName != 'html') { |
868
|
543 |
|
$html .= ' ' . $styleName . ' { ' . $this->assembleCSS($styleDefinition) . ' }' . PHP_EOL; |
869
|
|
|
} |
870
|
|
|
} |
871
|
543 |
|
$html .= $this->generatePageDeclarations(false); |
872
|
|
|
|
873
|
|
|
// End styles |
874
|
543 |
|
if ($generateSurroundingHTML) { |
875
|
543 |
|
$html .= ' </style>' . PHP_EOL; |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
// Return |
879
|
543 |
|
return $html; |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
/** @param string[][] $css */ |
883
|
551 |
|
private function buildCssRowHeights(Worksheet $sheet, array &$css, int $sheetIndex): void |
884
|
|
|
{ |
885
|
|
|
// Calculate row heights |
886
|
551 |
|
foreach ($sheet->getRowDimensions() as $rowDimension) { |
887
|
32 |
|
$row = $rowDimension->getRowIndex() - 1; |
888
|
|
|
|
889
|
|
|
// table.sheetN tr.rowYYYYYY { } |
890
|
32 |
|
$css['table.sheet' . $sheetIndex . ' tr.row' . $row] = []; |
891
|
|
|
|
892
|
32 |
|
if ($rowDimension->getRowHeight() != -1) { |
893
|
22 |
|
$pt_height = $rowDimension->getRowHeight(); |
894
|
22 |
|
$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'] = $pt_height . 'pt'; |
895
|
|
|
} |
896
|
32 |
|
if ($rowDimension->getVisible() === false) { |
897
|
8 |
|
$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['display'] = 'none'; |
898
|
8 |
|
$css['table.sheet' . $sheetIndex . ' tr.row' . $row]['visibility'] = 'hidden'; |
899
|
|
|
} |
900
|
|
|
} |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
/** @param string[][] $css */ |
904
|
551 |
|
private function buildCssPerSheet(Worksheet $sheet, array &$css): void |
905
|
|
|
{ |
906
|
|
|
// Calculate hash code |
907
|
551 |
|
$sheetIndex = $sheet->getParentOrThrow()->getIndex($sheet); |
908
|
551 |
|
$setup = $sheet->getPageSetup(); |
909
|
551 |
|
if ($setup->getFitToPage() && $setup->getFitToHeight() === 1) { |
910
|
11 |
|
$css["table.sheet$sheetIndex"]['page-break-inside'] = 'avoid'; |
911
|
11 |
|
$css["table.sheet$sheetIndex"]['break-inside'] = 'avoid'; |
912
|
|
|
} |
913
|
551 |
|
$picture = $sheet->getBackgroundImage(); |
914
|
551 |
|
if ($picture !== '') { |
915
|
1 |
|
$base64 = base64_encode($picture); |
916
|
1 |
|
$css["table.sheet$sheetIndex"]['background-image'] = 'url(data:' . $sheet->getBackgroundMime() . ';base64,' . $base64 . ')'; |
917
|
|
|
} |
918
|
|
|
|
919
|
|
|
// Build styles |
920
|
|
|
// Calculate column widths |
921
|
551 |
|
$sheet->calculateColumnWidths(); |
922
|
|
|
|
923
|
|
|
// col elements, initialize |
924
|
551 |
|
$highestColumnIndex = Coordinate::columnIndexFromString($sheet->getHighestColumn()) - 1; |
925
|
551 |
|
$column = -1; |
926
|
551 |
|
$colStr = 'A'; |
927
|
551 |
|
while ($column++ < $highestColumnIndex) { |
928
|
551 |
|
$this->columnWidths[$sheetIndex][$column] = self::DEFAULT_CELL_WIDTH_POINTS; // approximation |
929
|
551 |
|
if ($this->shouldGenerateColumn($sheet, $colStr)) { |
930
|
551 |
|
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = self::DEFAULT_CELL_WIDTH_POINTS . 'pt'; |
931
|
|
|
} |
932
|
551 |
|
++$colStr; |
933
|
|
|
} |
934
|
|
|
|
935
|
|
|
// col elements, loop through columnDimensions and set width |
936
|
551 |
|
foreach ($sheet->getColumnDimensions() as $columnDimension) { |
937
|
40 |
|
$column = Coordinate::columnIndexFromString($columnDimension->getColumnIndex()) - 1; |
938
|
40 |
|
$width = SharedDrawing::cellDimensionToPixels($columnDimension->getWidth(), $this->defaultFont); |
939
|
40 |
|
$width = SharedDrawing::pixelsToPoints($width); |
940
|
40 |
|
if ($columnDimension->getVisible() === false) { |
941
|
10 |
|
$css['table.sheet' . $sheetIndex . ' .column' . $column]['display'] = 'none'; |
942
|
|
|
// This would be better but Firefox has an 11-year-old bug. |
943
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=819045 |
944
|
|
|
//$css['table.sheet' . $sheetIndex . ' col.col' . $column]['visibility'] = 'collapse'; |
945
|
|
|
} |
946
|
40 |
|
if ($width >= 0) { |
947
|
29 |
|
$this->columnWidths[$sheetIndex][$column] = $width; |
948
|
29 |
|
$css['table.sheet' . $sheetIndex . ' col.col' . $column]['width'] = $width . 'pt'; |
949
|
|
|
} |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
// Default row height |
953
|
551 |
|
$rowDimension = $sheet->getDefaultRowDimension(); |
954
|
|
|
|
955
|
|
|
// table.sheetN tr { } |
956
|
551 |
|
$css['table.sheet' . $sheetIndex . ' tr'] = []; |
957
|
|
|
|
958
|
551 |
|
if ($rowDimension->getRowHeight() == -1) { |
959
|
533 |
|
$pt_height = SharedFont::getDefaultRowHeightByFont($this->spreadsheet->getDefaultStyle()->getFont()); |
960
|
|
|
} else { |
961
|
18 |
|
$pt_height = $rowDimension->getRowHeight(); |
962
|
|
|
} |
963
|
551 |
|
$css['table.sheet' . $sheetIndex . ' tr']['height'] = $pt_height . 'pt'; |
964
|
551 |
|
if ($rowDimension->getVisible() === false) { |
965
|
1 |
|
$css['table.sheet' . $sheetIndex . ' tr']['display'] = 'none'; |
966
|
1 |
|
$css['table.sheet' . $sheetIndex . ' tr']['visibility'] = 'hidden'; |
967
|
|
|
} |
968
|
|
|
|
969
|
551 |
|
$this->buildCssRowHeights($sheet, $css, $sheetIndex); |
970
|
|
|
} |
971
|
|
|
|
972
|
|
|
/** |
973
|
|
|
* Build CSS styles. |
974
|
|
|
* |
975
|
|
|
* @param bool $generateSurroundingHTML Generate surrounding HTML style? (html { }) |
976
|
|
|
* |
977
|
|
|
* @return string[][] |
978
|
|
|
*/ |
979
|
551 |
|
public function buildCSS(bool $generateSurroundingHTML = true): array |
980
|
|
|
{ |
981
|
|
|
// Cached? |
982
|
551 |
|
if ($this->cssStyles !== null) { |
983
|
541 |
|
return $this->cssStyles; |
984
|
|
|
} |
985
|
|
|
|
986
|
|
|
// Ensure that spans have been calculated |
987
|
551 |
|
$this->calculateSpans(); |
988
|
|
|
|
989
|
|
|
// Construct CSS |
990
|
|
|
/** @var string[][] */ |
991
|
551 |
|
$css = []; |
992
|
|
|
|
993
|
|
|
// Start styles |
994
|
551 |
|
if ($generateSurroundingHTML) { |
995
|
|
|
// html { } |
996
|
541 |
|
$css['html']['font-family'] = 'Calibri, Arial, Helvetica, sans-serif'; |
997
|
541 |
|
$css['html']['font-size'] = '11pt'; |
998
|
541 |
|
$css['html']['background-color'] = 'white'; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
// CSS for comments as found in LibreOffice |
1002
|
551 |
|
$css['a.comment-indicator:hover + div.comment'] = [ |
1003
|
551 |
|
'background' => '#ffd', |
1004
|
551 |
|
'position' => 'absolute', |
1005
|
551 |
|
'display' => 'block', |
1006
|
551 |
|
'border' => '1px solid black', |
1007
|
551 |
|
'padding' => '0.5em', |
1008
|
551 |
|
]; |
1009
|
|
|
|
1010
|
551 |
|
$css['a.comment-indicator'] = [ |
1011
|
551 |
|
'background' => 'red', |
1012
|
551 |
|
'display' => 'inline-block', |
1013
|
551 |
|
'border' => '1px solid black', |
1014
|
551 |
|
'width' => '0.5em', |
1015
|
551 |
|
'height' => '0.5em', |
1016
|
551 |
|
]; |
1017
|
|
|
|
1018
|
551 |
|
$css['div.comment']['display'] = 'none'; |
1019
|
|
|
|
1020
|
|
|
// table { } |
1021
|
551 |
|
$css['table']['border-collapse'] = 'collapse'; |
1022
|
|
|
|
1023
|
|
|
// .b {} |
1024
|
551 |
|
$css['.b']['text-align'] = 'center'; // BOOL |
1025
|
|
|
|
1026
|
|
|
// .e {} |
1027
|
551 |
|
$css['.e']['text-align'] = 'center'; // ERROR |
1028
|
|
|
|
1029
|
|
|
// .f {} |
1030
|
551 |
|
$css['.f']['text-align'] = 'right'; // FORMULA |
1031
|
|
|
|
1032
|
|
|
// .inlineStr {} |
1033
|
551 |
|
$css['.inlineStr']['text-align'] = 'left'; // INLINE |
1034
|
|
|
|
1035
|
|
|
// .n {} |
1036
|
551 |
|
$css['.n']['text-align'] = 'right'; // NUMERIC |
1037
|
|
|
|
1038
|
|
|
// .s {} |
1039
|
551 |
|
$css['.s']['text-align'] = 'left'; // STRING |
1040
|
|
|
|
1041
|
551 |
|
$css['.floatright']['float'] = 'right'; |
1042
|
551 |
|
$css['.floatleft']['float'] = 'left'; |
1043
|
|
|
|
1044
|
|
|
// Calculate cell style hashes |
1045
|
551 |
|
foreach ($this->spreadsheet->getCellXfCollection() as $index => $style) { |
1046
|
551 |
|
$css['td.style' . $index . ', th.style' . $index] = $this->createCSSStyle($style); |
1047
|
|
|
//$css['th.style' . $index] = $this->createCSSStyle($style); |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
|
|
// Fetch sheets |
1051
|
551 |
|
$sheets = []; |
1052
|
551 |
|
if ($this->sheetIndex === null) { |
1053
|
19 |
|
$sheets = $this->spreadsheet->getAllSheets(); |
1054
|
|
|
} else { |
1055
|
539 |
|
$sheets[] = $this->spreadsheet->getSheet($this->sheetIndex); |
1056
|
|
|
} |
1057
|
|
|
|
1058
|
|
|
// Build styles per sheet |
1059
|
551 |
|
foreach ($sheets as $sheet) { |
1060
|
551 |
|
$this->buildCssPerSheet($sheet, $css); |
1061
|
|
|
} |
1062
|
|
|
|
1063
|
|
|
// Cache |
1064
|
551 |
|
if ($this->cssStyles === null) { |
1065
|
551 |
|
$this->cssStyles = $css; |
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
// Return |
1069
|
551 |
|
return $css; |
1070
|
|
|
} |
1071
|
|
|
|
1072
|
|
|
/** |
1073
|
|
|
* Create CSS style. |
1074
|
|
|
* |
1075
|
|
|
* @return string[] |
1076
|
|
|
*/ |
1077
|
551 |
|
private function createCSSStyle(Style $style, bool $conditional = false): array |
1078
|
|
|
{ |
1079
|
|
|
// Create CSS |
1080
|
551 |
|
return array_merge( |
1081
|
551 |
|
$conditional ? [] : $this->createCSSStyleAlignment($style->getAlignment()), |
1082
|
551 |
|
$this->createCSSStyleBorders($style->getBorders()), |
1083
|
551 |
|
$this->createCSSStyleFont($style->getFont(), conditional: $conditional), |
1084
|
551 |
|
$this->createCSSStyleFill($style->getFill()) |
1085
|
551 |
|
); |
1086
|
|
|
} |
1087
|
|
|
|
1088
|
|
|
/** |
1089
|
|
|
* Create CSS style. |
1090
|
|
|
* |
1091
|
|
|
* @return string[] |
1092
|
|
|
*/ |
1093
|
551 |
|
private function createCSSStyleAlignment(Alignment $alignment): array |
1094
|
|
|
{ |
1095
|
|
|
// Construct CSS |
1096
|
551 |
|
$css = []; |
1097
|
|
|
|
1098
|
|
|
// Create CSS |
1099
|
551 |
|
$verticalAlign = $this->mapVAlign($alignment->getVertical() ?? ''); |
1100
|
551 |
|
if ($verticalAlign) { |
1101
|
551 |
|
$css['vertical-align'] = $verticalAlign; |
1102
|
|
|
} |
1103
|
551 |
|
$textAlign = $this->mapHAlign($alignment->getHorizontal() ?? ''); |
1104
|
551 |
|
if ($textAlign) { |
1105
|
18 |
|
$css['text-align'] = $textAlign; |
1106
|
18 |
|
if (in_array($textAlign, ['left', 'right'])) { |
1107
|
11 |
|
$css['padding-' . $textAlign] = (string) ((int) $alignment->getIndent() * 9) . 'px'; |
1108
|
|
|
} |
1109
|
|
|
} |
1110
|
551 |
|
$rotation = $alignment->getTextRotation(); |
1111
|
551 |
|
if ($rotation !== 0 && $rotation !== Alignment::TEXTROTATION_STACK_PHPSPREADSHEET) { |
1112
|
5 |
|
if ($this instanceof Pdf\Mpdf) { |
1113
|
1 |
|
$css['text-rotate'] = "$rotation"; |
1114
|
|
|
} else { |
1115
|
4 |
|
$css['transform'] = "rotate({$rotation}deg)"; |
1116
|
|
|
} |
1117
|
|
|
} |
1118
|
|
|
|
1119
|
551 |
|
return $css; |
1120
|
|
|
} |
1121
|
|
|
|
1122
|
|
|
/** |
1123
|
|
|
* Create CSS style. |
1124
|
|
|
* |
1125
|
|
|
* @return string[] |
1126
|
|
|
*/ |
1127
|
551 |
|
private function createCSSStyleFont(Font $font, bool $useDefaults = false, bool $conditional = false): array |
1128
|
|
|
{ |
1129
|
|
|
// Construct CSS |
1130
|
551 |
|
$css = []; |
1131
|
|
|
|
1132
|
|
|
// Create CSS |
1133
|
551 |
|
if ($font->getBold()) { |
1134
|
23 |
|
$css['font-weight'] = 'bold'; |
1135
|
551 |
|
} elseif ($useDefaults) { |
1136
|
14 |
|
$css['font-weight'] = 'normal'; |
1137
|
|
|
} |
1138
|
551 |
|
if ($font->getUnderline() != Font::UNDERLINE_NONE && $font->getStrikethrough()) { |
1139
|
1 |
|
$css['text-decoration'] = 'underline line-through'; |
1140
|
551 |
|
} elseif ($font->getUnderline() != Font::UNDERLINE_NONE) { |
1141
|
12 |
|
$css['text-decoration'] = 'underline'; |
1142
|
551 |
|
} elseif ($font->getStrikethrough()) { |
1143
|
1 |
|
$css['text-decoration'] = 'line-through'; |
1144
|
551 |
|
} elseif ($useDefaults) { |
1145
|
17 |
|
$css['text-decoration'] = 'normal'; |
1146
|
|
|
} |
1147
|
551 |
|
if ($font->getItalic()) { |
1148
|
12 |
|
$css['font-style'] = 'italic'; |
1149
|
551 |
|
} elseif ($useDefaults) { |
1150
|
17 |
|
$css['font-style'] = 'normal'; |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
551 |
|
$css['color'] = '#' . $font->getColor()->getRGB(); |
1154
|
551 |
|
if (!$conditional) { |
1155
|
551 |
|
$css['font-family'] = '\'' . htmlspecialchars((string) $font->getName(), ENT_QUOTES) . '\''; |
1156
|
551 |
|
$css['font-size'] = $font->getSize() . 'pt'; |
1157
|
|
|
} |
1158
|
|
|
|
1159
|
551 |
|
return $css; |
1160
|
|
|
} |
1161
|
|
|
|
1162
|
|
|
/** |
1163
|
|
|
* @param string[] $css |
1164
|
|
|
*/ |
1165
|
551 |
|
private function styleBorder(array &$css, string $index, Border $border): void |
1166
|
|
|
{ |
1167
|
551 |
|
$borderStyle = $border->getBorderStyle(); |
1168
|
|
|
// Mpdf doesn't process !important, so omit unimportant border none |
1169
|
551 |
|
if ($borderStyle === Border::BORDER_NONE && $this instanceof Pdf\Mpdf) { |
1170
|
25 |
|
return; |
1171
|
|
|
} |
1172
|
539 |
|
if ($borderStyle !== Border::BORDER_OMIT) { |
1173
|
539 |
|
$css[$index] = $this->createCSSStyleBorder($border); |
1174
|
|
|
} |
1175
|
|
|
} |
1176
|
|
|
|
1177
|
|
|
/** |
1178
|
|
|
* Create CSS style. |
1179
|
|
|
* |
1180
|
|
|
* @param Borders $borders Borders |
1181
|
|
|
* |
1182
|
|
|
* @return string[] |
1183
|
|
|
*/ |
1184
|
551 |
|
private function createCSSStyleBorders(Borders $borders): array |
1185
|
|
|
{ |
1186
|
|
|
// Construct CSS |
1187
|
551 |
|
$css = []; |
1188
|
|
|
|
1189
|
|
|
// Create CSS |
1190
|
551 |
|
$this->styleBorder($css, 'border-bottom', $borders->getBottom()); |
1191
|
551 |
|
$this->styleBorder($css, 'border-top', $borders->getTop()); |
1192
|
551 |
|
$this->styleBorder($css, 'border-left', $borders->getLeft()); |
1193
|
551 |
|
$this->styleBorder($css, 'border-right', $borders->getRight()); |
1194
|
|
|
|
1195
|
551 |
|
return $css; |
1196
|
|
|
} |
1197
|
|
|
|
1198
|
|
|
/** |
1199
|
|
|
* Create CSS style. |
1200
|
|
|
* |
1201
|
|
|
* @param Border $border Border |
1202
|
|
|
*/ |
1203
|
539 |
|
private function createCSSStyleBorder(Border $border): string |
1204
|
|
|
{ |
1205
|
|
|
// Create CSS - add !important to non-none border styles for merged cells |
1206
|
539 |
|
$borderStyle = $this->mapBorderStyle($border->getBorderStyle()); |
1207
|
|
|
|
1208
|
539 |
|
return $borderStyle . ' #' . $border->getColor()->getRGB() . (($borderStyle === self::BORDER_NONE) ? '' : ' !important'); |
1209
|
|
|
} |
1210
|
|
|
|
1211
|
|
|
/** |
1212
|
|
|
* Create CSS style (Fill). |
1213
|
|
|
* |
1214
|
|
|
* @param Fill $fill Fill |
1215
|
|
|
* |
1216
|
|
|
* @return string[] |
1217
|
|
|
*/ |
1218
|
551 |
|
private function createCSSStyleFill(Fill $fill): array |
1219
|
|
|
{ |
1220
|
|
|
// Construct HTML |
1221
|
551 |
|
$css = []; |
1222
|
|
|
|
1223
|
|
|
// Create CSS |
1224
|
551 |
|
if ($fill->getFillType() !== Fill::FILL_NONE) { |
1225
|
|
|
if ( |
1226
|
23 |
|
(in_array($fill->getFillType(), ['', Fill::FILL_SOLID], true) || !$fill->getEndColor()->getRGB()) |
1227
|
23 |
|
&& $fill->getStartColor()->getRGB() |
1228
|
|
|
) { |
1229
|
23 |
|
$value = '#' . $fill->getStartColor()->getRGB(); |
1230
|
23 |
|
$css['background-color'] = $value; |
1231
|
9 |
|
} elseif ($fill->getEndColor()->getRGB()) { |
1232
|
9 |
|
$value = '#' . $fill->getEndColor()->getRGB(); |
1233
|
9 |
|
$css['background-color'] = $value; |
1234
|
|
|
} |
1235
|
|
|
} |
1236
|
|
|
|
1237
|
551 |
|
return $css; |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
/** |
1241
|
|
|
* Generate HTML footer. |
1242
|
|
|
*/ |
1243
|
549 |
|
public function generateHTMLFooter(): string |
1244
|
|
|
{ |
1245
|
|
|
// Construct HTML |
1246
|
549 |
|
$html = ''; |
1247
|
549 |
|
$html .= ' </body>' . PHP_EOL; |
1248
|
549 |
|
$html .= '</html>' . PHP_EOL; |
1249
|
|
|
|
1250
|
549 |
|
return $html; |
1251
|
|
|
} |
1252
|
|
|
|
1253
|
549 |
|
private function getDir(Worksheet $worksheet): string |
1254
|
|
|
{ |
1255
|
549 |
|
if ($worksheet->getRightToLeft()) { |
1256
|
5 |
|
return " dir='rtl'"; |
1257
|
|
|
} |
1258
|
546 |
|
if ($this->rtlSheets) { |
1259
|
2 |
|
return " dir='ltr'"; |
1260
|
|
|
} |
1261
|
|
|
|
1262
|
544 |
|
return ''; |
1263
|
|
|
} |
1264
|
|
|
|
1265
|
549 |
|
private function getFloat(Worksheet $worksheet): string |
1266
|
|
|
{ |
1267
|
549 |
|
$float = ''; |
1268
|
549 |
|
if ($worksheet->getRightToLeft()) { |
1269
|
5 |
|
if ($this->ltrSheets) { |
1270
|
2 |
|
$float = ' floatright'; |
1271
|
|
|
} |
1272
|
|
|
} else { |
1273
|
546 |
|
if ($this->rtlSheets) { |
1274
|
2 |
|
$float = ' floatleft'; |
1275
|
|
|
} |
1276
|
|
|
} |
1277
|
|
|
|
1278
|
549 |
|
return $float; |
1279
|
|
|
} |
1280
|
|
|
|
1281
|
16 |
|
private function generateTableTagInline(Worksheet $worksheet, string $id): string |
1282
|
|
|
{ |
1283
|
16 |
|
$style = isset($this->cssStyles['table']) |
1284
|
16 |
|
? $this->assembleCSS($this->cssStyles['table']) : ''; |
1285
|
16 |
|
$rtl = $this->getDir($worksheet); |
1286
|
16 |
|
$float = $this->getFloat($worksheet); |
1287
|
16 |
|
$prntgrid = $worksheet->getPrintGridlines(); |
1288
|
16 |
|
$viewgrid = $this->isPdf ? $prntgrid : $worksheet->getShowGridlines(); |
1289
|
16 |
|
if ($viewgrid && $prntgrid) { |
1290
|
3 |
|
$html = " <table border='1' cellpadding='1'$rtl $id cellspacing='1' style='$style' class='gridlines gridlinesp$float'>" . PHP_EOL; |
1291
|
14 |
|
} elseif ($viewgrid) { |
1292
|
5 |
|
$html = " <table border='0' cellpadding='0'$rtl $id cellspacing='0' style='$style' class='gridlines$float'>" . PHP_EOL; |
1293
|
10 |
|
} elseif ($prntgrid) { |
1294
|
2 |
|
$html = " <table border='0' cellpadding='0'$rtl $id cellspacing='0' style='$style' class='gridlinesp$float'>" . PHP_EOL; |
1295
|
10 |
|
} elseif ($float === '') { |
1296
|
10 |
|
$html = " <table border='0' cellpadding='1'$rtl $id cellspacing='0' style='$style'>" . PHP_EOL; |
1297
|
|
|
} else { |
1298
|
|
|
$html = " <table border='0' cellpadding='1'$rtl $id cellspacing='0' style='$style' class='$float'>" . PHP_EOL; |
1299
|
|
|
} |
1300
|
|
|
|
1301
|
16 |
|
return $html; |
1302
|
|
|
} |
1303
|
|
|
|
1304
|
549 |
|
private function generateTableTag(Worksheet $worksheet, string $id, string &$html, int $sheetIndex): void |
1305
|
|
|
{ |
1306
|
549 |
|
if (!$this->useInlineCss) { |
1307
|
541 |
|
$rtl = $this->getDir($worksheet); |
1308
|
541 |
|
$float = $this->getFloat($worksheet); |
1309
|
541 |
|
$gridlines = $worksheet->getShowGridlines() ? ' gridlines' : ''; |
1310
|
541 |
|
$gridlinesp = $worksheet->getPrintGridlines() ? ' gridlinesp' : ''; |
1311
|
541 |
|
$html .= " <table border='0' cellpadding='0' cellspacing='0'$rtl $id class='sheet$sheetIndex$gridlines$gridlinesp$float'>" . PHP_EOL; |
1312
|
|
|
} else { |
1313
|
16 |
|
$html .= $this->generateTableTagInline($worksheet, $id); |
1314
|
|
|
} |
1315
|
|
|
} |
1316
|
|
|
|
1317
|
|
|
/** |
1318
|
|
|
* Generate table header. |
1319
|
|
|
* |
1320
|
|
|
* @param Worksheet $worksheet The worksheet for the table we are writing |
1321
|
|
|
* @param bool $showid whether or not to add id to table tag |
1322
|
|
|
*/ |
1323
|
549 |
|
private function generateTableHeader(Worksheet $worksheet, bool $showid = true): string |
1324
|
|
|
{ |
1325
|
549 |
|
$sheetIndex = $worksheet->getParentOrThrow()->getIndex($worksheet); |
1326
|
|
|
|
1327
|
|
|
// Construct HTML |
1328
|
549 |
|
$html = ''; |
1329
|
549 |
|
$id = $showid ? "id='sheet$sheetIndex'" : ''; |
1330
|
549 |
|
$clear = ($this->rtlSheets && $this->ltrSheets) ? '; clear:both' : ''; |
1331
|
|
|
|
1332
|
549 |
|
if ($showid) { |
1333
|
549 |
|
$html .= "<div style='page: page$sheetIndex$clear'>" . PHP_EOL; |
1334
|
|
|
} else { |
1335
|
2 |
|
$html .= "<div style='page: page$sheetIndex$clear' class='scrpgbrk'>" . PHP_EOL; |
1336
|
|
|
} |
1337
|
|
|
|
1338
|
549 |
|
$this->generateTableTag($worksheet, $id, $html, $sheetIndex); |
1339
|
|
|
|
1340
|
|
|
// Write <col> elements |
1341
|
549 |
|
$highestColumnIndex = Coordinate::columnIndexFromString($worksheet->getHighestColumn()) - 1; |
1342
|
549 |
|
$i = -1; |
1343
|
549 |
|
while ($i++ < $highestColumnIndex) { |
1344
|
549 |
|
if (!$this->useInlineCss) { |
1345
|
541 |
|
$html .= ' <col class="col' . $i . '" />' . PHP_EOL; |
1346
|
|
|
} else { |
1347
|
16 |
|
$style = isset($this->cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) |
1348
|
16 |
|
? $this->assembleCSS($this->cssStyles['table.sheet' . $sheetIndex . ' col.col' . $i]) : ''; |
1349
|
16 |
|
$html .= ' <col style="' . $style . '" />' . PHP_EOL; |
1350
|
|
|
} |
1351
|
|
|
} |
1352
|
|
|
|
1353
|
549 |
|
return $html; |
1354
|
|
|
} |
1355
|
|
|
|
1356
|
|
|
/** |
1357
|
|
|
* Generate table footer. |
1358
|
|
|
*/ |
1359
|
549 |
|
private function generateTableFooter(): string |
1360
|
|
|
{ |
1361
|
549 |
|
return ' </tbody></table>' . PHP_EOL . '</div>' . PHP_EOL; |
1362
|
|
|
} |
1363
|
|
|
|
1364
|
|
|
/** |
1365
|
|
|
* Generate row start. |
1366
|
|
|
* |
1367
|
|
|
* @param int $sheetIndex Sheet index (0-based) |
1368
|
|
|
* @param int $row row number |
1369
|
|
|
*/ |
1370
|
549 |
|
private function generateRowStart(Worksheet $worksheet, int $sheetIndex, int $row): string |
1371
|
|
|
{ |
1372
|
549 |
|
$html = ''; |
1373
|
549 |
|
if (count($worksheet->getBreaks()) > 0) { |
1374
|
2 |
|
$breaks = $worksheet->getRowBreaks(); |
1375
|
|
|
|
1376
|
|
|
// check if a break is needed before this row |
1377
|
2 |
|
if (isset($breaks['A' . $row])) { |
1378
|
|
|
// close table: </table> |
1379
|
2 |
|
$html .= $this->generateTableFooter(); |
1380
|
2 |
|
if ($this->isPdf && $this->useInlineCss) { |
1381
|
1 |
|
$html .= '<div style="page-break-before:always" />'; |
1382
|
|
|
} |
1383
|
|
|
|
1384
|
|
|
// open table again: <table> + <col> etc. |
1385
|
2 |
|
$html .= $this->generateTableHeader($worksheet, false); |
1386
|
2 |
|
$html .= '<tbody>' . PHP_EOL; |
1387
|
|
|
} |
1388
|
|
|
} |
1389
|
|
|
|
1390
|
|
|
// Write row start |
1391
|
549 |
|
if (!$this->useInlineCss) { |
1392
|
541 |
|
$html .= ' <tr class="row' . $row . '">' . PHP_EOL; |
1393
|
|
|
} else { |
1394
|
16 |
|
$style = isset($this->cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $row]) |
1395
|
16 |
|
? $this->assembleCSS($this->cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $row]) : ''; |
1396
|
|
|
|
1397
|
16 |
|
if ($style === '') { |
1398
|
15 |
|
$html .= ' <tr>' . PHP_EOL; |
1399
|
|
|
} else { |
1400
|
1 |
|
$html .= ' <tr style="' . $style . '">' . PHP_EOL; |
1401
|
|
|
} |
1402
|
|
|
} |
1403
|
|
|
|
1404
|
549 |
|
return $html; |
1405
|
|
|
} |
1406
|
|
|
|
1407
|
|
|
/** @return array{null|''|Cell, array{}|string, non-empty-string} */ |
1408
|
549 |
|
private function generateRowCellCss(Worksheet $worksheet, string $cellAddress, int $row, int $columnNumber): array |
1409
|
|
|
{ |
1410
|
549 |
|
$cell = ($cellAddress > '') ? $worksheet->getCellCollection()->get($cellAddress) : ''; |
1411
|
549 |
|
$coordinate = Coordinate::stringFromColumnIndex($columnNumber + 1) . ($row + 1); |
1412
|
549 |
|
if (!$this->useInlineCss) { |
1413
|
541 |
|
$cssClass = 'column' . $columnNumber; |
1414
|
|
|
} else { |
1415
|
16 |
|
$cssClass = []; |
1416
|
|
|
} |
1417
|
|
|
|
1418
|
549 |
|
return [$cell, $cssClass, $coordinate]; |
1419
|
|
|
} |
1420
|
|
|
|
1421
|
33 |
|
private function generateRowCellDataValueRich(RichText $richText, ?Font $defaultFont = null): string |
1422
|
|
|
{ |
1423
|
33 |
|
$cellData = ''; |
1424
|
|
|
// Loop through rich text elements |
1425
|
33 |
|
$elements = $richText->getRichTextElements(); |
1426
|
33 |
|
foreach ($elements as $element) { |
1427
|
|
|
// Rich text start? |
1428
|
33 |
|
$font = ($element instanceof Run) ? $element->getFont() : $defaultFont; |
1429
|
33 |
|
if ($element instanceof Run || $font !== null) { |
1430
|
17 |
|
$cellEnd = ''; |
1431
|
17 |
|
if ($font !== null) { |
1432
|
17 |
|
$cellData .= '<span style="' . $this->assembleCSS($this->createCSSStyleFont($font, true)) . '">'; |
1433
|
|
|
|
1434
|
17 |
|
if ($font->getSuperscript()) { |
1435
|
1 |
|
$cellData .= '<sup>'; |
1436
|
1 |
|
$cellEnd = '</sup>'; |
1437
|
17 |
|
} elseif ($font->getSubscript()) { |
1438
|
1 |
|
$cellData .= '<sub>'; |
1439
|
1 |
|
$cellEnd = '</sub>'; |
1440
|
|
|
} |
1441
|
|
|
} else { |
1442
|
|
|
$cellData .= '<span>'; |
1443
|
|
|
} |
1444
|
|
|
|
1445
|
|
|
// Convert UTF8 data to PCDATA |
1446
|
17 |
|
$cellText = $element->getText(); |
1447
|
17 |
|
$cellData .= htmlspecialchars($cellText, Settings::htmlEntityFlags()); |
1448
|
|
|
|
1449
|
17 |
|
$cellData .= $cellEnd; |
1450
|
|
|
|
1451
|
17 |
|
$cellData .= '</span>'; |
1452
|
|
|
} else { |
1453
|
|
|
// Convert UTF8 data to PCDATA |
1454
|
17 |
|
$cellText = $element->getText(); |
1455
|
17 |
|
$cellData .= htmlspecialchars($cellText, Settings::htmlEntityFlags()); |
1456
|
|
|
} |
1457
|
|
|
} |
1458
|
|
|
|
1459
|
33 |
|
return nl2br($cellData); |
1460
|
|
|
} |
1461
|
|
|
|
1462
|
547 |
|
private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, string &$cellData): void |
1463
|
|
|
{ |
1464
|
547 |
|
if ($cell->getValue() instanceof RichText) { |
1465
|
13 |
|
$cellData .= $this->generateRowCellDataValueRich($cell->getValue(), $cell->getStyle()->getFont()); |
1466
|
|
|
} else { |
1467
|
545 |
|
if ($this->preCalculateFormulas) { |
1468
|
|
|
try { |
1469
|
544 |
|
$origData = $cell->getCalculatedValue(); |
1470
|
|
|
} catch (CalculationException $exception) { |
1471
|
|
|
$origData = '#ERROR'; // mark as error, rather than crash everything |
1472
|
|
|
} |
1473
|
544 |
|
if ($this->betterBoolean && is_bool($origData)) { |
1474
|
4 |
|
$origData2 = $origData ? $this->getTrue : $this->getFalse; |
1475
|
|
|
} else { |
1476
|
544 |
|
$origData2 = $cell->getCalculatedValueString(); |
1477
|
|
|
} |
1478
|
|
|
} else { |
1479
|
1 |
|
$origData = $cell->getValue(); |
1480
|
1 |
|
if ($this->betterBoolean && is_bool($origData)) { |
1481
|
|
|
$origData2 = $origData ? $this->getTrue : $this->getFalse; |
1482
|
|
|
} else { |
1483
|
1 |
|
$origData2 = $cell->getValueString(); |
1484
|
|
|
} |
1485
|
|
|
} |
1486
|
545 |
|
$formatCode = $worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode(); |
1487
|
|
|
|
1488
|
545 |
|
$cellData = NumberFormat::toFormattedString( |
1489
|
545 |
|
$origData2, |
1490
|
545 |
|
$formatCode ?? NumberFormat::FORMAT_GENERAL, |
1491
|
545 |
|
[$this, 'formatColor'] |
1492
|
545 |
|
); |
1493
|
|
|
|
1494
|
545 |
|
if ($cellData === $origData) { |
1495
|
120 |
|
$cellData = htmlspecialchars($cellData, Settings::htmlEntityFlags()); |
1496
|
|
|
} |
1497
|
545 |
|
if ($worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont()->getSuperscript()) { |
1498
|
1 |
|
$cellData = '<sup>' . $cellData . '</sup>'; |
1499
|
545 |
|
} elseif ($worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont()->getSubscript()) { |
1500
|
1 |
|
$cellData = '<sub>' . $cellData . '</sub>'; |
1501
|
|
|
} |
1502
|
|
|
} |
1503
|
|
|
} |
1504
|
|
|
|
1505
|
|
|
/** @param string|string[] $cssClass */ |
1506
|
549 |
|
private function generateRowCellData(Worksheet $worksheet, null|Cell|string $cell, array|string &$cssClass): string |
1507
|
|
|
{ |
1508
|
549 |
|
$cellData = ' '; |
1509
|
549 |
|
if ($cell instanceof Cell) { |
1510
|
547 |
|
$cellData = ''; |
1511
|
|
|
// Don't know what this does, and no test cases. |
1512
|
|
|
//if ($cell->getParent() === null) { |
1513
|
|
|
// $cell->attach($worksheet); |
1514
|
|
|
//} |
1515
|
|
|
// Value |
1516
|
547 |
|
$this->generateRowCellDataValue($worksheet, $cell, $cellData); |
1517
|
|
|
|
1518
|
|
|
// Converts the cell content so that spaces occuring at beginning of each new line are replaced by |
1519
|
|
|
// Example: " Hello\n to the world" is converted to " Hello\n to the world" |
1520
|
547 |
|
$cellData = Preg::replace('/(?m)(?:^|\G) /', ' ', $cellData); |
1521
|
|
|
|
1522
|
|
|
// convert newline "\n" to '<br>' |
1523
|
547 |
|
$cellData = nl2br($cellData); |
1524
|
|
|
|
1525
|
|
|
// Extend CSS class? |
1526
|
547 |
|
$dataType = $cell->getDataType(); |
1527
|
547 |
|
if ($this->betterBoolean && $this->preCalculateFormulas && $dataType === DataType::TYPE_FORMULA) { |
1528
|
27 |
|
$calculatedValue = $cell->getCalculatedValue(); |
1529
|
27 |
|
if (is_bool($calculatedValue)) { |
1530
|
4 |
|
$dataType = DataType::TYPE_BOOL; |
1531
|
27 |
|
} elseif (is_numeric($calculatedValue)) { |
1532
|
20 |
|
$dataType = DataType::TYPE_NUMERIC; |
1533
|
19 |
|
} elseif (is_string($calculatedValue)) { |
1534
|
18 |
|
$dataType = DataType::TYPE_STRING; |
1535
|
|
|
} |
1536
|
|
|
} |
1537
|
547 |
|
if (!$this->useInlineCss && is_string($cssClass)) { |
1538
|
540 |
|
$cssClass .= ' style' . $cell->getXfIndex(); |
1539
|
540 |
|
$cssClass .= ' ' . $dataType; |
1540
|
15 |
|
} elseif (is_array($cssClass)) { |
1541
|
15 |
|
$index = $cell->getXfIndex(); |
1542
|
15 |
|
$styleIndex = 'td.style' . $index . ', th.style' . $index; |
1543
|
15 |
|
if (isset($this->cssStyles[$styleIndex])) { |
1544
|
15 |
|
$cssClass = array_merge($cssClass, $this->cssStyles[$styleIndex]); |
1545
|
|
|
} |
1546
|
|
|
|
1547
|
|
|
// General horizontal alignment: Actual horizontal alignment depends on dataType |
1548
|
15 |
|
$sharedStyle = $worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()); |
1549
|
|
|
if ( |
1550
|
15 |
|
$sharedStyle->getAlignment()->getHorizontal() == Alignment::HORIZONTAL_GENERAL |
1551
|
15 |
|
&& isset($this->cssStyles['.' . $cell->getDataType()]['text-align']) |
1552
|
|
|
) { |
1553
|
15 |
|
$cssClass['text-align'] = $this->cssStyles['.' . $dataType]['text-align']; |
1554
|
|
|
} |
1555
|
|
|
} |
1556
|
|
|
} else { |
1557
|
|
|
// Use default borders for empty cell |
1558
|
57 |
|
if (is_string($cssClass)) { |
1559
|
51 |
|
$cssClass .= ' style0'; |
1560
|
|
|
} |
1561
|
|
|
} |
1562
|
|
|
|
1563
|
549 |
|
return $cellData; |
1564
|
|
|
} |
1565
|
|
|
|
1566
|
549 |
|
private function generateRowIncludeCharts(Worksheet $worksheet, string $coordinate): string |
1567
|
|
|
{ |
1568
|
549 |
|
return $this->includeCharts ? $this->writeChartInCell($worksheet, $coordinate) : ''; |
1569
|
|
|
} |
1570
|
|
|
|
1571
|
549 |
|
private function generateRowSpans(string $html, int $rowSpan, int $colSpan): string |
1572
|
|
|
{ |
1573
|
549 |
|
$html .= ($colSpan > 1) ? (' colspan="' . $colSpan . '"') : ''; |
1574
|
549 |
|
$html .= ($rowSpan > 1) ? (' rowspan="' . $rowSpan . '"') : ''; |
1575
|
|
|
|
1576
|
549 |
|
return $html; |
1577
|
|
|
} |
1578
|
|
|
|
1579
|
|
|
/** |
1580
|
|
|
* @param string|string[] $cssClass |
1581
|
|
|
* @param Conditional[] $condStyles |
1582
|
|
|
*/ |
1583
|
549 |
|
private function generateRowWriteCell( |
1584
|
|
|
string &$html, |
1585
|
|
|
Worksheet $worksheet, |
1586
|
|
|
string $coordinate, |
1587
|
|
|
string $cellType, |
1588
|
|
|
string $cellData, |
1589
|
|
|
int $colSpan, |
1590
|
|
|
int $rowSpan, |
1591
|
|
|
array|string $cssClass, |
1592
|
|
|
int $colNum, |
1593
|
|
|
int $sheetIndex, |
1594
|
|
|
int $row, |
1595
|
|
|
array $condStyles = [] |
1596
|
|
|
): void { |
1597
|
|
|
// Image? |
1598
|
549 |
|
$htmlx = $this->writeImageInCell($coordinate); |
1599
|
|
|
// Chart? |
1600
|
549 |
|
$htmlx .= $this->generateRowIncludeCharts($worksheet, $coordinate); |
1601
|
|
|
// Column start |
1602
|
549 |
|
$html .= ' <' . $cellType; |
1603
|
549 |
|
if ($this->betterBoolean) { |
1604
|
548 |
|
$dataType = $worksheet->getCell($coordinate)->getDataType(); |
1605
|
548 |
|
if ($dataType === DataType::TYPE_BOOL) { |
1606
|
3 |
|
$html .= ' data-type="' . DataType::TYPE_BOOL . '"'; |
1607
|
548 |
|
} elseif ($dataType === DataType::TYPE_FORMULA && $this->preCalculateFormulas && is_bool($worksheet->getCell($coordinate)->getCalculatedValue())) { |
1608
|
4 |
|
$html .= ' data-type="' . DataType::TYPE_BOOL . '"'; |
1609
|
548 |
|
} elseif (is_numeric($cellData) && $worksheet->getCell($coordinate)->getDataType() === DataType::TYPE_STRING) { |
1610
|
3 |
|
$html .= ' data-type="' . DataType::TYPE_STRING . '"'; |
1611
|
|
|
} |
1612
|
|
|
} |
1613
|
549 |
|
$holdCss = ''; |
1614
|
549 |
|
if (!$this->useInlineCss && !$this->isPdf && is_string($cssClass)) { |
1615
|
516 |
|
$html .= ' class="' . $cssClass . '"'; |
1616
|
516 |
|
if ($htmlx) { |
1617
|
23 |
|
$html .= " style='position: relative;'"; |
1618
|
|
|
} |
1619
|
|
|
} else { |
1620
|
|
|
//** Necessary redundant code for the sake of \PhpOffice\PhpSpreadsheet\Writer\Pdf ** |
1621
|
|
|
// We must explicitly write the width of the <td> element because TCPDF |
1622
|
|
|
// does not recognize e.g. <col style="width:42pt"> |
1623
|
46 |
|
if ($this->useInlineCss) { |
1624
|
16 |
|
$xcssClass = is_array($cssClass) ? $cssClass : []; |
1625
|
|
|
} else { |
1626
|
32 |
|
if (is_string($cssClass)) { |
1627
|
32 |
|
$html .= ' class="' . $cssClass . '"'; |
1628
|
|
|
} |
1629
|
32 |
|
$xcssClass = []; |
1630
|
|
|
} |
1631
|
46 |
|
$width = 0; |
1632
|
46 |
|
$i = $colNum - 1; |
1633
|
46 |
|
$e = $colNum + $colSpan - 1; |
1634
|
46 |
|
while ($i++ < $e) { |
1635
|
46 |
|
if (isset($this->columnWidths[$sheetIndex][$i])) { |
1636
|
46 |
|
$width += $this->columnWidths[$sheetIndex][$i]; |
1637
|
|
|
} |
1638
|
|
|
} |
1639
|
46 |
|
$xcssClass['width'] = (string) $width . 'pt'; |
1640
|
|
|
// We must also explicitly write the height of the <td> element because TCPDF |
1641
|
|
|
// does not recognize e.g. <tr style="height:50pt"> |
1642
|
46 |
|
if (isset($this->cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $row]['height'])) { |
1643
|
8 |
|
$height = $this->cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $row]['height']; |
1644
|
8 |
|
$xcssClass['height'] = $height; |
1645
|
|
|
} |
1646
|
|
|
//** end of redundant code ** |
1647
|
46 |
|
if ($this->useInlineCss) { |
1648
|
16 |
|
foreach (['border-top', 'border-bottom', 'border-right', 'border-left'] as $borderType) { |
1649
|
16 |
|
if (($xcssClass[$borderType] ?? '') === 'none #000000') { |
1650
|
15 |
|
unset($xcssClass[$borderType]); |
1651
|
|
|
} |
1652
|
|
|
} |
1653
|
|
|
} |
1654
|
|
|
|
1655
|
46 |
|
if ($htmlx) { |
1656
|
11 |
|
$xcssClass['position'] = 'relative'; |
1657
|
|
|
} |
1658
|
|
|
/** @var string[] $xcssClass */ |
1659
|
46 |
|
$holdCss = $this->assembleCSS($xcssClass); |
1660
|
46 |
|
if ($this->useInlineCss) { |
1661
|
16 |
|
$prntgrid = $worksheet->getPrintGridlines(); |
1662
|
16 |
|
$viewgrid = $this->isPdf ? $prntgrid : $worksheet->getShowGridlines(); |
1663
|
16 |
|
if ($viewgrid && $prntgrid) { |
1664
|
3 |
|
$html .= ' class="gridlines gridlinesp"'; |
1665
|
14 |
|
} elseif ($viewgrid) { |
1666
|
5 |
|
$html .= ' class="gridlines"'; |
1667
|
10 |
|
} elseif ($prntgrid) { |
1668
|
2 |
|
$html .= ' class="gridlinesp"'; |
1669
|
|
|
} |
1670
|
|
|
} |
1671
|
|
|
} |
1672
|
|
|
|
1673
|
549 |
|
$html = $this->generateRowSpans($html, $rowSpan, $colSpan); |
1674
|
|
|
|
1675
|
549 |
|
$tables = $worksheet->getTablesWithStylesForCell($worksheet->getCell($coordinate)); |
1676
|
549 |
|
if (count($tables) > 0 || count($condStyles) > 0) { |
1677
|
12 |
|
$matched = false; // TODO the style gotten from the merger overrides everything |
1678
|
12 |
|
$styleMerger = new StyleMerger($worksheet->getCell($coordinate)->getStyle()); |
1679
|
12 |
|
if ($this->tableFormats) { |
1680
|
4 |
|
if (count($tables) > 0) { |
1681
|
4 |
|
foreach ($tables as $ts) { |
1682
|
|
|
/** @var Table $ts */ |
1683
|
4 |
|
$dxfsTableStyle = $ts->getStyle()->getTableDxfsStyle(); |
1684
|
4 |
|
if ($dxfsTableStyle !== null) { |
1685
|
|
|
/** @var int */ |
1686
|
4 |
|
$tableRow = $ts->getRowNumber($coordinate); |
1687
|
|
|
/** @var TableDxfsStyle $dxfsTableStyle */ |
1688
|
4 |
|
if ($tableRow === 0 && $dxfsTableStyle->getHeaderRowStyle() !== null) { |
1689
|
4 |
|
$styleMerger->mergeStyle($dxfsTableStyle->getHeaderRowStyle()); |
1690
|
4 |
|
$matched = true; |
1691
|
4 |
|
} elseif ($tableRow % 2 === 1 && $dxfsTableStyle->getFirstRowStripeStyle() !== null) { |
1692
|
4 |
|
$styleMerger->mergeStyle($dxfsTableStyle->getFirstRowStripeStyle()); |
1693
|
4 |
|
$matched = true; |
1694
|
4 |
|
} elseif ($tableRow % 2 === 0 && $dxfsTableStyle->getSecondRowStripeStyle() !== null) { |
1695
|
4 |
|
$styleMerger->mergeStyle($dxfsTableStyle->getSecondRowStripeStyle()); |
1696
|
4 |
|
$matched = true; |
1697
|
|
|
} |
1698
|
|
|
} |
1699
|
|
|
} |
1700
|
|
|
} |
1701
|
|
|
} |
1702
|
12 |
|
if (count($condStyles) > 0 && $this->conditionalFormatting) { |
1703
|
9 |
|
if ($worksheet->getConditionalRange($coordinate) !== null) { |
1704
|
9 |
|
$assessor = new CellStyleAssessor($worksheet->getCell($coordinate), $worksheet->getConditionalRange($coordinate)); |
1705
|
|
|
} else { |
1706
|
|
|
$assessor = new CellStyleAssessor($worksheet->getCell($coordinate), $coordinate); |
1707
|
|
|
} |
1708
|
9 |
|
$matchedStyle = $assessor->matchConditionsReturnNullIfNoneMatched($condStyles, $cellData, true); |
1709
|
|
|
|
1710
|
9 |
|
if ($matchedStyle !== null) { |
1711
|
9 |
|
$matched = true; |
1712
|
|
|
// this is really slow |
1713
|
9 |
|
$styleMerger->mergeStyle($matchedStyle); |
1714
|
|
|
} |
1715
|
|
|
} |
1716
|
12 |
|
if ($matched) { |
1717
|
11 |
|
$styles = $this->createCSSStyle($styleMerger->getStyle(), true); |
1718
|
11 |
|
$html .= ' style="'; |
1719
|
11 |
|
if ($holdCss !== '') { |
1720
|
1 |
|
$html .= "$holdCss; "; |
1721
|
1 |
|
$holdCss = ''; |
1722
|
|
|
} |
1723
|
11 |
|
foreach ($styles as $key => $value) { |
1724
|
11 |
|
if (!str_starts_with($key, 'border-') || $value !== 'none #000000') { |
1725
|
11 |
|
$html .= $key . ':' . $value . ';'; |
1726
|
|
|
} |
1727
|
|
|
} |
1728
|
11 |
|
$html .= '"'; |
1729
|
|
|
} |
1730
|
|
|
} |
1731
|
549 |
|
if ($holdCss !== '') { |
1732
|
46 |
|
$html .= ' style="' . $holdCss . '"'; |
1733
|
|
|
} |
1734
|
|
|
|
1735
|
549 |
|
$html .= '>'; |
1736
|
549 |
|
$html .= $htmlx; |
1737
|
|
|
|
1738
|
549 |
|
$html .= $this->writeComment($worksheet, $coordinate); |
1739
|
|
|
|
1740
|
|
|
// Cell data |
1741
|
549 |
|
$html .= $cellData; |
1742
|
|
|
|
1743
|
|
|
// Column end |
1744
|
549 |
|
$html .= '</' . $cellType . '>' . PHP_EOL; |
1745
|
|
|
} |
1746
|
|
|
|
1747
|
|
|
/** |
1748
|
|
|
* Generate row. |
1749
|
|
|
* |
1750
|
|
|
* @param array<int, string> $values Array containing cells in a row |
1751
|
|
|
* @param int $row Row number (0-based) |
1752
|
|
|
* @param string $cellType eg: 'td' |
1753
|
|
|
*/ |
1754
|
549 |
|
private function generateRow(Worksheet $worksheet, array $values, int $row, string $cellType): string |
1755
|
|
|
{ |
1756
|
|
|
// Sheet index |
1757
|
549 |
|
$sheetIndex = $worksheet->getParentOrThrow()->getIndex($worksheet); |
1758
|
549 |
|
$html = $this->generateRowStart($worksheet, $sheetIndex, $row); |
1759
|
|
|
|
1760
|
|
|
// Write cells |
1761
|
549 |
|
$colNum = 0; |
1762
|
549 |
|
$tcpdfInited = false; |
1763
|
549 |
|
foreach ($values as $key => $cellAddress) { |
1764
|
549 |
|
if ($this instanceof Pdf\Mpdf) { |
1765
|
25 |
|
$colNum = $key - 1; |
1766
|
532 |
|
} elseif ($this instanceof Pdf\Tcpdf) { |
1767
|
|
|
// It appears that Tcpdf requires first cell in tr. |
1768
|
8 |
|
$colNum = $key - 1; |
1769
|
8 |
|
if (!$tcpdfInited && $key !== 1) { |
1770
|
1 |
|
$tempspan = ($colNum > 1) ? " colspan='$colNum'" : ''; |
1771
|
1 |
|
$html .= "<td$tempspan></td>\n"; |
1772
|
|
|
} |
1773
|
8 |
|
$tcpdfInited = true; |
1774
|
|
|
} |
1775
|
549 |
|
[$cell, $cssClass, $coordinate] = $this->generateRowCellCss($worksheet, $cellAddress, $row, $colNum); |
1776
|
|
|
|
1777
|
|
|
// Cell Data |
1778
|
549 |
|
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass); |
1779
|
|
|
|
1780
|
|
|
// Get an array of all styles |
1781
|
549 |
|
$condStyles = $worksheet->getStyle($coordinate)->getConditionalStyles(); |
1782
|
|
|
|
1783
|
|
|
// Hyperlink? |
1784
|
549 |
|
if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) { |
1785
|
13 |
|
$url = $worksheet->getHyperlink($coordinate)->getUrl(); |
1786
|
13 |
|
$urlDecode1 = html_entity_decode($url, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); |
1787
|
13 |
|
$urlTrim = Preg::replace('/^\s+/u', '', $urlDecode1); |
1788
|
13 |
|
$parseScheme = Preg::isMatch('/^([\w\s\x00-\x1f]+):/u', strtolower($urlTrim), $matches); |
1789
|
13 |
|
if ($parseScheme && !in_array($matches[1], ['http', 'https', 'file', 'ftp', 'mailto', 's3'], true)) { |
1790
|
3 |
|
$cellData = htmlspecialchars($url, Settings::htmlEntityFlags()); |
1791
|
3 |
|
$cellData = self::replaceControlChars($cellData); |
1792
|
|
|
} else { |
1793
|
11 |
|
$tooltip = $worksheet->getHyperlink($coordinate)->getTooltip(); |
1794
|
11 |
|
$tooltipOut = empty($tooltip) ? '' : (' title="' . htmlspecialchars($tooltip) . '"'); |
1795
|
11 |
|
$cellData = '<a href="' |
1796
|
11 |
|
. htmlspecialchars($url) . '"' |
1797
|
11 |
|
. $tooltipOut |
1798
|
11 |
|
. '>' . $cellData . '</a>'; |
1799
|
|
|
} |
1800
|
|
|
} |
1801
|
|
|
|
1802
|
|
|
// Should the cell be written or is it swallowed by a rowspan or colspan? |
1803
|
549 |
|
$writeCell = !(isset($this->isSpannedCell[$worksheet->getParentOrThrow()->getIndex($worksheet)][$row + 1][$colNum]) |
1804
|
549 |
|
&& $this->isSpannedCell[$worksheet->getParentOrThrow()->getIndex($worksheet)][$row + 1][$colNum]); |
1805
|
|
|
|
1806
|
|
|
// Colspan and Rowspan |
1807
|
549 |
|
$colSpan = 1; |
1808
|
549 |
|
$rowSpan = 1; |
1809
|
549 |
|
if (isset($this->isBaseCell[$worksheet->getParentOrThrow()->getIndex($worksheet)][$row + 1][$colNum])) { |
1810
|
|
|
/** @var array<string, int> */ |
1811
|
21 |
|
$spans = $this->isBaseCell[$worksheet->getParentOrThrow()->getIndex($worksheet)][$row + 1][$colNum]; |
1812
|
21 |
|
$rowSpan = $spans['rowspan']; |
1813
|
21 |
|
$colSpan = $spans['colspan']; |
1814
|
|
|
|
1815
|
|
|
// Also apply style from last cell in merge to fix borders - |
1816
|
|
|
// relies on !important for non-none border declarations in createCSSStyleBorder |
1817
|
21 |
|
$endCellCoord = Coordinate::stringFromColumnIndex($colNum + $colSpan) . ($row + $rowSpan); |
1818
|
21 |
|
if (!$this->useInlineCss && is_string($cssClass)) { |
1819
|
18 |
|
$cssClass .= ' style' . $worksheet->getCell($endCellCoord)->getXfIndex(); |
1820
|
|
|
} else { |
1821
|
4 |
|
$endBorders = $this->spreadsheet->getCellXfByIndex($worksheet->getCell($endCellCoord)->getXfIndex())->getBorders(); |
1822
|
4 |
|
$altBorders = $this->createCSSStyleBorders($endBorders); |
1823
|
4 |
|
foreach ($altBorders as $altKey => $altValue) { |
1824
|
4 |
|
if (str_contains($altValue, '!important')) { |
1825
|
2 |
|
$cssClass[$altKey] = $altValue; |
1826
|
|
|
} |
1827
|
|
|
} |
1828
|
|
|
} |
1829
|
|
|
} |
1830
|
|
|
|
1831
|
|
|
// Write |
1832
|
549 |
|
if ($writeCell) { |
1833
|
549 |
|
$this->generateRowWriteCell($html, $worksheet, $coordinate, $cellType, $cellData, $colSpan, $rowSpan, $cssClass, $colNum, $sheetIndex, $row, $condStyles); |
1834
|
|
|
} |
1835
|
|
|
|
1836
|
|
|
// Next column |
1837
|
549 |
|
++$colNum; |
1838
|
|
|
} |
1839
|
|
|
|
1840
|
|
|
// Write row end |
1841
|
549 |
|
$html .= ' </tr>' . PHP_EOL; |
1842
|
|
|
|
1843
|
|
|
// Return |
1844
|
549 |
|
return $html; |
1845
|
|
|
} |
1846
|
|
|
|
1847
|
|
|
/** @param string[] $matches */ |
1848
|
2 |
|
private static function replaceNonAscii(array $matches): string |
1849
|
|
|
{ |
1850
|
2 |
|
return '&#' . mb_ord($matches[0], 'UTF-8') . ';'; |
1851
|
|
|
} |
1852
|
|
|
|
1853
|
3 |
|
private static function replaceControlChars(string $convert): string |
1854
|
|
|
{ |
1855
|
3 |
|
return (string) preg_replace_callback( |
1856
|
3 |
|
'/[\x00-\x1f]/', |
1857
|
3 |
|
[self::class, 'replaceNonAscii'], |
1858
|
3 |
|
$convert |
1859
|
3 |
|
); |
1860
|
|
|
} |
1861
|
|
|
|
1862
|
|
|
/** |
1863
|
|
|
* Takes array where of CSS properties / values and converts to CSS string. |
1864
|
|
|
* |
1865
|
|
|
* @param string[] $values |
1866
|
|
|
*/ |
1867
|
551 |
|
private function assembleCSS(array $values = []): string |
1868
|
|
|
{ |
1869
|
551 |
|
$pairs = []; |
1870
|
551 |
|
foreach ($values as $property => $value) { |
1871
|
551 |
|
$pairs[] = $property . ':' . $value; |
1872
|
|
|
} |
1873
|
551 |
|
$string = implode('; ', $pairs); |
1874
|
|
|
|
1875
|
551 |
|
return $string; |
1876
|
|
|
} |
1877
|
|
|
|
1878
|
|
|
/** |
1879
|
|
|
* Get images root. |
1880
|
|
|
*/ |
1881
|
19 |
|
public function getImagesRoot(): string |
1882
|
|
|
{ |
1883
|
19 |
|
return $this->imagesRoot; |
1884
|
|
|
} |
1885
|
|
|
|
1886
|
|
|
/** |
1887
|
|
|
* Set images root. |
1888
|
|
|
* |
1889
|
|
|
* @return $this |
1890
|
|
|
*/ |
1891
|
1 |
|
public function setImagesRoot(string $imagesRoot): static |
1892
|
|
|
{ |
1893
|
1 |
|
$this->imagesRoot = $imagesRoot; |
1894
|
|
|
|
1895
|
1 |
|
return $this; |
1896
|
|
|
} |
1897
|
|
|
|
1898
|
|
|
/** |
1899
|
|
|
* Get embed images. |
1900
|
|
|
*/ |
1901
|
5 |
|
public function getEmbedImages(): bool |
1902
|
|
|
{ |
1903
|
5 |
|
return $this->embedImages; |
1904
|
|
|
} |
1905
|
|
|
|
1906
|
|
|
/** |
1907
|
|
|
* Set embed images. |
1908
|
|
|
* |
1909
|
|
|
* @return $this |
1910
|
|
|
*/ |
1911
|
4 |
|
public function setEmbedImages(bool $embedImages): static |
1912
|
|
|
{ |
1913
|
4 |
|
$this->embedImages = $embedImages; |
1914
|
|
|
|
1915
|
4 |
|
return $this; |
1916
|
|
|
} |
1917
|
|
|
|
1918
|
|
|
/** |
1919
|
|
|
* Get use inline CSS? |
1920
|
|
|
*/ |
1921
|
1 |
|
public function getUseInlineCss(): bool |
1922
|
|
|
{ |
1923
|
1 |
|
return $this->useInlineCss; |
1924
|
|
|
} |
1925
|
|
|
|
1926
|
|
|
/** |
1927
|
|
|
* Set use inline CSS? |
1928
|
|
|
* |
1929
|
|
|
* @return $this |
1930
|
|
|
*/ |
1931
|
17 |
|
public function setUseInlineCss(bool $useInlineCss): static |
1932
|
|
|
{ |
1933
|
17 |
|
$this->useInlineCss = $useInlineCss; |
1934
|
|
|
|
1935
|
17 |
|
return $this; |
1936
|
|
|
} |
1937
|
|
|
|
1938
|
4 |
|
public function setTableFormats(bool $tableFormats): self |
1939
|
|
|
{ |
1940
|
4 |
|
$this->tableFormats = $tableFormats; |
1941
|
|
|
|
1942
|
4 |
|
return $this; |
1943
|
|
|
} |
1944
|
|
|
|
1945
|
9 |
|
public function setConditionalFormatting(bool $conditionalFormatting): self |
1946
|
|
|
{ |
1947
|
9 |
|
$this->conditionalFormatting = $conditionalFormatting; |
1948
|
|
|
|
1949
|
9 |
|
return $this; |
1950
|
|
|
} |
1951
|
|
|
|
1952
|
|
|
/** |
1953
|
|
|
* Add color to formatted string as inline style. |
1954
|
|
|
* |
1955
|
|
|
* @param string $value Plain formatted value without color |
1956
|
|
|
* @param string $format Format code |
1957
|
|
|
*/ |
1958
|
395 |
|
public function formatColor(string $value, string $format): string |
1959
|
|
|
{ |
1960
|
395 |
|
return self::formatColorStatic($value, $format); |
1961
|
|
|
} |
1962
|
|
|
|
1963
|
|
|
/** |
1964
|
|
|
* Add color to formatted string as inline style. |
1965
|
|
|
* |
1966
|
|
|
* @param string $value Plain formatted value without color |
1967
|
|
|
* @param string $format Format code |
1968
|
|
|
*/ |
1969
|
395 |
|
public static function formatColorStatic(string $value, string $format): string |
1970
|
|
|
{ |
1971
|
|
|
// Color information, e.g. [Red] is always at the beginning |
1972
|
395 |
|
$color = null; // initialize |
1973
|
395 |
|
$matches = []; |
1974
|
|
|
|
1975
|
395 |
|
$color_regex = '/^\[[a-zA-Z]+\]/'; |
1976
|
395 |
|
if (Preg::isMatch($color_regex, $format, $matches)) { |
1977
|
17 |
|
$color = str_replace(['[', ']'], '', $matches[0]); |
1978
|
17 |
|
$color = strtolower($color); |
1979
|
|
|
} |
1980
|
|
|
|
1981
|
|
|
// convert to PCDATA |
1982
|
395 |
|
$result = htmlspecialchars($value, Settings::htmlEntityFlags()); |
1983
|
|
|
|
1984
|
|
|
// color span tag |
1985
|
395 |
|
if ($color !== null) { |
1986
|
17 |
|
$result = '<span style="color:' . $color . '">' . $result . '</span>'; |
1987
|
|
|
} |
1988
|
|
|
|
1989
|
395 |
|
return $result; |
1990
|
|
|
} |
1991
|
|
|
|
1992
|
|
|
/** |
1993
|
|
|
* Calculate information about HTML colspan and rowspan which is not always the same as Excel's. |
1994
|
|
|
*/ |
1995
|
551 |
|
private function calculateSpans(): void |
1996
|
|
|
{ |
1997
|
551 |
|
if ($this->spansAreCalculated) { |
1998
|
551 |
|
return; |
1999
|
|
|
} |
2000
|
|
|
// Identify all cells that should be omitted in HTML due to cell merge. |
2001
|
|
|
// In HTML only the upper-left cell should be written and it should have |
2002
|
|
|
// appropriate rowspan / colspan attribute |
2003
|
551 |
|
$sheetIndexes = $this->sheetIndex !== null |
2004
|
551 |
|
? [$this->sheetIndex] : range(0, $this->spreadsheet->getSheetCount() - 1); |
2005
|
|
|
|
2006
|
551 |
|
foreach ($sheetIndexes as $sheetIndex) { |
2007
|
551 |
|
$sheet = $this->spreadsheet->getSheet($sheetIndex); |
2008
|
|
|
|
2009
|
551 |
|
$candidateSpannedRow = []; |
2010
|
|
|
|
2011
|
|
|
// loop through all Excel merged cells |
2012
|
551 |
|
foreach ($sheet->getMergeCells() as $cells) { |
2013
|
21 |
|
[$cells] = Coordinate::splitRange($cells); |
2014
|
21 |
|
$first = $cells[0]; |
2015
|
21 |
|
$last = $cells[1]; |
2016
|
|
|
|
2017
|
21 |
|
[$fc, $fr] = Coordinate::indexesFromString($first); |
2018
|
21 |
|
$fc = $fc - 1; |
2019
|
|
|
|
2020
|
21 |
|
[$lc, $lr] = Coordinate::indexesFromString($last); |
2021
|
21 |
|
$lc = $lc - 1; |
2022
|
|
|
|
2023
|
|
|
// loop through the individual cells in the individual merge |
2024
|
21 |
|
$r = $fr - 1; |
2025
|
21 |
|
while ($r++ < $lr) { |
2026
|
|
|
// also, flag this row as a HTML row that is candidate to be omitted |
2027
|
21 |
|
$candidateSpannedRow[$r] = $r; |
2028
|
|
|
|
2029
|
21 |
|
$c = $fc - 1; |
2030
|
21 |
|
while ($c++ < $lc) { |
2031
|
21 |
|
if (!($c == $fc && $r == $fr)) { |
2032
|
|
|
// not the upper-left cell (should not be written in HTML) |
2033
|
21 |
|
$this->isSpannedCell[$sheetIndex][$r][$c] = [ |
2034
|
21 |
|
'baseCell' => [$fr, $fc], |
2035
|
21 |
|
]; |
2036
|
|
|
} else { |
2037
|
|
|
// upper-left is the base cell that should hold the colspan/rowspan attribute |
2038
|
21 |
|
$this->isBaseCell[$sheetIndex][$r][$c] = [ |
2039
|
21 |
|
'xlrowspan' => $lr - $fr + 1, // Excel rowspan |
2040
|
21 |
|
'rowspan' => $lr - $fr + 1, // HTML rowspan, value may change |
2041
|
21 |
|
'xlcolspan' => $lc - $fc + 1, // Excel colspan |
2042
|
21 |
|
'colspan' => $lc - $fc + 1, // HTML colspan, value may change |
2043
|
21 |
|
]; |
2044
|
|
|
} |
2045
|
|
|
} |
2046
|
|
|
} |
2047
|
|
|
} |
2048
|
|
|
|
2049
|
551 |
|
$this->calculateSpansOmitRows($sheet, $sheetIndex, $candidateSpannedRow); |
2050
|
|
|
|
2051
|
|
|
// TODO: Same for columns |
2052
|
|
|
} |
2053
|
|
|
|
2054
|
|
|
// We have calculated the spans |
2055
|
551 |
|
$this->spansAreCalculated = true; |
2056
|
|
|
} |
2057
|
|
|
|
2058
|
|
|
/** @param int[] $candidateSpannedRow */ |
2059
|
551 |
|
private function calculateSpansOmitRows(Worksheet $sheet, int $sheetIndex, array $candidateSpannedRow): void |
2060
|
|
|
{ |
2061
|
|
|
// Identify which rows should be omitted in HTML. These are the rows where all the cells |
2062
|
|
|
// participate in a merge and the where base cells are somewhere above. |
2063
|
551 |
|
$countColumns = Coordinate::columnIndexFromString($sheet->getHighestColumn()); |
2064
|
551 |
|
foreach ($candidateSpannedRow as $rowIndex) { |
2065
|
21 |
|
if (isset($this->isSpannedCell[$sheetIndex][$rowIndex])) { |
2066
|
21 |
|
if (count($this->isSpannedCell[$sheetIndex][$rowIndex]) == $countColumns) { |
2067
|
9 |
|
$this->isSpannedRow[$sheetIndex][$rowIndex] = $rowIndex; |
2068
|
|
|
} |
2069
|
|
|
} |
2070
|
|
|
} |
2071
|
|
|
|
2072
|
|
|
// For each of the omitted rows we found above, the affected rowspans should be subtracted by 1 |
2073
|
551 |
|
if (isset($this->isSpannedRow[$sheetIndex])) { |
2074
|
9 |
|
foreach ($this->isSpannedRow[$sheetIndex] as $rowIndex) { |
2075
|
9 |
|
$adjustedBaseCells = []; |
2076
|
9 |
|
$c = -1; |
2077
|
9 |
|
$e = $countColumns - 1; |
2078
|
9 |
|
while ($c++ < $e) { |
2079
|
9 |
|
$baseCell = $this->isSpannedCell[$sheetIndex][$rowIndex][$c]['baseCell']; |
2080
|
|
|
|
2081
|
9 |
|
if (!in_array($baseCell, $adjustedBaseCells, true)) { |
2082
|
|
|
// subtract rowspan by 1 |
2083
|
|
|
/** @var array<int|string> $baseCell */ |
2084
|
9 |
|
--$this->isBaseCell[$sheetIndex][$baseCell[0]][$baseCell[1]]['rowspan']; |
2085
|
9 |
|
$adjustedBaseCells[] = $baseCell; |
2086
|
|
|
} |
2087
|
|
|
} |
2088
|
|
|
} |
2089
|
|
|
} |
2090
|
|
|
} |
2091
|
|
|
|
2092
|
|
|
/** |
2093
|
|
|
* Write a comment in the same format as LibreOffice. |
2094
|
|
|
* |
2095
|
|
|
* @see https://github.com/LibreOffice/core/blob/9fc9bf3240f8c62ad7859947ab8a033ac1fe93fa/sc/source/filter/html/htmlexp.cxx#L1073-L1092 |
2096
|
|
|
*/ |
2097
|
549 |
|
private function writeComment(Worksheet $worksheet, string $coordinate): string |
2098
|
|
|
{ |
2099
|
549 |
|
$result = ''; |
2100
|
549 |
|
if (!$this->isPdf && isset($worksheet->getComments()[$coordinate])) { |
2101
|
24 |
|
$sanitizedString = $this->generateRowCellDataValueRich($worksheet->getComment($coordinate)->getText()); |
2102
|
24 |
|
$dir = ($worksheet->getComment($coordinate)->getTextboxDirection() === Comment::TEXTBOX_DIRECTION_RTL) ? ' dir="rtl"' : ''; |
2103
|
24 |
|
$align = strtolower($worksheet->getComment($coordinate)->getAlignment()); |
2104
|
24 |
|
$alignment = Alignment::HORIZONTAL_ALIGNMENT_FOR_HTML[$align] ?? ''; |
2105
|
24 |
|
if ($alignment !== '') { |
2106
|
2 |
|
$alignment = " style=\"text-align:$alignment\""; |
2107
|
|
|
} |
2108
|
24 |
|
if ($sanitizedString !== '') { |
2109
|
24 |
|
$result .= '<a class="comment-indicator"></a>'; |
2110
|
24 |
|
$result .= "<div class=\"comment\"$dir$alignment>" . $sanitizedString . '</div>'; |
2111
|
24 |
|
$result .= PHP_EOL; |
2112
|
|
|
} |
2113
|
|
|
} |
2114
|
|
|
|
2115
|
549 |
|
return $result; |
2116
|
|
|
} |
2117
|
|
|
|
2118
|
520 |
|
public function getOrientation(): ?string |
2119
|
|
|
{ |
2120
|
|
|
// Expect Pdf classes to override this method. |
2121
|
520 |
|
return $this->isPdf ? PageSetup::ORIENTATION_PORTRAIT : null; |
2122
|
|
|
} |
2123
|
|
|
|
2124
|
|
|
/** |
2125
|
|
|
* Generate @page declarations. |
2126
|
|
|
*/ |
2127
|
551 |
|
private function generatePageDeclarations(bool $generateSurroundingHTML): string |
2128
|
|
|
{ |
2129
|
|
|
// Ensure that Spans have been calculated? |
2130
|
551 |
|
$this->calculateSpans(); |
2131
|
|
|
|
2132
|
|
|
// Fetch sheets |
2133
|
551 |
|
$sheets = []; |
2134
|
551 |
|
if ($this->sheetIndex === null) { |
2135
|
19 |
|
$sheets = $this->spreadsheet->getAllSheets(); |
2136
|
|
|
} else { |
2137
|
539 |
|
$sheets[] = $this->spreadsheet->getSheet($this->sheetIndex); |
2138
|
|
|
} |
2139
|
|
|
|
2140
|
|
|
// Construct HTML |
2141
|
551 |
|
$htmlPage = $generateSurroundingHTML ? ('<style type="text/css">' . PHP_EOL) : ''; |
2142
|
|
|
|
2143
|
|
|
// Loop all sheets |
2144
|
551 |
|
$sheetId = 0; |
2145
|
551 |
|
foreach ($sheets as $worksheet) { |
2146
|
551 |
|
$htmlPage .= "@page page$sheetId { "; |
2147
|
551 |
|
$left = StringHelper::formatNumber($worksheet->getPageMargins()->getLeft()) . 'in; '; |
2148
|
551 |
|
$htmlPage .= 'margin-left: ' . $left; |
2149
|
551 |
|
$right = StringHelper::FormatNumber($worksheet->getPageMargins()->getRight()) . 'in; '; |
2150
|
551 |
|
$htmlPage .= 'margin-right: ' . $right; |
2151
|
551 |
|
$top = StringHelper::FormatNumber($worksheet->getPageMargins()->getTop()) . 'in; '; |
2152
|
551 |
|
$htmlPage .= 'margin-top: ' . $top; |
2153
|
551 |
|
$bottom = StringHelper::FormatNumber($worksheet->getPageMargins()->getBottom()) . 'in; '; |
2154
|
551 |
|
$htmlPage .= 'margin-bottom: ' . $bottom; |
2155
|
551 |
|
$orientation = $this->getOrientation() ?? $worksheet->getPageSetup()->getOrientation(); |
2156
|
551 |
|
if ($orientation === PageSetup::ORIENTATION_LANDSCAPE) { |
2157
|
17 |
|
$htmlPage .= 'size: landscape; '; |
2158
|
535 |
|
} elseif ($orientation === PageSetup::ORIENTATION_PORTRAIT) { |
2159
|
13 |
|
$htmlPage .= 'size: portrait; '; |
2160
|
|
|
} |
2161
|
551 |
|
$htmlPage .= '}' . PHP_EOL; |
2162
|
551 |
|
++$sheetId; |
2163
|
|
|
} |
2164
|
551 |
|
$htmlPage .= implode(PHP_EOL, [ |
2165
|
551 |
|
'.navigation {page-break-after: always;}', |
2166
|
551 |
|
'.scrpgbrk, div + div {page-break-before: always;}', |
2167
|
551 |
|
'@media screen {', |
2168
|
551 |
|
' .gridlines td {border: 1px solid black;}', |
2169
|
551 |
|
' .gridlines th {border: 1px solid black;}', |
2170
|
551 |
|
' body>div {margin-top: 5px;}', |
2171
|
551 |
|
' body>div:first-child {margin-top: 0;}', |
2172
|
551 |
|
' .scrpgbrk {margin-top: 1px;}', |
2173
|
551 |
|
'}', |
2174
|
551 |
|
'@media print {', |
2175
|
551 |
|
' .gridlinesp td {border: 1px solid black;}', |
2176
|
551 |
|
' .gridlinesp th {border: 1px solid black;}', |
2177
|
551 |
|
' .navigation {display: none;}', |
2178
|
551 |
|
'}', |
2179
|
551 |
|
'', |
2180
|
551 |
|
]); |
2181
|
551 |
|
$htmlPage .= $generateSurroundingHTML ? ('</style>' . PHP_EOL) : ''; |
2182
|
|
|
|
2183
|
551 |
|
return $htmlPage; |
2184
|
|
|
} |
2185
|
|
|
|
2186
|
549 |
|
private function shouldGenerateRow(Worksheet $sheet, int $row): bool |
2187
|
|
|
{ |
2188
|
549 |
|
if (!($this instanceof Pdf\Mpdf || $this instanceof Pdf\Tcpdf)) { |
2189
|
526 |
|
return true; |
2190
|
|
|
} |
2191
|
|
|
|
2192
|
31 |
|
return $sheet->isRowVisible($row); |
2193
|
|
|
} |
2194
|
|
|
|
2195
|
551 |
|
private function shouldGenerateColumn(Worksheet $sheet, string $colStr): bool |
2196
|
|
|
{ |
2197
|
551 |
|
if (!($this instanceof Pdf\Mpdf || $this instanceof Pdf\Tcpdf)) { |
2198
|
528 |
|
return true; |
2199
|
|
|
} |
2200
|
31 |
|
if (!$sheet->columnDimensionExists($colStr)) { |
2201
|
30 |
|
return true; |
2202
|
|
|
} |
2203
|
|
|
|
2204
|
11 |
|
return $sheet->getColumnDimension($colStr)->getVisible(); |
2205
|
|
|
} |
2206
|
|
|
|
2207
|
1 |
|
public function getBetterBoolean(): bool |
2208
|
|
|
{ |
2209
|
1 |
|
return $this->betterBoolean; |
2210
|
|
|
} |
2211
|
|
|
|
2212
|
3 |
|
public function setBetterBoolean(bool $betterBoolean): self |
2213
|
|
|
{ |
2214
|
3 |
|
$this->betterBoolean = $betterBoolean; |
2215
|
|
|
|
2216
|
3 |
|
return $this; |
2217
|
|
|
} |
2218
|
|
|
} |
2219
|
|
|
|