1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use Fisharebest\Webtrees\MediaFile;
23use Fisharebest\Webtrees\Webtrees;
24use League\Flysystem\FilesystemInterface;
25
26use function count;
27
28/**
29 * Class PdfRenderer
30 */
31class PdfRenderer extends AbstractRenderer
32{
33    /**
34     * PDF compression - Zlib extension is required
35     *
36     * @var bool const
37     */
38    private const COMPRESSION = true;
39
40    /**
41     * If true reduce the RAM memory usage by caching temporary data on filesystem (slower).
42     *
43     * @var bool const
44     */
45    private const DISK_CACHE = false;
46
47    /**
48     * true means that the input text is unicode (PDF)
49     *
50     * @var bool const
51     */
52    private const UNICODE = true;
53
54    /**
55     * false means that the full font is embedded, true means only the used chars
56     * in TCPDF v5.9 font subsetting is a very slow process, this leads to larger files
57     *
58     * @var bool const
59     */
60    private const SUBSETTING = false;
61
62    /**
63     * @var TcpdfWrapper
64     */
65    public $tcpdf;
66
67    /** @var ReportBaseElement[] Array of elements in the header */
68    public $headerElements = [];
69
70    /** @var ReportBaseElement[] Array of elements in the footer */
71    public $footerElements = [];
72
73    /** @var ReportBaseElement[] Array of elements in the body */
74    public $bodyElements = [];
75
76    /** @var ReportPdfFootnote[] Array of elements in the footer notes */
77    public $printedfootnotes = [];
78
79    /** @var string Currently used style name */
80    public $currentStyle = '';
81
82    /** @var float The last cell height */
83    public $lastCellHeight = 0;
84
85    /** @var float The largest font size within a TextBox to calculate the height */
86    public $largestFontHeight = 0;
87
88    /** @var int The last pictures page number */
89    public $lastpicpage = 0;
90
91    /** @var PdfRenderer The current report. */
92    public $wt_report;
93
94    /**
95     * PDF Header -PDF
96     *
97     * @return void
98     */
99    public function header(): void
100    {
101        foreach ($this->headerElements as $element) {
102            if ($element instanceof ReportBaseElement) {
103                $element->render($this);
104            } elseif ($element === 'footnotetexts') {
105                $this->footnotes();
106            } elseif ($element === 'addpage') {
107                $this->newPage();
108            }
109        }
110    }
111
112    /**
113     * PDF Body -PDF
114     *
115     * @return void
116     */
117    public function body(): void
118    {
119        $this->tcpdf->AddPage();
120
121        foreach ($this->bodyElements as $element) {
122            if ($element instanceof ReportBaseElement) {
123                $element->render($this);
124            } elseif ($element === 'footnotetexts') {
125                $this->footnotes();
126            } elseif ($element === 'addpage') {
127                $this->newPage();
128            }
129        }
130    }
131
132    /**
133     * Generate footnotes
134     *
135     * @return void
136     */
137    public function footnotes(): void
138    {
139        foreach ($this->printedfootnotes as $element) {
140            if ($this->tcpdf->GetY() + $element->getFootnoteHeight($this) > $this->tcpdf->getPageHeight()) {
141                $this->tcpdf->AddPage();
142            }
143
144            $element->renderFootnote($this);
145
146            if ($this->tcpdf->GetY() > $this->tcpdf->getPageHeight()) {
147                $this->tcpdf->AddPage();
148            }
149        }
150    }
151
152    /**
153     * PDF Footer -PDF
154     *
155     * @return void
156     */
157    public function footer(): void
158    {
159        foreach ($this->footerElements as $element) {
160            if ($element instanceof ReportBaseElement) {
161                $element->render($this);
162            } elseif ($element === 'footnotetexts') {
163                $this->footnotes();
164            } elseif ($element === 'addpage') {
165                $this->newPage();
166            }
167        }
168    }
169
170    /**
171     * Add an element to the Header -PDF
172     *
173     * @param ReportBaseElement|string $element
174     *
175     * @return void
176     */
177    public function addHeader($element): void
178    {
179        $this->headerElements[] = $element;
180    }
181
182    /**
183     * Add an element to the Body -PDF
184     *
185     * @param ReportBaseElement|string $element
186     *
187     * @return void
188     */
189    public function addBody($element): void
190    {
191        $this->bodyElements[] = $element;
192    }
193
194    /**
195     * Add an element to the Footer -PDF
196     *
197     * @param ReportBaseElement|string $element
198     *
199     * @return void
200     */
201    public function addFooter($element): void
202    {
203        $this->footerElements[] = $element;
204    }
205
206    /**
207     * Remove the header.
208     *
209     * @param int $index
210     *
211     * @return void
212     */
213    public function removeHeader(int $index): void
214    {
215        unset($this->headerElements[$index]);
216    }
217
218    /**
219     * Remove the body.
220     *
221     * @param int $index
222     *
223     * @return void
224     */
225    public function removeBody(int $index): void
226    {
227        unset($this->bodyElements[$index]);
228    }
229
230    /**
231     * Remove the footer.
232     *
233     * @param int $index
234     *
235     * @return void
236     */
237    public function removeFooter(int $index): void
238    {
239        unset($this->footerElements[$index]);
240    }
241
242    /**
243     * Clear the Header -PDF
244     *
245     * @return void
246     */
247    public function clearHeader(): void
248    {
249        unset($this->headerElements);
250        $this->headerElements = [];
251    }
252
253    /**
254     * Set the report.
255     *
256     * @param PdfRenderer $report
257     *
258     * @return void
259     */
260    public function setReport(PdfRenderer $report): void
261    {
262        $this->wt_report = $report;
263    }
264
265    /**
266     * Get the currently used style name -PDF
267     *
268     * @return string
269     */
270    public function getCurrentStyle(): string
271    {
272        return $this->currentStyle;
273    }
274
275    /**
276     * Setup a style for usage -PDF
277     *
278     * @param string $s Style name
279     *
280     * @return void
281     */
282    public function setCurrentStyle(string $s): void
283    {
284        $this->currentStyle = $s;
285        $style              = $this->wt_report->getStyle($s);
286        $this->tcpdf->SetFont($style['font'], $style['style'], $style['size']);
287    }
288
289    /**
290     * Get the style -PDF
291     *
292     * @param string $s Style name
293     *
294     * @return array
295     */
296    public function getStyle(string $s): array
297    {
298        if (!isset($this->wt_report->styles[$s])) {
299            $s                           = $this->getCurrentStyle();
300            $this->wt_report->styles[$s] = $s;
301        }
302
303        return $this->wt_report->styles[$s];
304    }
305
306    /**
307     * Add margin when static horizontal position is used -PDF
308     * RTL supported
309     *
310     * @param float $x Static position
311     *
312     * @return float
313     */
314    public function addMarginX(float $x): float
315    {
316        $m = $this->tcpdf->getMargins();
317        if ($this->tcpdf->getRTL()) {
318            $x += $m['right'];
319        } else {
320            $x += $m['left'];
321        }
322        $this->tcpdf->SetX($x);
323
324        return $x;
325    }
326
327    /**
328     * Get the maximum line width to draw from the curren position -PDF
329     * RTL supported
330     *
331     * @return float
332     */
333    public function getMaxLineWidth(): float
334    {
335        $m = $this->tcpdf->getMargins();
336        if ($this->tcpdf->getRTL()) {
337            return $this->tcpdf->getRemainingWidth() + $m['right'];
338        }
339
340        return $this->tcpdf->getRemainingWidth() + $m['left'];
341    }
342
343    /**
344     * Get the height of the footnote.
345     *
346     * @return float
347     */
348    public function getFootnotesHeight(): float
349    {
350        $h = 0;
351        foreach ($this->printedfootnotes as $element) {
352            $h += $element->getHeight($this);
353        }
354
355        return $h;
356    }
357
358    /**
359     * Returns the the current font size height -PDF
360     *
361     * @return float
362     */
363    public function getCurrentStyleHeight(): float
364    {
365        if ($this->currentStyle === '') {
366            return $this->wt_report->default_font_size;
367        }
368        $style = $this->wt_report->getStyle($this->currentStyle);
369
370        return (float) $style['size'];
371    }
372
373    /**
374     * Checks the Footnote and numbers them
375     *
376     * @param ReportPdfFootnote $footnote
377     *
378     * @return ReportPdfFootnote|bool object if already numbered, false otherwise
379     */
380    public function checkFootnote(ReportPdfFootnote $footnote)
381    {
382        $ct  = count($this->printedfootnotes);
383        $val = $footnote->getValue();
384        $i   = 0;
385        while ($i < $ct) {
386            if ($this->printedfootnotes[$i]->getValue() == $val) {
387                // If this footnote already exist then set up the numbers for this object
388                $footnote->setNum($i + 1);
389                $footnote->setAddlink((string) ($i + 1));
390
391                return $this->printedfootnotes[$i];
392            }
393            $i++;
394        }
395        // If this Footnote has not been set up yet
396        $footnote->setNum($ct + 1);
397        $footnote->setAddlink((string) $this->tcpdf->AddLink());
398        $this->printedfootnotes[] = $footnote;
399
400        return false;
401    }
402
403    /**
404     * Used this function instead of AddPage()
405     * This function will make sure that images will not be overwritten
406     *
407     * @return void
408     */
409    public function newPage(): void
410    {
411        if ($this->lastpicpage > $this->tcpdf->getPage()) {
412            $this->tcpdf->setPage($this->lastpicpage);
413        }
414        $this->tcpdf->AddPage();
415    }
416
417    /**
418     * Add a page if needed -PDF
419     *
420     * @param float $height Cell height
421     *
422     * @return bool true in case of page break, false otherwise
423     */
424    public function checkPageBreakPDF(float $height): bool
425    {
426        return $this->tcpdf->checkPageBreak($height);
427    }
428
429    /**
430     * Returns the remaining width between the current position and margins -PDF
431     *
432     * @return float Remaining width
433     */
434    public function getRemainingWidthPDF(): float
435    {
436        return $this->tcpdf->getRemainingWidth();
437    }
438    /**
439     * PDF Setup - ReportPdf
440     *
441     * @return void
442     */
443    public function setup(): void
444    {
445        parent::setup();
446
447        $this->tcpdf = new TcpdfWrapper(
448            $this->orientation,
449            self::UNITS,
450            [$this->page_width, $this->page_height],
451            self::UNICODE,
452            'UTF-8',
453            self::DISK_CACHE
454        );
455
456        $this->tcpdf->SetMargins($this->left_margin, $this->top_margin, $this->right_margin);
457        $this->tcpdf->setHeaderMargin($this->header_margin);
458        $this->tcpdf->setFooterMargin($this->footer_margin);
459        $this->tcpdf->SetAutoPageBreak(true, $this->bottom_margin);
460        $this->tcpdf->setFontSubsetting(self::SUBSETTING);
461        $this->tcpdf->SetCompression(self::COMPRESSION);
462        $this->tcpdf->setRTL($this->rtl);
463        $this->tcpdf->SetCreator(Webtrees::NAME . ' ' . Webtrees::VERSION);
464        $this->tcpdf->SetAuthor($this->rauthor);
465        $this->tcpdf->SetTitle($this->title);
466        $this->tcpdf->SetSubject($this->rsubject);
467        $this->tcpdf->SetKeywords($this->rkeywords);
468        $this->tcpdf->SetHeaderData('', 0, $this->title);
469        $this->tcpdf->setHeaderFont([$this->default_font, '', $this->default_font_size]);
470
471        $this->setReport($this);
472
473        if ($this->show_generated_by) {
474            // The default style name for Generated by.... is 'genby'
475            $element = new ReportPdfCell(0.0, 10.0, '', 'C', '', 'genby', 1, ReportBaseElement::CURRENT_POSITION, ReportBaseElement::CURRENT_POSITION, 0, 0, '', '', true);
476            $element->addText($this->generated_by);
477            $element->setUrl(Webtrees::URL);
478            $this->addFooter($element);
479        }
480    }
481
482    /**
483     * Add an element.
484     *
485     * @param ReportBaseElement|string $element
486     *
487     * @return void
488     */
489    public function addElement($element): void
490    {
491        if ($this->processing === 'B') {
492            $this->addBody($element);
493
494            return;
495        }
496
497        if ($this->processing === 'H') {
498            $this->addHeader($element);
499
500            return;
501        }
502
503        if ($this->processing === 'F') {
504            $this->addFooter($element);
505
506            return;
507        }
508    }
509
510    /**
511     * Run the report.
512     *
513     * @return void
514     */
515    public function run(): void
516    {
517        $this->body();
518        echo $this->tcpdf->Output('doc.pdf', 'S');
519    }
520
521    /**
522     * Create a new Cell object.
523     *
524     * @param int    $width   cell width (expressed in points)
525     * @param int    $height  cell height (expressed in points)
526     * @param mixed  $border  Border style
527     * @param string $align   Text alignement
528     * @param string $bgcolor Background color code
529     * @param string $style   The name of the text style
530     * @param int    $ln      Indicates where the current position should go after the call
531     * @param mixed  $top     Y-position
532     * @param mixed  $left    X-position
533     * @param int    $fill    Indicates if the cell background must be painted (1) or transparent (0). Default value: 1
534     * @param int    $stretch Stretch carachter mode
535     * @param string $bocolor Border color
536     * @param string $tcolor  Text color
537     * @param bool   $reseth
538     *
539     * @return ReportBaseCell
540     */
541    public function createCell(float $width, float $height, string $border, string $align, string $bgcolor, string $style, int $ln, $top, $left, int $fill, int $stretch, string $bocolor, string $tcolor, bool $reseth): ReportBaseCell
542    {
543        return new ReportPdfCell($width, $height, $border, $align, $bgcolor, $style, $ln, $top, $left, $fill, $stretch, $bocolor, $tcolor, $reseth);
544    }
545
546    /**
547     * Create a new TextBox object.
548     *
549     * @param float  $width   Text box width
550     * @param float  $height  Text box height
551     * @param bool   $border
552     * @param string $bgcolor Background color code in HTML
553     * @param bool   $newline
554     * @param float  $left
555     * @param float  $top
556     * @param bool   $pagecheck
557     * @param string $style
558     * @param bool   $fill
559     * @param bool   $padding
560     * @param bool   $reseth
561     *
562     * @return ReportBaseTextbox
563     */
564    public function createTextBox(
565        float $width,
566        float $height,
567        bool $border,
568        string $bgcolor,
569        bool $newline,
570        float $left,
571        float $top,
572        bool $pagecheck,
573        string $style,
574        bool $fill,
575        bool $padding,
576        bool $reseth
577    ): ReportBaseTextbox {
578        return new ReportPdfTextBox($width, $height, $border, $bgcolor, $newline, $left, $top, $pagecheck, $style, $fill, $padding, $reseth);
579    }
580
581    /**
582     * Create a text element.
583     *
584     * @param string $style
585     * @param string $color
586     *
587     * @return ReportBaseText
588     */
589    public function createText(string $style, string $color): ReportBaseText
590    {
591        return new ReportPdfText($style, $color);
592    }
593
594    /**
595     * Create a new Footnote object.
596     *
597     * @param string $style Style name
598     *
599     * @return ReportBaseFootnote
600     */
601    public function createFootnote(string $style): ReportBaseFootnote
602    {
603        return new ReportPdfFootnote($style);
604    }
605
606    /**
607     * Create a new image object.
608     *
609     * @param string $file  Filename
610     * @param float  $x
611     * @param float  $y
612     * @param float  $w     Image width
613     * @param float  $h     Image height
614     * @param string $align L:left, C:center, R:right or empty to use x/y
615     * @param string $ln    T:same line, N:next line
616     *
617     * @return ReportBaseImage
618     */
619    public function createImage(string $file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage
620    {
621        return new ReportPdfImage($file, $x, $y, $w, $h, $align, $ln);
622    }
623
624    /**
625     * Create a new image object from Media Object.
626     *
627     * @param MediaFile           $media_file
628     * @param float               $x
629     * @param float               $y
630     * @param float               $w     Image width
631     * @param float               $h     Image height
632     * @param string              $align L:left, C:center, R:right or empty to use x/y
633     * @param string              $ln    T:same line, N:next line
634     * @param FilesystemInterface $data_filesystem
635     *
636     * @return ReportBaseImage
637     */
638    public function createImageFromObject(
639        MediaFile $media_file,
640        float $x,
641        float $y,
642        float $w,
643        float $h,
644        string $align,
645        string $ln,
646        FilesystemInterface $data_filesystem
647    ): ReportBaseImage {
648        return new ReportPdfImage('@' . $media_file->fileContents($data_filesystem), $x, $y, $w, $h, $align, $ln);
649    }
650
651    /**
652     * Create a line.
653     *
654     * @param float $x1
655     * @param float $y1
656     * @param float $x2
657     * @param float $y2
658     *
659     * @return ReportBaseLine
660     */
661    public function createLine(float $x1, float $y1, float $x2, float $y2): ReportBaseLine
662    {
663        return new ReportPdfLine($x1, $y1, $x2, $y2);
664    }
665}
666