ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPhantomJSRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 require_once './Services/PDFGeneration/classes/class.ilPDFGenerationConstants.php';
4 require_once './Services/PDFGeneration/interfaces/interface.ilRendererConfig.php';
5 require_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 
57  protected $javascript_delay;
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 
87  protected $header_show_pages;
88 
92  protected $footer_text;
93 
97  protected $footer_height;
98 
103 
107  protected $path;
108 
112  protected $page_type = self::PAGE;
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 
136  public function addConfigElementsToForm(\ilPropertyFormGUI $form, $service, $purpose)
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 
217  public function validateConfigInForm(\ilPropertyFormGUI $form, $service, $purpose)
218  {
219  if (true) {
220  return true;
221  }
222  return false;
223  }
224 
234  public function getConfigFromForm(\ilPropertyFormGUI $form, $service, $purpose)
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 
291 
301  public function prepareGenerationRequest($service, $purpose)
302  {
304  ->init(ilMathJax::PURPOSE_PDF)
305  ->setRendering(ilMathJax::RENDER_SVG_AS_XML_EMBED);
306  }
307 
308 
319  public function generatePDF($service, $purpose, $config, $job)
320  {
321  $html_file = $this->getHtmlTempName();
322  file_put_contents($html_file, implode('', $job->getPages()));
323  $this->createPDFFileFromHTMLFile($html_file, $config, $job);
324  }
325 
331  public function createPDFFileFromHTMLFile($a_path_to_file, $config, $job)
332  {
334  global $ilLog;
335 
336  if (file_exists($a_path_to_file)) {
337  $temp_file = $this->getPdfTempName();
338 
339  $args = ' ' . $a_path_to_file . ' ' . $temp_file . ' ' . $this->getCommandLineConfig($config);
340  $return_value = ilUtil::execQuoted($config['path'], $this->do_not_validate_ssl . ' ' . $this->path_to_rasterize . ' ' . $args);
341 
342  $ilLog->write('ilPhantomJSRenderer command line config: ' . $args);
343  foreach ($return_value as $key => $value) {
344  $ilLog->write('ilPhantomJSRenderer return value line ' . $key . ' : ' . $value);
345  }
346 
347  if (file_exists($temp_file)) {
348  $ilLog->write('ilPhantomJSRenderer file exists: ' . $temp_file . ' file size is :' . filesize($temp_file) . ' bytes, will be renamed to ' . $job->getFilename());
349  ilFileUtils::rename($temp_file, $job->getFilename());
350  unlink($a_path_to_file);
351  } else {
352  $ilLog->write('ilPhantomJSRenderer error: ' . print_r($return_value, true));
353  }
354  }
355  }
356 
360  protected function buildHeaderTextForm()
361  {
362  $header_text = new ilTextInputGUI($this->lng->txt('head_text'), 'header_text');
363  $header_text->setValue($this->header_text);
364  return $header_text;
365  }
366 
370  protected function buildHeaderHeightForm()
371  {
372  $header_height = new ilTextInputGUI($this->lng->txt('header_height'), 'header_height');
373  $header_height->setValue($this->header_height);
374  return $header_height;
375  }
376 
380  protected function buildViewPortForm()
381  {
382  $viewport = new ilTextInputGUI($this->lng->txt('viewport'), 'viewport');
383  $viewport->setValue($this->viewport);
384  $viewport->setInfo($this->lng->txt('viewport_info'));
385  return $viewport;
386  }
387 
391  protected function buildHeaderPageNumbersForm()
392  {
393  $header_show_pages = new ilCheckboxInputGUI($this->lng->txt('header_show_pages'), 'header_show_pages');
394  if ($this->header_show_pages == true || $this->header_show_pages == 1) {
395  $header_show_pages->setChecked(true);
396  }
397  return $header_show_pages;
398  }
399 
403  protected function buildFooterTextForm()
404  {
405  $footer_text = new ilTextInputGUI($this->lng->txt('footer_text'), 'footer_text');
406  $footer_text->setValue($this->footer_text);
407  return $footer_text;
408  }
409 
413  protected function buildFooterHeightForm()
414  {
415  $footer_height = new ilTextInputGUI($this->lng->txt('footer_height'), 'footer_height');
416  $footer_height->setValue($this->footer_height);
417  return $footer_height;
418  }
419 
423  protected function buildFooterPageNumbersForm()
424  {
425  $footer_show_pages = new ilCheckboxInputGUI($this->lng->txt('footer_show_pages'), 'footer_show_pages');
426  if ($this->footer_show_pages == true || $this->footer_show_pages == 1) {
427  $footer_show_pages->setChecked(true);
428  }
429  return $footer_show_pages;
430  }
431 
435  protected function buildPageSizesForm()
436  {
437  $page_size = new ilSelectInputGUI($this->lng->txt('page_size'), 'page_size');
439  $page_size->setValue($this->page_size);
440  return $page_size;
441  }
442 
446  protected function buildOrientationForm()
447  {
448  $orientation = new ilSelectInputGUI($this->lng->txt('orientation'), 'orientation');
450  $orientation->setValue($this->orientation);
451  return $orientation;
452  }
453 
457  protected function buildMarginForm()
458  {
459  $margin = new ilTextInputGUI($this->lng->txt('margin'), 'margin');
460  $margin->setValue($this->margin);
461  return $margin;
462  }
463 
467  protected function buildJavascriptDelayForm()
468  {
469  $javascript_delay = new ilTextInputGUI($this->lng->txt('javascript_delay'), 'javascript_delay');
470  $javascript_delay->setInfo($this->lng->txt('javascript_delay_info'));
471  $javascript_delay->setValue($this->javascript_delay);
472  return $javascript_delay;
473  }
474 
478  public function getPdfTempName()
479  {
480  return $this->getTempFileName('pdf');
481  }
482 
486  public function getHtmlTempName()
487  {
488  return $this->getTempFileName('html');
489  }
490 
495  protected function getTempFileName($file_type)
496  {
497  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && $file_type == 'html') {
498  return 'file:///' . str_replace(':/', '://', ilUtil::ilTempnam()) . '.' . $file_type;
499  } else {
500  return ilUtil::ilTempnam() . '.' . $file_type;
501  }
502  }
503 
509  protected function getCommandLineConfig($config)
510  {
511  $r_config = array();
512 
513  if ($config['header_type'] == ilPDFGenerationConstants::HEADER_TEXT) {
514  $h_config = array(
515  'text' => $config['header_text'],
516  'height' => $config['header_height'],
517  'show_pages' => $config['header_show_pages']);
518  } else {
519  $h_config = null;
520  }
521 
522  if ($config['footer_type'] == ilPDFGenerationConstants::FOOTER_TEXT) {
523  $f_config = array(
524  'text' => $config['footer_text'],
525  'height' => $config['footer_height'],
526  'show_pages' => $config['footer_show_pages']);
527  } else {
528  $f_config = null;
529  }
530 
531  $r_config['page_size'] = $config['page_size'];
532  $r_config['orientation'] = $config['orientation'];
533  $r_config['margin'] = $config['margin'];
534  $r_config['delay'] = $config['javascript_delay'];
535  $r_config['viewport'] = $config['viewport'];
536  $r_config['header'] = $h_config;
537  $r_config['footer'] = $f_config;
538  $r_config['page_type'] = $config['page_type'];
539 
540  return json_encode(json_encode($r_config));
541  }
542 }
This class represents an option in a radio group.
prepareGenerationRequest($service, $purpose)
Prepare the content processing at the beginning of a PDF generation request Should be used to initial...
getItemByPostVar($a_post_var)
Get Item by POST variable.
getDefaultConfig($service, $purpose)
from ilRendererConfig
This class represents a property form user interface.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
const PURPOSE_PDF
This class represents a checkbox property in a property form.
addItem($a_item)
Add Item (Property, SectionHeader).
getConfigFromForm(\ilPropertyFormGUI $form, $service, $purpose)
from ilRendererConfig
static rename($a_source, $a_target)
Rename a file.
static setCheckedIfTrue(\ilPropertyFormGUI $form)
This class represents a property in a property form.
addOption($a_option)
Add Option.
validateConfigInForm(\ilPropertyFormGUI $form, $service, $purpose)
from ilRendererConfig
generatePDF($service, $purpose, $config, $job)
from ilPDFRenderer
$service
Definition: result.php:17
populateConfigElementsInForm(\ilPropertyFormGUI $form, $service, $purpose, $config)
from ilRendererConfig
static execQuoted($cmd, $args=null)
exec command and fix spaces on windows
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
addConfigElementsToForm(\ilPropertyFormGUI $form, $service, $purpose)
from ilRendererConfig
static getInstance()
Singleton: get instance.
$DIC
Definition: xapitoken.php:46
const RENDER_SVG_AS_XML_EMBED
const PATH_TO_PHANTOMJS