-
Notifications
You must be signed in to change notification settings - Fork 18
/
pdfout.php
139 lines (121 loc) · 3.87 KB
/
pdfout.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
<?php
use Dompdf\Dompdf;
class PdfOut extends Dompdf
{
protected $name = 'pdf_file';
protected $html = '';
protected $orientation = 'portrait';
protected $font = 'Dejavu Sans';
protected $attachment = false;
protected $remoteFiles = true;
protected $saveToPath = '';
protected $dpi = 100;
protected $saveAndSend = true;
private function injectPageCount(Dompdf $dompdf): void
{
/** @var CPDF $canvas */
$canvas = $dompdf->getCanvas();
$pdf = $canvas->get_cpdf();
foreach ($pdf->objects as &$o) {
if ($o['t'] === 'contents') {
$o['c'] = str_replace('DOMPDF_PAGE_COUNT_PLACEHOLDER', $canvas->get_page_count(), $o['c']);
}
}
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function setHtml(string $html, bool $outputfilter = false): self
{
if ($outputfilter) {
$html = rex_extension::registerPoint(new rex_extension_point('OUTPUT_FILTER', $html));
}
$this->html = $html;
return $this;
}
public function setOrientation(string $orientation): self
{
$this->orientation = $orientation;
return $this;
}
public function setFont(string $font): self
{
$this->font = $font;
return $this;
}
public function setAttachment(bool $attachment): self
{
$this->attachment = $attachment;
return $this;
}
public function setRemoteFiles(bool $remoteFiles): self
{
$this->remoteFiles = $remoteFiles;
return $this;
}
public function setSaveToPath(string $saveToPath): self
{
$this->saveToPath = $saveToPath;
return $this;
}
public function setDpi(int $dpi): self
{
$this->dpi = $dpi;
return $this;
}
public function setSaveAndSend(bool $saveAndSend): self
{
$this->saveAndSend = $saveAndSend;
return $this;
}
public function run(): void
{
$this->loadHtml($this->html);
// Optionen festlegen
$options = $this->getOptions();
$options->setChroot(rex_path::frontend());
$options->setDefaultFont($this->font);
$options->setDpi($this->dpi);
$options->setFontCache(rex_path::addonCache('pdfout', 'fonts'));
$options->setIsRemoteEnabled($this->remoteFiles);
$this->setOptions($options);
$this->setPaper('A4', $this->orientation);
// Rendern des PDF
$this->render();
// Pagecounter Placeholder esetzen, wenn vorhanden
$this->injectPageCount($this);
// Speichern des PDF
if ($this->saveToPath !== '') {
$savedata = $this->output();
if (!is_null($savedata)) {
rex_file::put($this->saveToPath . rex_string::normalize($this->name) . '.pdf', $savedata);
}
}
// Ausliefern des PDF
if ($this->saveToPath === '' || $this->saveAndSend === true) {
rex_response::cleanOutputBuffers(); // OutputBuffer leeren
header('Content-Type: application/pdf');
$this->stream(rex_string::normalize($this->name), array('Attachment' => $this->attachment));
die();
}
}
public static function mediaUrl(string $type, string $file): string
{
$paddon = rex_addon::get('pdfout');
$url = rex_media_manager::getUrl($type, $file);
if ($addon->getProperty('aspdf', false) || rex_request('pdfout', 'int', 0) === 1) {
return rex::getServer() . $url;
}
return $url;
}
public static function viewer(string $file = ''): string
{
if ($file !== '') {
return rex_url::assets('addons/pdfout/vendor/web/viewer.html?file=' . urlencode($file));
} else {
return '#pdf_missing';
}
}
}