|
1
|
|
|
<?php |
|
2
|
|
|
defined('ROOT_PATH') or exit('Access denied'); |
|
3
|
|
|
/** |
|
4
|
|
|
* TNH Framework |
|
5
|
|
|
* |
|
6
|
|
|
* A simple PHP framework using HMVC architecture |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2017 TNH Framework |
|
11
|
|
|
* |
|
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
14
|
|
|
* in the Software without restriction, including without limitation the rights |
|
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
17
|
|
|
* furnished to do so, subject to the following conditions: |
|
18
|
|
|
* |
|
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
20
|
|
|
* copies or substantial portions of the Software. |
|
21
|
|
|
* |
|
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
28
|
|
|
* SOFTWARE. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* PDF library to generate PDF document using the library DOMPDF |
|
33
|
|
|
*/ |
|
34
|
|
|
class PDF extends BaseClass { |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The dompdf instance |
|
38
|
|
|
* @var Dompdf |
|
39
|
|
|
*/ |
|
40
|
|
|
private $dompdf = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The PDF generated filename |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $filename = 'output.pdf'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The HTML content |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $html = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The PDF document paper type like 'A4', 'A5', 'letter', etc. |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $paper = 'A4'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The PDF document orientation like 'portrait', 'landscape' |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
private $orientation = 'portrait'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create PDF library instance |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct() { |
|
70
|
|
|
parent::__construct(); |
|
71
|
|
|
require_once VENDOR_PATH . 'dompdf/dompdf_config.inc.php'; |
|
72
|
|
|
$this->dompdf = new Dompdf(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the instance of Dompdf |
|
77
|
|
|
* |
|
78
|
|
|
* @return object the dompdf instance |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getDompdf() { |
|
81
|
|
|
return $this->dompdf; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Return the instance of Canvas |
|
86
|
|
|
* |
|
87
|
|
|
* @return object the canvas instance |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getCanvas() { |
|
90
|
|
|
return $this->dompdf->get_canvas(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* This method is the shortcut to Dompdf::render |
|
95
|
|
|
* @return object the current instance |
|
96
|
|
|
*/ |
|
97
|
|
|
public function render() { |
|
98
|
|
|
$this->dompdf->load_html($this->html); |
|
99
|
|
|
$this->dompdf->set_paper($this->paper, $this->orientation); |
|
100
|
|
|
$this->dompdf->render(); |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the filename of generated PDF document |
|
106
|
|
|
* @param string $filename the filename |
|
107
|
|
|
* |
|
108
|
|
|
* @return object the current instance |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setFilename($filename) { |
|
111
|
|
|
if(stripos($filename, '.pdf') === false) { |
|
112
|
|
|
$filename .= '.pdf'; |
|
113
|
|
|
} |
|
114
|
|
|
$this->filename = $filename; |
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set the HTML content to use to generate the PDF |
|
120
|
|
|
* @param string $html the content of HTML |
|
121
|
|
|
* |
|
122
|
|
|
* @return object the current instance |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setHtml($html) { |
|
125
|
|
|
$this->html = $html; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set the page paper of the generated PDF |
|
131
|
|
|
* @param string $paper the page paper like "A4", "letter", etc. |
|
132
|
|
|
* |
|
133
|
|
|
* @return object the current instance |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setPaper($paper) { |
|
136
|
|
|
$this->paper = $paper; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the page orientation of the generated PDF to "portrait" |
|
142
|
|
|
* |
|
143
|
|
|
* @return object the current instance |
|
144
|
|
|
*/ |
|
145
|
|
|
public function portrait() { |
|
146
|
|
|
$this->orientation = 'portrait'; |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Set the page orientation of the generated PDF to "portrait" |
|
152
|
|
|
* |
|
153
|
|
|
* @return object the current instance |
|
154
|
|
|
*/ |
|
155
|
|
|
public function landscape() { |
|
156
|
|
|
$this->orientation = 'landscape'; |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Download the generated PDF document |
|
162
|
|
|
* @codeCoverageIgnore |
|
163
|
|
|
* |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
public function download() { |
|
167
|
|
|
$this->logger->info('Download of PDF document: filename [' . $this->filename . '], ' |
|
168
|
|
|
. 'paper [' . $this->paper . '], orientation [' . $this->orientation . ']'); |
|
169
|
|
|
$this->prepare(); |
|
170
|
|
|
$this->dompdf->stream($this->filename); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Return the content of the generated PDF document as string |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function content() { |
|
178
|
|
|
$this->logger->info('Return of PDF document as string: paper ' |
|
179
|
|
|
. '[' . $this->paper . '], orientation [' . $this->orientation . ']'); |
|
180
|
|
|
$this->prepare(); |
|
181
|
|
|
return $this->dompdf->output(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Save the content of the generated PDF document on the server filesystem |
|
186
|
|
|
* @return void |
|
187
|
|
|
*/ |
|
188
|
|
|
public function save() { |
|
189
|
|
|
$this->logger->info('Saving PDF document : filename path [' . $this->filename . '], paper ' |
|
190
|
|
|
. '[' . $this->paper . '], orientation [' . $this->orientation . ']'); |
|
191
|
|
|
file_put_contents($this->filename, $this->content()); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Prepare the PDF to generate |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
|
|
protected function prepare() { |
|
199
|
|
|
//If the canvas instance is null so means the method "render" |
|
200
|
|
|
// not yet called |
|
201
|
|
|
if ($this->dompdf->get_canvas() === null) { |
|
202
|
|
|
$this->render(); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
|