-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathPhpWord.php
559 lines (502 loc) · 13.8 KB
/
PhpWord.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord;
use BadMethodCallException;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Style\AbstractStyle;
/**
* PHPWord main class.
*
* @method Collection\Titles getTitles()
* @method Collection\Footnotes getFootnotes()
* @method Collection\Endnotes getEndnotes()
* @method Collection\Charts getCharts()
* @method Collection\Comments getComments()
* @method int addBookmark(Element\Bookmark $bookmark)
* @method int addTitle(Element\Title $title)
* @method int addFootnote(Element\Footnote $footnote)
* @method int addEndnote(Element\Endnote $endnote)
* @method int addChart(Element\Chart $chart)
* @method int addComment(Element\Comment $comment)
* @method Style\Paragraph addParagraphStyle(string $styleName, mixed $styles)
* @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null)
* @method Style\Font addLinkStyle(string $styleName, mixed $styles)
* @method Style\Font addTitleStyle(mixed $depth, mixed $fontStyle, mixed $paragraphStyle = null)
* @method Style\Table addTableStyle(string $styleName, mixed $styleTable, mixed $styleFirstRow = null)
* @method Style\Numbering addNumberingStyle(string $styleName, mixed $styles)
*/
class PhpWord
{
/**
* Collection of sections.
*
* @var Section[]
*/
private $sections = [];
/**
* Collections.
*
* @var array
*/
private $collections = [];
/**
* Metadata.
*
* @var array
*
* @since 0.12.0
*/
private $metadata = [];
/**
* Style register.
*
* @var array
*/
private $styles = [];
/**
* Create new instance.
*
* Collections are created dynamically
*/
public function __construct()
{
// Reset Media and styles
Media::resetElements();
Settings::setDefaultRtl(null);
// Collection
$collections = ['Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts', 'Comments'];
foreach ($collections as $collection) {
$class = 'PhpOffice\\PhpWord\\Collection\\' . $collection;
$this->collections[$collection] = new $class();
}
// Metadata
$metadata = ['DocInfo', 'Settings', 'Compatibility'];
foreach ($metadata as $meta) {
$class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta;
$this->metadata[$meta] = new $class();
}
}
/**
* Dynamic function call to reduce static dependency.
*
* @since 0.12.0
*
* @param mixed $function
* @param mixed $args
*
* @return mixed
*/
public function __call($function, $args)
{
$function = strtolower($function);
$getCollection = [];
$addCollection = [];
$collections = ['Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment'];
foreach ($collections as $collection) {
$getCollection[] = strtolower("get{$collection}s");
$addCollection[] = strtolower("add{$collection}");
}
// Run get collection method
if (in_array($function, $getCollection)) {
$key = ucfirst(str_replace('get', '', $function));
return $this->collections[$key];
}
// Run add collection item method
if (in_array($function, $addCollection)) {
$key = ucfirst(str_replace('add', '', $function) . 's');
$collectionObject = $this->collections[$key];
return $collectionObject->addItem($args[0] ?? null);
}
// Exception
throw new BadMethodCallException("Method $function is not defined.");
}
/**
* Get document properties object.
*
* @return Metadata\DocInfo
*/
public function getDocInfo()
{
return $this->metadata['DocInfo'];
}
/**
* Get compatibility.
*
* @return Metadata\Compatibility
*
* @since 0.12.0
*/
public function getCompatibility()
{
return $this->metadata['Compatibility'];
}
/**
* Get compatibility.
*
* @return Metadata\Settings
*
* @since 0.14.0
*/
public function getSettings()
{
return $this->metadata['Settings'];
}
/**
* Get all sections.
*
* @return Section[]
*/
public function getSections()
{
return $this->sections;
}
/**
* Returns the section at the requested position.
*
* @param int $index
*
* @return null|Section
*/
public function getSection($index)
{
if (array_key_exists($index, $this->sections)) {
return $this->sections[$index];
}
return null;
}
/**
* Create new section.
*
* @param null|array|string $style
*
* @return Section
*/
public function addSection($style = null)
{
$section = new Section(count($this->sections) + 1, $style);
$section->setPhpWord($this);
$this->sections[] = $section;
return $section;
}
/**
* Sorts the sections using the callable passed.
*
* @see http://php.net/manual/en/function.usort.php for usage
*
* @param callable $sorter
*/
public function sortSections($sorter): void
{
usort($this->sections, $sorter);
}
/**
* Get default font name.
*
* @return string
*/
public function getDefaultFontName()
{
return Settings::getDefaultFontName();
}
/**
* Set default font name.
*
* @param string $fontName
*/
public function setDefaultFontName($fontName): void
{
Settings::setDefaultFontName($fontName);
}
/**
* Get default asian font name.
*/
public function getDefaultAsianFontName(): string
{
return Settings::getDefaultAsianFontName();
}
/**
* Set default asian font name.
*
* @param string $fontName
*/
public function setDefaultAsianFontName($fontName): void
{
Settings::setDefaultAsianFontName($fontName);
}
/**
* Set default font color.
*/
public function setDefaultFontColor(string $fontColor): void
{
Settings::setDefaultFontColor($fontColor);
}
/**
* Get default font color.
*/
public function getDefaultFontColor(): string
{
return Settings::getDefaultFontColor();
}
/**
* Get default font size.
*
* @return int
*/
public function getDefaultFontSize()
{
return Settings::getDefaultFontSize();
}
/**
* Set default font size.
*
* @param int $fontSize
*/
public function setDefaultFontSize($fontSize): void
{
Settings::setDefaultFontSize($fontSize);
}
/**
* Save to file or download.
*
* All exceptions should already been handled by the writers
*
* @param string $filename
* @param string $format
* @param bool $download
*
* @return bool
*/
public function save($filename, $format = 'Word2007', $download = false)
{
$mime = [
'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'ODText' => 'application/vnd.oasis.opendocument.text',
'RTF' => 'application/rtf',
'HTML' => 'text/html',
'PDF' => 'application/pdf',
];
$writer = IOFactory::createWriter($this, $format);
if ($download === true) {
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: ' . $mime[$format]);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$filename = 'php://output'; // Change filename to force download
}
$writer->save($filename);
return true;
}
/**
* Create new section.
*
* @deprecated 0.10.0
*
* @param array $settings
*
* @return Section
*
* @codeCoverageIgnore
*/
public function createSection($settings = null)
{
return $this->addSection($settings);
}
/**
* Get document properties object.
*
* @deprecated 0.12.0
*
* @return Metadata\DocInfo
*
* @codeCoverageIgnore
*/
public function getDocumentProperties()
{
return $this->getDocInfo();
}
/**
* Set document properties object.
*
* @deprecated 0.12.0
*
* @param Metadata\DocInfo $documentProperties
*
* @return self
*
* @codeCoverageIgnore
*/
public function setDocumentProperties($documentProperties)
{
$this->metadata['Document'] = $documentProperties;
return $this;
}
/**
* Add paragraph style.
*
* @param string $styleName
* @param AbstractStyle|array $styles
*
* @return \PhpOffice\PhpWord\Style\Paragraph
*/
public function addParagraphStyle($styleName, $styles)
{
return $this->setStyleValues($styleName, new Style\Paragraph(), $styles);
}
/**
* Add font style.
*
* @param string $styleName
* @param AbstractStyle|array $fontStyle
* @param AbstractStyle|array $paragraphStyle
*
* @return \PhpOffice\PhpWord\Style\Font
*/
public function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
{
return $this->setStyleValues($styleName, new Style\Font('text', $paragraphStyle), $fontStyle);
}
/**
* Add link style.
*
* @param string $styleName
* @param AbstractStyle|array $styles
*
* @return \PhpOffice\PhpWord\Style\Font
*/
public function addLinkStyle($styleName, $styles)
{
return $this->setStyleValues($styleName, new Style\Font('link'), $styles);
}
/**
* Add numbering style.
*
* @param string $styleName
* @param AbstractStyle|array $styleValues
*
* @return \PhpOffice\PhpWord\Style\Numbering
*
* @since 0.10.0
*/
public function addNumberingStyle($styleName, $styleValues)
{
return $this->setStyleValues($styleName, new Style\Numbering(), $styleValues);
}
/**
* Add title style.
*
* @param null|int $depth Provide null to set title font
* @param AbstractStyle|array $fontStyle
* @param AbstractStyle|array $paragraphStyle
*
* @return \PhpOffice\PhpWord\Style\Font
*/
public function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
{
if (empty($depth)) {
$styleName = 'Title';
} else {
$styleName = "Heading_{$depth}";
}
return $this->setStyleValues($styleName, new Style\Font('title', $paragraphStyle), $fontStyle);
}
/**
* Add table style.
*
* @param string $styleName
* @param array $styleTable
* @param null|array $styleFirstRow
*
* @return \PhpOffice\PhpWord\Style\Table
*/
public function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
{
return $this->setStyleValues($styleName, new Style\Table($styleTable, $styleFirstRow), null);
}
/**
* Set default paragraph style.
*
* @param AbstractStyle|array $styles Paragraph style definition
*
* @return \PhpOffice\PhpWord\Style\Paragraph
*/
public function setDefaultParagraphStyle($styles)
{
return $this->addParagraphStyle('Normal', $styles);
}
/**
* Get all styles.
*
* @return AbstractStyle[]
*/
public function getStyles()
{
$styles = Style::getStyles();
$index = Style::countStyles() + 1;
foreach ($this->styles AS $name => $style) {
if (isset($styles[$name])) {
$style->setIndex($styles[$name]->getIndex());
} else {
$style->setIndex($index);
$index ++;
}
$styles[$name] = $style;
}
return $styles;
}
/**
* Get style by name.
*
* @param string $styleName
*
* @return ?AbstractStyle Paragraph|Font|Table|Numbering
*/
public function getStyle($styleName)
{
return $this->styles[$styleName] ?? Style::getStyle($styleName);
}
/**
* Set style values and put it to style collection.
*
* The $styleValues could be an array or object
*
* @param string $name
* @param AbstractStyle $style
* @param AbstractStyle|array $value
*
* @return AbstractStyle
*/
private function setStyleValues($name, $style, $value = null)
{
if (!isset($this->styles[$name])) {
if ($value !== null) {
if (is_array($value)) {
$style->setStyleByArray($value);
} elseif ($value instanceof AbstractStyle) {
if (get_class($style) == get_class($value)) {
$style = $value;
}
}
}
$style->setStyleName($name);
$this->styles[$name] = $style;
}
return $this->styles[$name];
}
}