ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTestPDFGenerator.php
Go to the documentation of this file.
1 <?php
28 {
29  public const PDF_OUTPUT_DOWNLOAD = 'D';
30  public const PDF_OUTPUT_INLINE = 'I';
31  public const PDF_OUTPUT_FILE = 'F';
32 
33  public const service = "Test";
34 
35  private static function buildHtmlDocument($contentHtml, $styleHtml): string
36  {
37  return "
38  <html>
39  <head>
40  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
41  $styleHtml
42  </head>
43  <body>$contentHtml</body>
44  </html>
45  ";
46  }
47 
53  private static function makeHtmlDocument($contentHtml, $styleHtml): string
54  {
55  if (!is_string($contentHtml) || !strlen(trim($contentHtml))) {
56  return $contentHtml;
57  }
58 
59  $html = self::buildHtmlDocument($contentHtml, $styleHtml);
60 
61  $dom = new DOMDocument("1.0", "utf-8");
62  if (!@$dom->loadHTML($html)) {
63  return $html;
64  }
65 
66  $invalid_elements = array();
67 
68  $script_elements = $dom->getElementsByTagName('script');
69  foreach ($script_elements as $elm) {
70  $invalid_elements[] = $elm;
71  }
72 
73  foreach ($invalid_elements as $elm) {
74  $elm->parentNode->removeChild($elm);
75  }
76 
77  // remove noprint elems as tcpdf will make empty pdf when hidden by css rules
78  $domX = new DomXPath($dom);
79  foreach ($domX->query("//*[contains(@class, 'noprint')]") as $node) {
80  $node->parentNode->removeChild($node);
81  }
82 
83  $dom->encoding = 'UTF-8';
84 
85  $img_src_map = array();
86  foreach ($dom->getElementsByTagName('img') as $elm) {
88  $uid = 'img_src_' . uniqid();
89  $src = $elm->getAttribute('src');
90 
91  $elm->setAttribute('src', $uid);
92 
93  $img_src_map[$uid] = $src;
94  }
95 
96  $cleaned_html = $dom->saveHTML();
97 
98  foreach ($img_src_map as $uid => $src) {
99  $cleaned_html = str_replace($uid, $src, $cleaned_html);
100  }
101 
102  if (!$cleaned_html) {
103  return $html;
104  }
105 
106  return $cleaned_html;
107  }
108 
109  public static function generatePDF($pdf_output, $output_mode, $filename = null, $purpose = null)
110  {
111  $pdf_output = self::preprocessHTML($pdf_output);
112 
113  if (substr($filename, strlen($filename) - 4, 4) != '.pdf') {
114  $filename .= '.pdf';
115  }
116  $pdf_factory = new ilHtmlToPdfTransformerFactory();
117 
118  if (!$pdf_factory->deliverPDFFromHTMLString(
119  $pdf_output,
120  $filename,
121  $output_mode,
122  self::service,
123  $purpose
124  )) {
125  throw new \Exception('could not write PDF');
126  }
127  return true;
128  }
129 
130  public static function preprocessHTML($html): string
131  {
132  $html = self::makeHtmlDocument($html, '<style>' . self::getCssContent() . '</style>');
133 
134  return $html;
135  }
136 
137  protected static function getTemplatePath($a_filename, $module_path = 'Modules/Test/'): string
138  {
139  // use ilStyleDefinition instead of account to get the current skin
140  include_once "Services/Style/System/classes/class.ilStyleDefinition.php";
141  $fname = '';
142  if (ilStyleDefinition::getCurrentSkin() != "default") {
143  $fname = "./Customizing/global/skin/" .
144  ilStyleDefinition::getCurrentSkin() . "/" . $module_path . basename($a_filename);
145  }
146 
147  if ($fname == "" || !file_exists($fname)) {
148  $fname = "./" . $module_path . "templates/default/" . basename($a_filename);
149  }
150  return $fname;
151  }
152 
153  protected static function getCssContent(): string
154  {
155  $cssContent = file_get_contents(self::getTemplatePath('delos.css', ''));
156  $cssContent .= file_get_contents(self::getTemplatePath('test_pdf.css'));
157 
158  return $cssContent;
159  }
160 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static buildHtmlDocument($contentHtml, $styleHtml)
static getCurrentSkin()
get the current skin use always this function instead of getting the account&#39;s skin the current skin ...
$filename
Definition: buildRTE.php:78
static generatePDF($pdf_output, $output_mode, $filename=null, $purpose=null)
static getTemplatePath($a_filename, $module_path='Modules/Test/')