ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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{
16 const PDF_OUTPUT_INLINE = 'I';
17 const PDF_OUTPUT_FILE = 'F';
18
23 private static function removeScriptElements($html)
24 {
25 if(!is_string($html) || !strlen(trim($html)))
26 {
27 return $html;
28 }
29
30 $dom = new DOMDocument("1.0", "utf-8");
31 if(!@$dom->loadHTML('<?xml encoding="UTF-8">' . $html))
32 {
33 return $html;
34 }
35
36 $invalid_elements = array();
37
38 $script_elements = $dom->getElementsByTagName('script');
39 foreach($script_elements as $elm)
40 {
41 $invalid_elements[] = $elm;
42 }
43
44 foreach($invalid_elements as $elm)
45 {
46 $elm->parentNode->removeChild($elm);
47 }
48
49 $dom->encoding = 'UTF-8';
50 $cleaned_html = $dom->saveHTML();
51 if(!$cleaned_html)
52 {
53 return $html;
54 }
55
56 return $cleaned_html;
57 }
58
59 public static function generatePDF($pdf_output, $output_mode, $filename=null)
60 {
61 $pdf_output = self::preprocessHTML($pdf_output);
62
63 if (substr($filename, strlen($filename) - 4, 4) != '.pdf')
64 {
65 $filename .= '.pdf';
66 }
67
68 require_once './Services/PDFGeneration/classes/class.ilPDFGeneration.php';
69
70 $job = new ilPDFGenerationJob();
71 $job->setAutoPageBreak(true)
72 ->setCreator('ILIAS Test')
73 ->setFilename($filename)
74 ->setMarginLeft('20')
75 ->setMarginRight('20')
76 ->setMarginTop('20')
77 ->setMarginBottom('20')
78 ->setOutputMode($output_mode)
79 ->addPage($pdf_output);
80
82 }
83
84 public static function preprocessHTML($html)
85 {
86 $html = self::removeScriptElements($html);
87 $pdf_css_path = self::getTemplatePath('test_pdf.css');
88 return '<style>' . file_get_contents($pdf_css_path) . '</style>' . $html;
89 }
90
91 protected static function getTemplatePath($a_filename)
92 {
93 $module_path = "Modules/Test/";
94
95 // use ilStyleDefinition instead of account to get the current skin
96 include_once "Services/Style/classes/class.ilStyleDefinition.php";
97 if (ilStyleDefinition::getCurrentSkin() != "default")
98 {
99 $fname = "./Customizing/global/skin/".
100 ilStyleDefinition::getCurrentSkin()."/".$module_path.basename($a_filename);
101 }
102
103 if($fname == "" || !file_exists($fname))
104 {
105 $fname = "./".$module_path."templates/default/".basename($a_filename);
106 }
107 return $fname;
108 }
109
110}
$filename
Definition: buildRTE.php:89
Class ilPDFGenerationJob.
static doJob(ilPDFGenerationJob $job)
Class ilTestPDFGenerator.
static getTemplatePath($a_filename)
static generatePDF($pdf_output, $output_mode, $filename=null)