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