ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilWkhtmlToPdfRenderer.php
Go to the documentation of this file.
1 <?php
2 
4 {
6  protected static $instance = null;
8  protected $config;
10  protected $lng;
11 
16  public function __construct($phpunit = false)
17  {
18  if (!$phpunit) {
19  global $DIC;
20  $this->setLanguage($DIC['lng']);
21  }
22  }
23 
27  protected function setLanguage($lng)
28  {
29  $this->lng = $lng;
30  }
31 
37  public function addConfigElementsToForm(ilPropertyFormGUI $form, $service, $purpose)
38  {
39  $gui = new ilWkhtmlToPdfConfigFormGUI();
40  $gui->addConfigForm($form);
41  }
42 
50  {
51  $this->config = new ilWkhtmlToPdfConfig($config);
52  $gui = new ilWkhtmlToPdfConfigFormGUI();
53  $gui->populateForm($form, $this->config);
54  }
55 
62  public function validateConfigInForm(ilPropertyFormGUI $form, $service, $purpose)
63  {
64  $gui = new ilWkhtmlToPdfConfigFormGUI();
65  return $gui->validateForm();
66  }
67 
74  public function getConfigFromForm(ilPropertyFormGUI $form, $service, $purpose)
75  {
76  $gui = new ilWkhtmlToPdfConfigFormGUI();
77  return $gui->getConfigFromForm($form);
78  }
79 
85  public function getDefaultConfig($service, $purpose)
86  {
88  return $config;
89  }
90 
97  public function generatePDF($service, $purpose, $config, $job)
98  {
99  $html_file = $this->getHtmlTempName();
100  file_put_contents($html_file, implode('', $job->getPages()));
101  $this->createPDFFileFromHTMLFile($html_file, $config, $job);
102  }
103 
107  public function getHtmlTempName()
108  {
109  return $this->getTempFileName('html');
110  }
111 
116  protected function getTempFileName($file_type)
117  {
118  return ilUtil::ilTempnam() . '.' . $file_type;
119  }
120 
126  public function createPDFFileFromHTMLFile($a_path_to_file, $config, $job)
127  {
128  if (is_array($a_path_to_file)) {
129  $files_list_as_string = ' ';
130  foreach ($a_path_to_file as $file) {
131  if (file_exists($file)) {
132  $files_list_as_string .= ' ' . $files_list_as_string;
133  }
134  }
135  $this->runCommandLine($files_list_as_string, $job->getFilename(), $config);
136  } else {
137  if (file_exists($a_path_to_file)) {
138  $this->runCommandLine($a_path_to_file, $job->getFilename(), $config);
139  }
140  }
141  }
142 
148  protected function runCommandLine($a_path_to_file, $a_target, $config)
149  {
150  global $DIC;
151  $log = $DIC['ilLog'];
153  $temp_file = $this->getPdfTempName();
154  $args = $config->getCommandLineConfig() . ' ' . $a_path_to_file . ' ' . $temp_file . $this->redirectLog();
155  $this->appendDefaultFontStyle($a_path_to_file, $config);
156  $return_value = ilUtil::execQuoted($config->getWKHTMLToPdfDefaultPath(), $args);
157  $log->debug('ilWebkitHtmlToPdfTransformer command line config: ' . $args);
158  foreach ($return_value as $key => $value) {
159  $log->debug('ilWebkitHtmlToPdfTransformer return value line ' . $key . ' : ' . $value);
160  }
161  if (file_exists($temp_file)) {
162  $log->debug('ilWebkitHtmlToPdfTransformer file exists: ' . $temp_file . ' file size is :' . filesize($temp_file) . ' bytes, will be renamed to ' . $a_target);
163  rename($temp_file, $a_target);
164  } else {
165  $log->info('ilWebkitHtmlToPdfTransformer error: ' . print_r($return_value, true));
166  }
167  unlink($a_path_to_file);
168  }
169 
173  public function getPdfTempName()
174  {
175  return $this->getTempFileName('pdf');
176  }
177 
181  protected function redirectLog()
182  {
183  return $redirect_log = ' 2>&1 ';
184  }
185 
190  protected function appendDefaultFontStyle($a_path_to_file, $config)
191  {
192  $backupStyle = $config->getOverwriteDefaultFont(true);
193  $originalFile = file_get_contents($a_path_to_file) . $backupStyle;
194  file_put_contents($a_path_to_file, $originalFile);
195  }
196 
201  public function prepareGenerationRequest($service, $purpose)
202  {
204  ->init(ilMathJax::PURPOSE_PDF)
205  ->setRendering(ilMathJax::RENDER_SVG_AS_XML_EMBED);
206  }
207 }
runCommandLine($a_path_to_file, $a_target, $config)
This class represents a property form user interface.
getDefaultConfig($service, $purpose)
const PURPOSE_PDF
generatePDF($service, $purpose, $config, $job)
getConfigFromForm(ilPropertyFormGUI $form, $service, $purpose)
populateConfigElementsInForm(ilPropertyFormGUI $form, $service, $purpose, $config)
$log
Definition: result.php:15
$service
Definition: result.php:17
addConfigElementsToForm(ilPropertyFormGUI $form, $service, $purpose)
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.
static getInstance()
Singleton: get instance.
__construct($phpunit=false)
ilWkhtmlToPdfRenderer constructor.
$DIC
Definition: xapitoken.php:46
createPDFFileFromHTMLFile($a_path_to_file, $config, $job)
prepareGenerationRequest($service, $purpose)
appendDefaultFontStyle($a_path_to_file, $config)
validateConfigInForm(ilPropertyFormGUI $form, $service, $purpose)
const RENDER_SVG_AS_XML_EMBED