This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
/
PdfTemplate.php
97 lines (84 loc) · 3.31 KB
/
PdfTemplate.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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 JustCarmen (http://justcarmen.nl)
* 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 <http://www.gnu.org/licenses/>.
*/
namespace JustCarmen\WebtreesAddOns\FancyTreeviewPdf\Template;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\User;
use JustCarmen\WebtreesAddOns\FancyTreeviewPdf\FancyTreeviewPdfClass;
use Mpdf\Mpdf;
class PdfTemplate extends FancyTreeviewPdfClass {
public function pageBody() {
$tmp_dir = WT_DATA_DIR . 'ftv_pdf_tmp/';
require_once(WT_MODULES_DIR . $this->getName() . '/vendor/autoload.php');
$stylesheet = file_get_contents($this->directory . '/css/style.css');
$stylesheet_rtl = file_get_contents($this->directory . '/css/style-rtl.css');
$html = Filter::post('pdfContent');
$header = '<header>=== ' . $this->tree()->getTitleHtml() . ' ===</header>';
$footer = '<footer>' .
'<table><tr>' .
'<td class="left">' . WT_BASE_URL . '</td>' .
'<td class="center">{DATE d-m-Y}</td>' .
'<td class="right">{PAGENO}</td>' .
'</tr></table>' .
'</footer>';
$mpdf = new Mpdf([
'tempDir' => $tmp_dir,
'simpleTables' => true,
'shrink_tables_to_fit' => 1,
'autoScriptToLang' => true,
'autoLangToFont' => true,
'setAutoTopMargin' => 'stretch',
'setAutoBottomMargin' => 'stretch',
'autoMarginPadding' => 5
]);
if (I18N::direction() === 'rtl') {
$mpdf->SetDirectionality('rtl');
$mpdf->WriteHTML($stylesheet_rtl, 1);
} else {
$mpdf->WriteHTML($stylesheet, 1);
}
$admin = User::find($this->tree()->getPreference('WEBMASTER_USER_ID'))->getRealName();
$mpdf->setCreator($this->getTitle() . ' - a webtrees module by justcarmen.nl');
$mpdf->SetTitle(Filter::get('title'));
$mpdf->setAuthor($admin);
$mpdf->SetHTMLHeader($header);
$mpdf->setHTMLFooter($footer);
$html_chunks = explode("\n", $html);
$chunks = count($html_chunks);
$i = 1;
foreach ($html_chunks as $html_chunk) {
// write html body parts only (option 2);
if ($i === 1) {
// first chunk (initialize all buffers - init=true)
$mpdf->WriteHTML($html_chunk, 2, true, false);
} elseif ($i === $chunks) {
// last chunck (close all buffers - close=true)
$mpdf->WriteHTML($html_chunk, 2, false, true);
} else {
// all other parts (keep the buffer open)
$mpdf->WriteHTML($html_chunk, 2, false, false);
}
$i++;
}
$index = '
<pagebreak type="next-odd" />
<h2>' . I18N::translate('Index') . '</h2>
<columns column-count="2" column-gap="5" />
<indexinsert usedivletters="on" links="on" collation="' . WT_LOCALE . '.utf8" collationgroup="' . I18N::collation() . '" />';
$mpdf->writeHTML($index);
$mpdf->Output($tmp_dir . Filter::get('title') . '.pdf', 'F');
}
}