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