ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilTestPDFGenerator.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
15  const PDF_OUTPUT_DOWNLOAD = 'D';
16  const PDF_OUTPUT_INLINE = 'I';
17  const PDF_OUTPUT_FILE = 'F';
18 
19  private static function buildHtmlDocument($contentHtml, $styleHtml)
20  {
21  return "
22  <html>
23  <head>
24  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
25  $styleHtml
26  </head>
27  <body>$contentHtml</body>
28  </html>
29  ";
30  }
31 
36  private static function makeHtmlDocument($contentHtml, $styleHtml)
37  {
38  if(!is_string($contentHtml) || !strlen(trim($contentHtml)))
39  {
40  return $contentHtml;
41  }
42 
43  $html = self::buildHtmlDocument($contentHtml, $styleHtml);
44 
45  $dom = new DOMDocument("1.0", "utf-8");
46  if(!@$dom->loadHTML($html))
47  {
48  return $html;
49  }
50 
51  $invalid_elements = array();
52 
53  $script_elements = $dom->getElementsByTagName('script');
54  foreach($script_elements as $elm)
55  {
56  $invalid_elements[] = $elm;
57  }
58 
59  foreach($invalid_elements as $elm)
60  {
61  $elm->parentNode->removeChild($elm);
62  }
63 
64  // remove noprint elems as tcpdf will make empty pdf when hidden by css rules
65  $domX = new DomXPath($dom);
66  foreach($domX->query("//*[contains(@class, 'noprint')]") as $node)
67  {
68  $node->parentNode->removeChild($node);
69  }
70 
71  // remove noprint elems as tcpdf will make empty pdf when hidden by css rules
72  $domX = new DomXPath($dom);
73  foreach($domX->query("//*[contains(@class, 'ilNoDisplay')]") as $node)
74  {
75  $node->parentNode->removeChild($node);
76  }
77 
78  $dom->encoding = 'UTF-8';
79 
80  $img_src_map = array();
81  foreach($dom->getElementsByTagName('img') as $elm)
82  {
84  $uid = 'img_src_' . uniqid();
85  $src = $elm->getAttribute('src');
86 
87  $elm->setAttribute('src', $uid);
88 
89  $img_src_map[$uid] = $src;
90  }
91 
92  $cleaned_html = $dom->saveHTML();
93 
94  foreach($img_src_map as $uid => $src)
95  {
96  $cleaned_html = str_replace($uid, $src, $cleaned_html);
97  }
98 
99  if(!$cleaned_html)
100  {
101  return $html;
102  }
103 
104  return '<?xml encoding="UTF-8">'.$cleaned_html;
105  }
106 
107  public static function generatePDF($pdf_output, $output_mode, $filename=null)
108  {
109  $pdf_output = self::preprocessHTML($pdf_output);
110 
111  if (substr($filename, strlen($filename) - 4, 4) != '.pdf')
112  {
113  $filename .= '.pdf';
114  }
115 
116  require_once './Services/PDFGeneration/classes/class.ilPDFGeneration.php';
117 
118  $job = new ilPDFGenerationJob();
119  $job->setAutoPageBreak(true)
120  ->setCreator('ILIAS Test')
121  ->setFilename($filename)
122  ->setMarginLeft('20')
123  ->setMarginRight('20')
124  ->setMarginTop('20')
125  ->setMarginBottom('20')
126  ->setOutputMode($output_mode)
127  ->addPage($pdf_output);
128 
130  }
131 
132  public static function preprocessHTML($html)
133  {
134  $pdf_css_path = self::getTemplatePath('test_pdf.css');
135  $html = self::makeHtmlDocument($html, '<style>'.file_get_contents($pdf_css_path).'</style>');
136 
137  return $html;
138  }
139 
140  protected static function getTemplatePath($a_filename)
141  {
142  $module_path = "Modules/Test/";
143 
144  // use ilStyleDefinition instead of account to get the current skin
145  include_once "Services/Style/classes/class.ilStyleDefinition.php";
146  if (ilStyleDefinition::getCurrentSkin() != "default")
147  {
148  $fname = "./Customizing/global/skin/".
149  ilStyleDefinition::getCurrentSkin()."/".$module_path.basename($a_filename);
150  }
151 
152  if($fname == "" || !file_exists($fname))
153  {
154  $fname = "./".$module_path."templates/default/".basename($a_filename);
155  }
156  return $fname;
157  }
158 
159 }
Class ilTestPDFGenerator.
static buildHtmlDocument($contentHtml, $styleHtml)
$filename
Definition: buildRTE.php:89
static getTemplatePath($a_filename)
Class ilPDFGenerationJob.
static generatePDF($pdf_output, $output_mode, $filename=null)
static doJob(ilPDFGenerationJob $job)
$html
Definition: example_001.php:87