ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  $dom->encoding = 'UTF-8';
72 
73  $img_src_map = array();
74  foreach($dom->getElementsByTagName('img') as $elm)
75  {
77  $uid = 'img_src_' . uniqid();
78  $src = $elm->getAttribute('src');
79 
80  $elm->setAttribute('src', $uid);
81 
82  $img_src_map[$uid] = $src;
83  }
84 
85  $cleaned_html = $dom->saveHTML();
86 
87  foreach($img_src_map as $uid => $src)
88  {
89  $cleaned_html = str_replace($uid, $src, $cleaned_html);
90  }
91 
92  if(!$cleaned_html)
93  {
94  return $html;
95  }
96 
97  return $cleaned_html;
98  }
99 
100  public static function generatePDF($pdf_output, $output_mode, $filename=null)
101  {
102  $pdf_output = self::preprocessHTML($pdf_output);
103 
104  if (substr($filename, strlen($filename) - 4, 4) != '.pdf')
105  {
106  $filename .= '.pdf';
107  }
108 
109  require_once './Services/PDFGeneration/classes/class.ilPDFGeneration.php';
110 
111  $job = new ilPDFGenerationJob();
112  $job->setAutoPageBreak(true)
113  ->setCreator('ILIAS Test')
114  ->setFilename($filename)
115  ->setMarginLeft('20')
116  ->setMarginRight('20')
117  ->setMarginTop('20')
118  ->setMarginBottom('20')
119  ->setOutputMode($output_mode)
120  ->addPage($pdf_output);
121 
123  }
124 
125  public static function preprocessHTML($html)
126  {
127  $html = self::makeHtmlDocument($html, '<style>'.self::getCssContent().'</style>');
128 
129  return $html;
130  }
131 
132  protected static function getTemplatePath($a_filename, $module_path = 'Modules/Test/')
133  {
134  // use ilStyleDefinition instead of account to get the current skin
135  include_once "Services/Style/System/classes/class.ilStyleDefinition.php";
136  if (ilStyleDefinition::getCurrentSkin() != "default")
137  {
138  $fname = "./Customizing/global/skin/".
139  ilStyleDefinition::getCurrentSkin()."/".$module_path.basename($a_filename);
140  }
141 
142  if($fname == "" || !file_exists($fname))
143  {
144  $fname = "./".$module_path."templates/default/".basename($a_filename);
145  }
146  return $fname;
147  }
148 
149  protected static function getCssContent()
150  {
151  $cssContent = file_get_contents( self::getTemplatePath('delos.css', '') );
152  $cssContent .= file_get_contents( self::getTemplatePath('test_pdf.css') );
153 
154  return $cssContent;
155  }
156 }
Class ilTestPDFGenerator.
static buildHtmlDocument($contentHtml, $styleHtml)
Create styles array
The data for the language used.
Class ilPDFGenerationJob.
static generatePDF($pdf_output, $output_mode, $filename=null)
static doJob(ilPDFGenerationJob $job)
$html
Definition: example_001.php:87
static getTemplatePath($a_filename, $module_path='Modules/Test/')