-
Notifications
You must be signed in to change notification settings - Fork 290
/
PdfRenderer.php
581 lines (524 loc) · 15.6 KB
/
PdfRenderer.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2021 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Report;
use Fisharebest\Webtrees\MediaFile;
use Fisharebest\Webtrees\Webtrees;
use League\Flysystem\FilesystemOperator;
use function count;
/**
* Class PdfRenderer
*/
class PdfRenderer extends AbstractRenderer
{
/**
* PDF compression - Zlib extension is required
*
* @var bool const
*/
private const COMPRESSION = true;
/**
* If true reduce the RAM memory usage by caching temporary data on filesystem (slower).
*
* @var bool const
*/
private const DISK_CACHE = false;
/**
* true means that the input text is unicode (PDF)
*
* @var bool const
*/
private const UNICODE = true;
// Font sub-setting in TCPDF is slow.
private const SUBSETTING = false;
public TcpdfWrapper $tcpdf;
/** @var array<ReportPdfFootnote> Array of elements in the footer notes */
public array $printedfootnotes = [];
/** @var float The last cell height */
public float $lastCellHeight = 0.0;
/** @var float The largest font size within a TextBox to calculate the height */
public float $largestFontHeight = 0.0;
/** @var int The last pictures page number */
public int $lastpicpage = 0;
/** @var PdfRenderer The current report. */
public $wt_report;
/**
* PDF Header -PDF
*
* @return void
*/
public function header(): void
{
foreach ($this->headerElements as $element) {
if ($element instanceof ReportBaseElement) {
$element->render($this);
} elseif ($element === 'footnotetexts') {
$this->footnotes();
} elseif ($element === 'addpage') {
$this->newPage();
}
}
}
/**
* PDF Body -PDF
*
* @return void
*/
public function body(): void
{
$this->tcpdf->AddPage();
foreach ($this->bodyElements as $key => $element) {
if ($element instanceof ReportBaseElement) {
$element->render($this);
} elseif ($element === 'footnotetexts') {
$this->footnotes();
} elseif ($element === 'addpage') {
$this->newPage();
}
}
}
/**
* PDF Footnotes -PDF
*
* @return void
*/
public function footnotes(): void
{
foreach ($this->printedfootnotes as $element) {
if ($this->tcpdf->GetY() + $element->getFootnoteHeight($this) > $this->tcpdf->getPageHeight()) {
$this->tcpdf->AddPage();
}
$element->renderFootnote($this);
if ($this->tcpdf->GetY() > $this->tcpdf->getPageHeight()) {
$this->tcpdf->AddPage();
}
}
}
/**
* PDF Footer -PDF
*
* @return void
*/
public function footer(): void
{
foreach ($this->footerElements as $element) {
if ($element instanceof ReportBaseElement) {
$element->render($this);
} elseif ($element === 'footnotetexts') {
$this->footnotes();
} elseif ($element === 'addpage') {
$this->newPage();
}
}
}
/**
* Remove the header.
*
* @param int $index
*
* @return void
*/
public function removeHeader(int $index): void
{
unset($this->headerElements[$index]);
}
/**
* Remove the body.
*
* @param int $index
*
* @return void
*/
public function removeBody(int $index): void
{
unset($this->bodyElements[$index]);
}
/**
* Remove the footer.
*
* @param int $index
*
* @return void
*/
public function removeFooter(int $index): void
{
unset($this->footerElements[$index]);
}
/**
* Clear the Header -PDF
*
* @return void
*/
public function clearHeader(): void
{
unset($this->headerElements);
$this->headerElements = [];
}
/**
* Set the report.
*
* @param PdfRenderer $report
*
* @return void
*/
public function setReport(PdfRenderer $report): void
{
$this->wt_report = $report;
}
/**
* Get the currently used style name -PDF
*
* @return string
*/
public function getCurrentStyle(): string
{
return $this->currentStyle;
}
/**
* Setup a style for usage -PDF
*
* @param string $s Style name
*
* @return void
*/
public function setCurrentStyle(string $s): void
{
$this->currentStyle = $s;
$style = $this->wt_report->getStyle($s);
$this->tcpdf->SetFont($style['font'], $style['style'], $style['size']);
}
/**
* Get the style -PDF
*
* @param string $s Style name
*
* @return array
*/
public function getStyle(string $s): array
{
if (!isset($this->wt_report->styles[$s])) {
$s = $this->getCurrentStyle();
$this->wt_report->styles[$s] = $s;
}
return $this->wt_report->styles[$s];
}
/**
* Add margin when static horizontal position is used -PDF
* RTL supported
*
* @param float $x Static position
*
* @return float
*/
public function addMarginX(float $x): float
{
$m = $this->tcpdf->getMargins();
if ($this->tcpdf->getRTL()) {
$x += $m['right'];
} else {
$x += $m['left'];
}
$this->tcpdf->SetX($x);
return $x;
}
/**
* Get the maximum line width to draw from the curren position -PDF
* RTL supported
*
* @return float
*/
public function getMaxLineWidth(): float
{
$m = $this->tcpdf->getMargins();
if ($this->tcpdf->getRTL()) {
return $this->tcpdf->getRemainingWidth() + $m['right'];
}
return $this->tcpdf->getRemainingWidth() + $m['left'];
}
/**
* Get the height of the footnote.
*
* @return float
*/
public function getFootnotesHeight(): float
{
$h = 0;
foreach ($this->printedfootnotes as $element) {
$h += $element->getHeight($this);
}
return $h;
}
/**
* Returns the the current font size height -PDF
*
* @return float
*/
public function getCurrentStyleHeight(): float
{
if ($this->currentStyle === '') {
return $this->wt_report->default_font_size;
}
$style = $this->wt_report->getStyle($this->currentStyle);
return (float) $style['size'];
}
/**
* Checks the Footnote and numbers them
*
* @param ReportPdfFootnote $footnote
*
* @return ReportPdfFootnote|bool object if already numbered, false otherwise
*/
public function checkFootnote(ReportPdfFootnote $footnote)
{
$ct = count($this->printedfootnotes);
$val = $footnote->getValue();
$i = 0;
while ($i < $ct) {
if ($this->printedfootnotes[$i]->getValue() == $val) {
// If this footnote already exist then set up the numbers for this object
$footnote->setNum($i + 1);
$footnote->setAddlink((string) ($i + 1));
return $this->printedfootnotes[$i];
}
$i++;
}
// If this Footnote has not been set up yet
$footnote->setNum($ct + 1);
$footnote->setAddlink((string) $this->tcpdf->AddLink());
$this->printedfootnotes[] = $footnote;
return false;
}
/**
* Used this function instead of AddPage()
* This function will make sure that images will not be overwritten
*
* @return void
*/
public function newPage(): void
{
if ($this->lastpicpage > $this->tcpdf->getPage()) {
$this->tcpdf->setPage($this->lastpicpage);
}
$this->tcpdf->AddPage();
}
/**
* Add a page if needed -PDF
*
* @param float $height Cell height
*
* @return bool true in case of page break, false otherwise
*/
public function checkPageBreakPDF(float $height): bool
{
return $this->tcpdf->checkPageBreak($height);
}
/**
* Returns the remaining width between the current position and margins -PDF
*
* @return float Remaining width
*/
public function getRemainingWidthPDF(): float
{
return $this->tcpdf->getRemainingWidth();
}
/**
* PDF Setup - ReportPdf
*
* @return void
*/
public function setup(): void
{
parent::setup();
$this->tcpdf = new TcpdfWrapper(
$this->orientation,
self::UNITS,
[$this->page_width, $this->page_height],
self::UNICODE,
'UTF-8',
self::DISK_CACHE
);
$this->tcpdf->SetMargins($this->left_margin, $this->top_margin, $this->right_margin);
$this->tcpdf->setHeaderMargin($this->header_margin);
$this->tcpdf->setFooterMargin($this->footer_margin);
$this->tcpdf->SetAutoPageBreak(true, $this->bottom_margin);
$this->tcpdf->setFontSubsetting(self::SUBSETTING);
$this->tcpdf->SetCompression(self::COMPRESSION);
$this->tcpdf->setRTL($this->rtl);
$this->tcpdf->SetCreator(Webtrees::NAME . ' ' . Webtrees::VERSION);
$this->tcpdf->SetAuthor($this->rauthor);
$this->tcpdf->SetTitle($this->title);
$this->tcpdf->SetSubject($this->rsubject);
$this->tcpdf->SetKeywords($this->rkeywords);
$this->tcpdf->SetHeaderData('', 0, $this->title);
$this->tcpdf->setHeaderFont([$this->default_font, '', $this->default_font_size]);
$this->setReport($this);
if ($this->show_generated_by) {
// The default style name for Generated by.... is 'genby'
$element = new ReportPdfCell(0, 10, 0, 'C', '', 'genby', 1, ReportBaseElement::CURRENT_POSITION, ReportBaseElement::CURRENT_POSITION, 0, 0, '', '', true);
$element->addText($this->generated_by);
$element->setUrl(Webtrees::URL);
$this->addElementToFooter($element);
}
}
/**
* Run the report.
*
* @return void
*/
public function run(): void
{
$this->body();
echo $this->tcpdf->Output('doc.pdf', 'S');
}
/**
* Create a new Cell object.
*
* @param int $width cell width (expressed in points)
* @param int $height cell height (expressed in points)
* @param mixed $border Border style
* @param string $align Text alignement
* @param string $bgcolor Background color code
* @param string $style The name of the text style
* @param int $ln Indicates where the current position should go after the call
* @param mixed $top Y-position
* @param mixed $left X-position
* @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 1
* @param int $stretch Stretch carachter mode
* @param string $bocolor Border color
* @param string $tcolor Text color
* @param bool $reseth
*
* @return ReportBaseCell
*/
public function createCell(int $width, int $height, $border, string $align, string $bgcolor, string $style, int $ln, $top, $left, int $fill, int $stretch, string $bocolor, string $tcolor, bool $reseth): ReportBaseCell
{
return new ReportPdfCell($width, $height, $border, $align, $bgcolor, $style, $ln, $top, $left, $fill, $stretch, $bocolor, $tcolor, $reseth);
}
/**
* Create a new TextBox object.
*
* @param float $width Text box width
* @param float $height Text box height
* @param bool $border
* @param string $bgcolor Background color code in HTML
* @param bool $newline
* @param float $left
* @param float $top
* @param bool $pagecheck
* @param string $style
* @param bool $fill
* @param bool $padding
* @param bool $reseth
*
* @return ReportBaseTextbox
*/
public function createTextBox(
float $width,
float $height,
bool $border,
string $bgcolor,
bool $newline,
float $left,
float $top,
bool $pagecheck,
string $style,
bool $fill,
bool $padding,
bool $reseth
): ReportBaseTextbox {
return new ReportPdfTextBox($width, $height, $border, $bgcolor, $newline, $left, $top, $pagecheck, $style, $fill, $padding, $reseth);
}
/**
* Create a text element.
*
* @param string $style
* @param string $color
*
* @return ReportBaseText
*/
public function createText(string $style, string $color): ReportBaseText
{
return new ReportPdfText($style, $color);
}
/**
* Create a new Footnote object.
*
* @param string $style Style name
*
* @return ReportBaseFootnote
*/
public function createFootnote(string $style): ReportBaseFootnote
{
return new ReportPdfFootnote($style);
}
/**
* Create a new image object.
*
* @param string $file Filename
* @param float $x
* @param float $y
* @param float $w Image width
* @param float $h Image height
* @param string $align L:left, C:center, R:right or empty to use x/y
* @param string $ln T:same line, N:next line
*
* @return ReportBaseImage
*/
public function createImage(string $file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage
{
return new ReportPdfImage($file, $x, $y, $w, $h, $align, $ln);
}
/**
* Create a new image object from Media Object.
*
* @param MediaFile $media_file
* @param float $x
* @param float $y
* @param float $w Image width
* @param float $h Image height
* @param string $align L:left, C:center, R:right or empty to use x/y
* @param string $ln T:same line, N:next line
* @param FilesystemOperator $data_filesystem
*
* @return ReportBaseImage
*/
public function createImageFromObject(
MediaFile $media_file,
float $x,
float $y,
float $w,
float $h,
string $align,
string $ln,
FilesystemOperator $data_filesystem
): ReportBaseImage {
return new ReportPdfImage('@' . $media_file->fileContents($data_filesystem), $x, $y, $w, $h, $align, $ln);
}
/**
* Create a line.
*
* @param float $x1
* @param float $y1
* @param float $x2
* @param float $y2
*
* @return ReportBaseLine
*/
public function createLine(float $x1, float $y1, float $x2, float $y2): ReportBaseLine
{
return new ReportPdfLine($x1, $y1, $x2, $y2);
}
}