1 | <?php |
||
9 | class PdfGenerator |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $baseUrl; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $binaryPath = 'phantomjs'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $storagePath; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $htmlPath; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $pdfPath; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $timeout = 10; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $commandLineOptions = []; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $convertScript = 'generate-pdf.js'; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $orientation = 'portrait'; |
||
55 | |||
56 | /** |
||
57 | * Save a PDF file to the disk |
||
58 | * @param string|object $view |
||
59 | * @param string $path |
||
60 | */ |
||
61 | 1 | public function saveFromView($view, $path) |
|
69 | |||
70 | /** |
||
71 | * Generate paths for the temporary files |
||
72 | * @throws Exception |
||
73 | */ |
||
74 | 1 | protected function generateFilePaths() |
|
84 | |||
85 | /** |
||
86 | * Validate that the storage path is set and is writable |
||
87 | * @throws \Exception |
||
88 | */ |
||
89 | 3 | protected function validateStoragePath() |
|
99 | |||
100 | /** |
||
101 | * Run the script with PhantomJS |
||
102 | * @param string $view |
||
103 | */ |
||
104 | 2 | protected function generatePdf($view) |
|
130 | |||
131 | /** |
||
132 | * Convert the provided view to a string. The __toString method is called manually to be able to catch exceptions |
||
133 | * in the view which is not possible otherwise. https://bugs.php.net/bug.php?id=53648 |
||
134 | * @param mixed $view |
||
135 | * @return string |
||
136 | */ |
||
137 | 1 | protected function viewToString($view) |
|
141 | |||
142 | /** |
||
143 | * Save a string to a html file |
||
144 | * @param string $html |
||
145 | */ |
||
146 | 1 | protected function saveHtml($html) |
|
152 | |||
153 | /** |
||
154 | * Insert a base tag after the head tag to allow relative references to assets |
||
155 | * @param string $view |
||
156 | * @return string |
||
157 | */ |
||
158 | 2 | protected function insertBaseTag($view) |
|
166 | |||
167 | /** |
||
168 | * Prefix the input path for windows versions of PhantomJS |
||
169 | * @param string $path |
||
170 | * @return string |
||
171 | */ |
||
172 | 1 | public function prefixHtmlPath($path) |
|
180 | |||
181 | /** |
||
182 | * Set the base url for the base tag |
||
183 | * @param string $url |
||
184 | * @return self |
||
185 | */ |
||
186 | 1 | public function setBaseUrl($url) |
|
192 | |||
193 | 1 | public function getBaseUrl() |
|
197 | |||
198 | /** |
||
199 | * Set the binary path |
||
200 | * @param string $path |
||
201 | * @return self |
||
202 | */ |
||
203 | 1 | public function setBinaryPath($path) |
|
209 | |||
210 | 1 | public function getBinaryPath() |
|
214 | |||
215 | /** |
||
216 | * Set the storage path for temporary files |
||
217 | * @param string $path |
||
218 | * @return self |
||
219 | */ |
||
220 | 1 | public function setStoragePath($path) |
|
226 | |||
227 | 1 | public function getStoragePath() |
|
231 | |||
232 | /** |
||
233 | * @param int $seconds |
||
234 | * @return self |
||
235 | */ |
||
236 | 1 | public function setTimeout($seconds) |
|
242 | |||
243 | /** |
||
244 | * @return int |
||
245 | */ |
||
246 | 1 | public function getTimeout() |
|
250 | |||
251 | /** |
||
252 | * Ignore PhantomJS SSL errors |
||
253 | * @return self |
||
254 | */ |
||
255 | 1 | public function ignoreSSLErrors() |
|
261 | |||
262 | /** |
||
263 | * Add a command line option for PhantomJS |
||
264 | * @param string $option |
||
265 | * @return self |
||
266 | */ |
||
267 | 1 | public function addCommandLineOption($option) |
|
273 | |||
274 | /** |
||
275 | * @return array |
||
276 | */ |
||
277 | 1 | public function getCommandLineOptions() |
|
281 | |||
282 | /** |
||
283 | * Use a custom script to be run via PhantomJS |
||
284 | * @param string $path |
||
285 | * @return self |
||
286 | */ |
||
287 | 1 | public function setConvertScript($path) |
|
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | */ |
||
297 | 1 | public function getConvertScript() |
|
301 | |||
302 | /** |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getOrientation() |
||
309 | |||
310 | /** |
||
311 | * @param string $orientation |
||
312 | * @return self |
||
313 | */ |
||
314 | public function setOrientation($orientation) |
||
320 | } |
||
321 |