ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilPhantomJSRenderer.php
Go to the documentation of this file.
1<?php
2
3require_once './Services/PDFGeneration/classes/class.ilPDFGenerationConstants.php';
4require_once './Services/PDFGeneration/interfaces/interface.ilRendererConfig.php';
5require_once './Services/PDFGeneration/interfaces/interface.ilPDFRenderer.php';
6
8{
9 const PAGE = 0;
10 const VIEWPORT = 1;
11
13 protected $lng;
14
16 protected $path_to_rasterize = './Services/PDFGeneration/js/rasterize.js';
17
18 public function __construct($phpunit = false)
19 {
20 if (!$phpunit) {
21 global $DIC;
22 $this->setLanguage($DIC['lng']);
23 }
24 }
25
29 protected function setLanguage($lng)
30 {
31 $this->lng = $lng;
32 }
33
38
42 protected $page_size;
43
47 protected $orientation;
48
52 protected $margin;
53
58
62 protected $viewport;
63
67 protected $header_type;
68
72 protected $footer_type;
73
77 protected $header_text;
78
82 protected $header_height;
83
88
92 protected $footer_text;
93
97 protected $footer_height;
98
103
107 protected $path;
108
113
117 public function getPluginName()
118 {
119 return $this->lng->txt('pdfgen_renderer_dummyrender_plugname');
120 }
121
125 protected $do_not_validate_ssl = ' --ssl-protocol=any --ignore-ssl-errors=true ';
126
137 {
138 $path = new ilTextInputGUI($this->lng->txt('path'), 'path');
139 $path->setValue($this->path);
140 $form->addItem($path);
141
142 $item_group = new ilRadioGroupInputGUI($this->lng->txt('page_settings'), 'page_type');
143
144 $op = new ilRadioOption($this->lng->txt('page'), self::PAGE);
145 $op->addSubItem($this->buildMarginForm());
146 $op->addSubItem($this->buildOrientationForm());
147 $op->addSubItem($this->buildPageSizesForm());
148 $header_select = new ilRadioGroupInputGUI($this->lng->txt('header_type'), 'header_select');
149 $header_select->addOption(new ilRadioOption($this->lng->txt('none'), ilPDFGenerationConstants::HEADER_NONE, ''));
150 $header_text = new ilRadioOption($this->lng->txt('text'), ilPDFGenerationConstants::HEADER_TEXT, '');
151 $header_text->addSubItem($this->buildHeaderTextForm());
152 $header_text->addSubItem($this->buildHeaderHeightForm());
153 $header_text->addSubItem($this->buildHeaderPageNumbersForm());
154 $header_select->addOption($header_text);
155 $header_select->setValue($this->header_type);
156 $op->addSubItem($header_select);
157 $footer_select = new ilRadioGroupInputGUI($this->lng->txt('footer_type'), 'footer_select');
158 $footer_select->addOption(new ilRadioOption($this->lng->txt('none'), ilPDFGenerationConstants::FOOTER_NONE, ''));
159 $footer_text = new ilRadioOption($this->lng->txt('text'), ilPDFGenerationConstants::FOOTER_TEXT, '');
160 $footer_text->addSubItem($this->buildFooterTextForm());
161 $footer_text->addSubItem($this->buildFooterHeightForm());
162 $footer_text->addSubItem($this->buildFooterPageNumbersForm());
163 $footer_select->addOption($footer_text);
164 $footer_select->setValue($this->footer_type);
165 $op->addSubItem($footer_select);
166 $item_group->addOption($op);
167
168 $op = new ilRadioOption($this->lng->txt('viewport'), self::VIEWPORT);
169 $op->addSubItem($this->buildViewPortForm());
170 $item_group->addOption($op);
171 $item_group->setValue($this->page_type);
172 $form->addItem($item_group);
173
174 $form->addItem($this->buildJavascriptDelayForm());
175 }
176
188 {
189 $form->getItemByPostVar('path')->setValue($config['path']);
190 $form->getItemByPostVar('page_size')->setValue($config['page_size']);
191 $form->getItemByPostVar('margin')->setValue($config['margin']);
192 $form->getItemByPostVar('javascript_delay')->setValue($config['javascript_delay']);
193 $form->getItemByPostVar('viewport')->setValue($config['viewport']);
194 $form->getItemByPostVar('orientation')->setValue($config['orientation']);
195 $form->getItemByPostVar('header_select')->setValue($config['header_type']);
196 $form->getItemByPostVar('header_text')->setValue($config['header_text']);
197 $form->getItemByPostVar('header_height')->setValue($config['header_height']);
198 $form->getItemByPostVar('header_show_pages')->setChecked($config['header_show_pages']);
199 $form->getItemByPostVar('footer_select')->setValue($config['footer_type']);
200 $form->getItemByPostVar('footer_text')->setValue($config['footer_text']);
201 $form->getItemByPostVar('footer_height')->setValue($config['footer_height']);
202 $form->getItemByPostVar('footer_show_pages')->setChecked($config['footer_show_pages']);
203 $form->getItemByPostVar('page_type')->setValue($config['page_type']);
204
206 }
207
218 {
219 if (true) {
220 return true;
221 }
222 return false;
223 }
224
235 {
236 $config = array();
237 $config['path'] =$form->getItemByPostVar('path')->getValue();
238 $config['page_size'] = $form->getItemByPostVar('page_size')->getValue();
239 $config['margin'] =$form->getItemByPostVar('margin')->getValue();
240 $config['javascript_delay'] = $form->getItemByPostVar('javascript_delay')->getValue();
241 $config['viewport'] = $form->getItemByPostVar('viewport')->getValue();
242 $config['orientation'] = $form->getItemByPostVar('orientation')->getValue();
243 $config['header_type'] = $form->getItemByPostVar('header_select')->getValue();
244 $config['header_text'] = $form->getItemByPostVar('header_text')->getValue();
245 $config['header_height'] = $form->getItemByPostVar('header_height')->getValue();
246 $config['header_show_pages'] = $form->getItemByPostVar('header_show_pages')->getChecked();
247 $config['footer_type'] = $form->getItemByPostVar('footer_select')->getValue();
248 $config['footer_text'] = $form->getItemByPostVar('footer_text')->getValue();
249 $config['footer_height'] = $form->getItemByPostVar('footer_height')->getValue();
250 $config['footer_show_pages'] = $form->getItemByPostVar('footer_show_pages')->getChecked();
251 $config['page_type'] = $form->getItemByPostVar('page_type')->getValue();
252
253 return $config;
254 }
255
264 public function getDefaultConfig($service, $purpose)
265 {
266 $config = array();
267 if (PATH_TO_PHANTOMJS !== '') {
268 $config['path'] = PATH_TO_PHANTOMJS;
269 } else {
270 $config['path'] = '/usr/local/bin/phantomjs';
271 }
272
273 $config['page_size'] = 'A4';
274 $config['margin'] = '1cm';
275 $config['javascript_delay'] = 200;
276 $config['viewport'] = '';
277 $config['orientation'] = 'Portrait';
278 $config['header_type'] = 0;
279 $config['header_text'] = '';
280 $config['header_height'] = '0cm';
281 $config['header_show_pages'] = 0;
282 $config['footer_type'] = 0;
283 $config['footer_text'] = '';
284 $config['footer_height'] = '0cm';
285 $config['footer_show_pages'] = 0;
286 $config['page_type'] = self::PAGE;
287
288 return $config;
289 }
290
301 public function generatePDF($service, $purpose, $config, $job)
302 {
303 $html_file = $this->getHtmlTempName();
304 file_put_contents($html_file, implode('', $job->getPages()));
305 $this->createPDFFileFromHTMLFile($html_file, $config, $job);
306 }
307
313 public function createPDFFileFromHTMLFile($a_path_to_file, $config, $job)
314 {
316 global $ilLog;
317
318 if (file_exists($a_path_to_file)) {
319 $temp_file = $this->getPdfTempName();
320
321 $args = ' ' . $a_path_to_file . ' ' . $temp_file . ' ' . $this->getCommandLineConfig($config);
322 $return_value = ilUtil::execQuoted($config['path'], $this->do_not_validate_ssl . ' ' . $this->path_to_rasterize . ' ' . $args);
323
324 $ilLog->write('ilPhantomJSRenderer command line config: ' . $args);
325 foreach ($return_value as $key => $value) {
326 $ilLog->write('ilPhantomJSRenderer return value line ' . $key . ' : ' . $value);
327 }
328
329 if (file_exists($temp_file)) {
330 $ilLog->write('ilPhantomJSRenderer file exists: ' . $temp_file . ' file size is :' . filesize($temp_file) . ' bytes, will be renamed to ' . $job->getFilename());
331 ilFileUtils::rename($temp_file, $job->getFilename());
332 } else {
333 $ilLog->write('ilPhantomJSRenderer error: ' . print_r($return_value, true));
334 }
335 }
336 }
337
341 protected function buildHeaderTextForm()
342 {
343 $header_text = new ilTextInputGUI($this->lng->txt('head_text'), 'header_text');
344 $header_text->setValue($this->header_text);
345 return $header_text;
346 }
347
351 protected function buildHeaderHeightForm()
352 {
353 $header_height = new ilTextInputGUI($this->lng->txt('header_height'), 'header_height');
354 $header_height->setValue($this->header_height);
355 return $header_height;
356 }
357
361 protected function buildViewPortForm()
362 {
363 $viewport = new ilTextInputGUI($this->lng->txt('viewport'), 'viewport');
364 $viewport->setValue($this->viewport);
365 $viewport->setInfo($this->lng->txt('viewport_info'));
366 return $viewport;
367 }
368
372 protected function buildHeaderPageNumbersForm()
373 {
374 $header_show_pages = new ilCheckboxInputGUI($this->lng->txt('header_show_pages'), 'header_show_pages');
375 if ($this->header_show_pages == true || $this->header_show_pages == 1) {
376 $header_show_pages->setChecked(true);
377 }
378 return $header_show_pages;
379 }
380
384 protected function buildFooterTextForm()
385 {
386 $footer_text = new ilTextInputGUI($this->lng->txt('footer_text'), 'footer_text');
387 $footer_text->setValue($this->footer_text);
388 return $footer_text;
389 }
390
394 protected function buildFooterHeightForm()
395 {
396 $footer_height = new ilTextInputGUI($this->lng->txt('footer_height'), 'footer_height');
397 $footer_height->setValue($this->footer_height);
398 return $footer_height;
399 }
400
404 protected function buildFooterPageNumbersForm()
405 {
406 $footer_show_pages = new ilCheckboxInputGUI($this->lng->txt('footer_show_pages'), 'footer_show_pages');
407 if ($this->footer_show_pages == true || $this->footer_show_pages == 1) {
408 $footer_show_pages->setChecked(true);
409 }
410 return $footer_show_pages;
411 }
412
416 protected function buildPageSizesForm()
417 {
418 $page_size = new ilSelectInputGUI($this->lng->txt('page_size'), 'page_size');
420 $page_size->setValue($this->page_size);
421 return $page_size;
422 }
423
427 protected function buildOrientationForm()
428 {
429 $orientation = new ilSelectInputGUI($this->lng->txt('orientation'), 'orientation');
431 $orientation->setValue($this->orientation);
432 return $orientation;
433 }
434
438 protected function buildMarginForm()
439 {
440 $margin = new ilTextInputGUI($this->lng->txt('margin'), 'margin');
441 $margin->setValue($this->margin);
442 return $margin;
443 }
444
448 protected function buildJavascriptDelayForm()
449 {
450 $javascript_delay = new ilTextInputGUI($this->lng->txt('javascript_delay'), 'javascript_delay');
451 $javascript_delay->setInfo($this->lng->txt('javascript_delay_info'));
452 $javascript_delay->setValue($this->javascript_delay);
453 return $javascript_delay;
454 }
455
459 public function getPdfTempName()
460 {
461 return $this->getTempFileName('pdf');
462 }
463
467 public function getHtmlTempName()
468 {
469 return $this->getTempFileName('html');
470 }
471
476 protected function getTempFileName($file_type)
477 {
478 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && $file_type == 'html') {
479 return 'file:///' . str_replace(':/', '://', ilUtil::ilTempnam()) . '.' . $file_type;
480 } else {
481 return ilUtil::ilTempnam() . '.' . $file_type;
482 }
483 }
484
490 protected function getCommandLineConfig($config)
491 {
492 $r_config = array();
493
494 if ($config['header_type'] == ilPDFGenerationConstants::HEADER_TEXT) {
495 $h_config = array(
496 'text' => $config['header_text'],
497 'height' => $config['header_height'],
498 'show_pages' => $config['header_show_pages']);
499 } else {
500 $h_config = null;
501 }
502
503 if ($config['footer_type'] == ilPDFGenerationConstants::FOOTER_TEXT) {
504 $f_config = array(
505 'text' => $config['footer_text'],
506 'height' => $config['footer_height'],
507 'show_pages' => $config['footer_show_pages']);
508 } else {
509 $f_config = null;
510 }
511
512 $r_config['page_size'] = $config['page_size'];
513 $r_config['orientation'] = $config['orientation'];
514 $r_config['margin'] = $config['margin'];
515 $r_config['delay'] = $config['javascript_delay'];
516 $r_config['viewport'] = $config['viewport'];
517 $r_config['header'] = $h_config;
518 $r_config['footer'] = $f_config;
519 $r_config['page_type'] = $config['page_type'];
520
521 return json_encode(json_encode($r_config));
522 }
523}
An exception for terminatinating execution or to throw for unit testing.
This class represents a checkbox property in a property form.
static rename($a_source, $a_target)
Rename a file.
static setCheckedIfTrue(\ilPropertyFormGUI $form)
getDefaultConfig($service, $purpose)
from ilRendererConfig
populateConfigElementsInForm(\ilPropertyFormGUI $form, $service, $purpose, $config)
from ilRendererConfig
addConfigElementsToForm(\ilPropertyFormGUI $form, $service, $purpose)
from ilRendererConfig
validateConfigInForm(\ilPropertyFormGUI $form, $service, $purpose)
from ilRendererConfig
getConfigFromForm(\ilPropertyFormGUI $form, $service, $purpose)
from ilRendererConfig
generatePDF($service, $purpose, $config, $job)
from ilPDFRenderer
This class represents a property form user interface.
This class represents a property in a property form.
This class represents an option in a radio group.
This class represents a selection list property in a property form.
This class represents a text property in a property form.
static execQuoted($cmd, $args=null)
exec command and fix spaces on windows
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
$key
Definition: croninfo.php:18
const PATH_TO_PHANTOMJS
$service
Definition: login.php:15
if(isset($_POST['submit'])) $form
global $DIC
Definition: saml.php:7