ILIAS  release_8 Revision v8.23
class.ilWkhtmlToPdfRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
27  protected ilLanguage $lng;
28  protected ilLogger $log;
29 
30  public function __construct(bool $phpunit = false)
31  {
32  if (!$phpunit) {
33  global $DIC;
34  $this->setLanguage($DIC->language());
35  $this->log = $DIC->logger()->root();
36  }
37  }
38 
39  protected function setLanguage(ilLanguage $lng): void
40  {
41  $this->lng = $lng;
42  }
43 
44  public function addConfigElementsToForm(ilPropertyFormGUI $form, string $service, string $purpose): void
45  {
46  $gui = new ilWkhtmlToPdfConfigFormGUI();
47  $gui->addConfigForm($form);
48  }
49 
50  public function populateConfigElementsInForm(ilPropertyFormGUI $form, string $service, string $purpose, $config): void
51  {
52  $this->config = new ilWkhtmlToPdfConfig($config);
53  $gui = new ilWkhtmlToPdfConfigFormGUI();
54  $gui->populateForm($form, $this->config);
55  }
56 
57  public function validateConfigInForm(ilPropertyFormGUI $form, string $service, string $purpose): bool
58  {
59  $gui = new ilWkhtmlToPdfConfigFormGUI();
60  return $gui->validateForm($form);
61  }
62 
63  public function getConfigFromForm(ilPropertyFormGUI $form, string $service, string $purpose): array
64  {
65  $gui = new ilWkhtmlToPdfConfigFormGUI();
66  return $gui->getConfigFromForm($form);
67  }
68 
69  public function getDefaultConfig(string $service, string $purpose): ilWkhtmlToPdfConfig
70  {
71  $config = new ilWkhtmlToPdfConfig();
72  return $config;
73  }
74 
75  public function generatePDF(string $service, string $purpose, $config, ilPDFGenerationJob $job): void
76  {
77  $html_file = $this->getHtmlTempName();
78  $pages = $job->getPages();
79 
80  file_put_contents($html_file, implode('', $pages));
81  $this->createPDFFileFromHTMLFile($html_file, $config, $job);
82  }
83 
84  public function getHtmlTempName(): string
85  {
86  return $this->getTempFileName('html');
87  }
88 
89  protected function getTempFileName(string $file_type): string
90  {
91  return ilFileUtils::ilTempnam() . '.' . $file_type;
92  }
93 
94  public function createPDFFileFromHTMLFile(string $a_path_to_file, $config, ilPDFGenerationJob $job): void
95  {
96  $this->runCommandLine($a_path_to_file, $job->getFilename(), $config);
97  }
98 
99  protected function runCommandLine(string $a_path_to_file, string $a_target, $config): void
100  {
101  $wkConfig = new ilWkhtmlToPdfConfig($config);
102  $temp_file = $this->getPdfTempName();
103  $args = $wkConfig->getCommandLineConfig() . ' ' . $a_path_to_file . ' ' . $temp_file . $this->redirectLog();
104  $this->appendDefaultFontStyle($a_path_to_file, $wkConfig);
105  $this->fixIconSizeForPatchedQt($a_path_to_file);
106  $return_value = ilShellUtil::execQuoted($wkConfig->getWKHTMLToPdfDefaultPath(), $args);
107  $this->log->debug('ilWebkitHtmlToPdfTransformer command line config: ' . $args);
108  $this->checkReturnValueFromCommandLine($return_value, $temp_file, $a_target);
109  unlink($a_path_to_file);
110  }
111 
112  public function getPdfTempName(): string
113  {
114  return $this->getTempFileName('pdf');
115  }
116 
117  protected function redirectLog(): string
118  {
119  return ' 2>&1 ';
120  }
121 
122  protected function appendDefaultFontStyle(string $a_path_to_file, ilWkhtmlToPdfConfig $config): void
123  {
124  $backupStyle = $config->getOverwriteDefaultFont(true);
125  $originalFile = file_get_contents($a_path_to_file) . $backupStyle;
126  file_put_contents($a_path_to_file, $originalFile);
127  }
128 
134  protected function fixIconSizeForPatchedQt(string $a_path_to_file): void
135  {
136  $style = '<style>div.questionPrintview img.small {height: 20px; width: 20px;}</style>';
137  $originalFile = file_get_contents($a_path_to_file) . $style;
138  file_put_contents($a_path_to_file, $originalFile);
139  }
140 
141 
142  public function prepareGenerationRequest(string $service, string $purpose): void
143  {
145  ->init(ilMathJax::PURPOSE_PDF)
146  ->setRendering(ilMathJax::RENDER_SVG_AS_XML_EMBED);
147  }
148 
149  protected function checkReturnValueFromCommandLine(array $return_value, string $temp_file, string $a_target): void
150  {
151  foreach ($return_value as $key => $value) {
152  $this->log->debug('ilWebkitHtmlToPdfTransformer return value line ' . $key . ' : ' . $value);
153  }
154  if (file_exists($temp_file)) {
155  $this->log->debug('ilWebkitHtmlToPdfTransformer file exists: ' . $temp_file . ' file size is :' . filesize($temp_file) . ' bytes, will be renamed to ' . $a_target);
156  rename($temp_file, $a_target);
157  } else {
158  $this->log->info('ilWebkitHtmlToPdfTransformer error: ' . print_r($return_value, true));
159  }
160  }
161 }
appendDefaultFontStyle(string $a_path_to_file, ilWkhtmlToPdfConfig $config)
fixIconSizeForPatchedQt(string $a_path_to_file)
Fix for an issue of WkhtmlToPdf with patched QT Without a fixed width the icons would be distorted an...
checkReturnValueFromCommandLine(array $return_value, string $temp_file, string $a_target)
const PURPOSE_PDF
validateConfigInForm(ilPropertyFormGUI $form, string $service, string $purpose)
getOverwriteDefaultFont(bool $renderStyle=false)
global $DIC
Definition: feed.php:28
getConfigFromForm(ilPropertyFormGUI $form, string $service, string $purpose)
string $key
Consumer key/client ID value.
Definition: System.php:193
generatePDF(string $service, string $purpose, $config, ilPDFGenerationJob $job)
static execQuoted(string $cmd, ?string $args=null)
runCommandLine(string $a_path_to_file, string $a_target, $config)
getDefaultConfig(string $service, string $purpose)
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static getInstance()
Singleton: get instance for use in ILIAS requests with a config loaded from the settings.
createPDFFileFromHTMLFile(string $a_path_to_file, $config, ilPDFGenerationJob $job)
populateConfigElementsInForm(ilPropertyFormGUI $form, string $service, string $purpose, $config)
prepareGenerationRequest(string $service, string $purpose)
Prepare the content processing at the beginning of a PDF generation request Should be used to initial...
$service
Definition: ltiservices.php:43
addConfigElementsToForm(ilPropertyFormGUI $form, string $service, string $purpose)
const RENDER_SVG_AS_XML_EMBED