|
1
|
|
|
<?php |
|
2
|
|
|
/* See license terms in /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
use Chamilo\CoreBundle\Component\Utils\ChamiloApi; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class PDF |
|
8
|
|
|
* @package chamilo.library |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
class PDF |
|
12
|
|
|
{ |
|
13
|
|
|
public $pdf; |
|
14
|
|
|
public $custom_header = array(); |
|
15
|
|
|
public $custom_footer = array(); |
|
16
|
|
|
public $params = array(); |
|
17
|
|
|
public $template; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Creates the mPDF object |
|
21
|
|
|
* @param string $pageFormat format A4 A4-L see http://mpdf1.com/manual/index.php?tid=184&searchstring=format |
|
22
|
|
|
* @param string $orientation orientation "P" = Portrait "L" = Landscape |
|
23
|
|
|
* @param array $params |
|
24
|
|
|
* @param Template $template |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
$pageFormat = 'A4', |
|
28
|
|
|
$orientation = 'P', |
|
29
|
|
|
$params = array(), |
|
30
|
|
|
$template = null |
|
31
|
|
|
) { |
|
32
|
|
|
$this->template = $template; |
|
33
|
|
|
/* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF |
|
34
|
|
|
* mPDF ([ string $mode [, mixed $format [, float $default_font_size [, string $default_font [, float $margin_left , float $margin_right , float $margin_top , float $margin_bottom , float $margin_header , float $margin_footer [, string $orientation ]]]]]]) |
|
35
|
|
|
*/ |
|
36
|
|
|
if (!in_array($orientation, array('P', 'L'))) { |
|
37
|
|
|
$orientation = 'P'; |
|
38
|
|
|
} |
|
39
|
|
|
//left, right, top, bottom, margin_header, margin footer |
|
40
|
|
|
|
|
41
|
|
|
$params['left'] = isset($params['left']) ? $params['left'] : 15; |
|
42
|
|
|
$params['right'] = isset($params['right']) ? $params['right'] : 15; |
|
43
|
|
|
$params['top'] = isset($params['top']) ? $params['top'] : 20; |
|
44
|
|
|
$params['bottom'] = isset($params['bottom']) ? $params['bottom'] : 15; |
|
45
|
|
|
|
|
46
|
|
|
$this->params['filename'] = isset($params['filename']) ? $params['filename'] : api_get_local_time(); |
|
47
|
|
|
$this->params['pdf_title'] = isset($params['pdf_title']) ? $params['pdf_title'] : get_lang('Untitled'); |
|
48
|
|
|
$this->params['course_info'] = isset($params['course_info']) ? $params['course_info'] : api_get_course_info(); |
|
49
|
|
|
$this->params['session_info'] = isset($params['session_info']) ? $params['session_info'] : api_get_session_info(api_get_session_id()); |
|
50
|
|
|
$this->params['course_code'] = isset($params['course_code']) ? $params['course_code'] : api_get_course_id(); |
|
51
|
|
|
$this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : []; |
|
52
|
|
|
$this->params['show_real_course_teachers'] = isset($params['show_real_course_teachers']) ? $params['show_real_course_teachers'] : false; |
|
53
|
|
|
$this->params['student_info'] = isset($params['student_info']) ? $params['student_info'] : false; |
|
54
|
|
|
$this->params['show_grade_generated_date'] = isset($params['show_grade_generated_date']) ? $params['show_grade_generated_date'] : false; |
|
55
|
|
|
$this->params['show_teacher_as_myself'] = isset($params['show_teacher_as_myself']) ? $params['show_teacher_as_myself'] : true; |
|
56
|
|
|
$this->params['pdf_date'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date(api_get_local_time(), DATE_TIME_FORMAT_LONG); |
|
57
|
|
|
|
|
58
|
|
|
$this->pdf = new mPDF( |
|
59
|
|
|
'UTF-8', |
|
60
|
|
|
$pageFormat, |
|
61
|
|
|
'', |
|
62
|
|
|
'', |
|
63
|
|
|
$params['left'], |
|
64
|
|
|
$params['right'], |
|
65
|
|
|
$params['top'], |
|
66
|
|
|
$params['bottom'], |
|
67
|
|
|
8, |
|
68
|
|
|
8, |
|
69
|
|
|
$orientation |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
// Default value is 96 set in the mpdf library file config.php |
|
73
|
|
|
$value = api_get_configuration_value('pdf_img_dpi'); |
|
74
|
|
|
if (!empty($value)) { |
|
75
|
|
|
$this->pdf->img_dpi = (int) $value; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Export the given HTML to PDF, using a global template |
|
81
|
|
|
* |
|
82
|
|
|
* @uses export/table_pdf.tpl |
|
83
|
|
|
|
|
84
|
|
|
* @param $content |
|
85
|
|
|
* @param bool|false $saveToFile |
|
86
|
|
|
* @param bool|false $returnHtml |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function html_to_pdf_with_template($content, $saveToFile = false, $returnHtml = false) |
|
91
|
|
|
{ |
|
92
|
|
|
if (empty($this->template)) { |
|
93
|
|
|
$tpl = new Template('', false, false, false); |
|
94
|
|
|
} else { |
|
95
|
|
|
$tpl = $this->template; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Assignments |
|
99
|
|
|
$tpl->assign('pdf_content', $content); |
|
100
|
|
|
|
|
101
|
|
|
// Showing only the current teacher/admin instead the all teacher list name see BT#4080 |
|
102
|
|
|
if (isset($this->params['show_real_course_teachers']) && |
|
103
|
|
|
$this->params['show_real_course_teachers'] |
|
104
|
|
|
) { |
|
105
|
|
|
if (isset($this->params['session_info']) && |
|
106
|
|
|
!empty($this->params['session_info']) |
|
107
|
|
|
) { |
|
108
|
|
|
$teacher_list = SessionManager::getCoachesByCourseSessionToString( |
|
109
|
|
|
$this->params['session_info']['id'], |
|
110
|
|
|
$this->params['course_info']['real_id'] |
|
111
|
|
|
|
|
112
|
|
|
); |
|
113
|
|
|
} else { |
|
114
|
|
|
$teacher_list = CourseManager::get_teacher_list_from_course_code_to_string( |
|
115
|
|
|
$this->params['course_code'] |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
} else { |
|
119
|
|
|
$user_info = api_get_user_info(); |
|
120
|
|
|
|
|
121
|
|
|
if ($this->params['show_teacher_as_myself']) { |
|
122
|
|
|
$teacher_list = $user_info['complete_name']; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$tpl->assign('pdf_course', $this->params['course_code']); |
|
127
|
|
|
$tpl->assign('pdf_course_info', $this->params['course_info']); |
|
128
|
|
|
$tpl->assign('pdf_session_info', $this->params['session_info']); |
|
129
|
|
|
$tpl->assign('pdf_date', $this->params['pdf_date']); |
|
130
|
|
|
$tpl->assign('pdf_teachers', $teacher_list); |
|
131
|
|
|
$tpl->assign('pdf_title', $this->params['pdf_title']); |
|
132
|
|
|
$tpl->assign('pdf_student_info', $this->params['student_info']); |
|
133
|
|
|
$tpl->assign('show_grade_generated_date', $this->params['show_grade_generated_date']); |
|
134
|
|
|
$tpl->assign('add_signatures', $this->params['add_signatures']); |
|
135
|
|
|
|
|
136
|
|
|
// Getting template |
|
137
|
|
|
$tableTemplate = $tpl->get_template('export/table_pdf.tpl'); |
|
138
|
|
|
$html = $tpl->fetch($tableTemplate); |
|
139
|
|
|
$html = api_utf8_encode($html); |
|
140
|
|
|
|
|
141
|
|
|
$css_file = api_get_path(SYS_CSS_PATH).'/print.css'; |
|
142
|
|
|
$css = file_exists($css_file) ? @file_get_contents($css_file) : ''; |
|
143
|
|
|
|
|
144
|
|
|
$html = self::content_to_pdf( |
|
145
|
|
|
$html, |
|
146
|
|
|
$css, |
|
147
|
|
|
$this->params['filename'], |
|
148
|
|
|
$this->params['course_code'], |
|
149
|
|
|
'D', |
|
150
|
|
|
$saveToFile, |
|
151
|
|
|
null, |
|
152
|
|
|
$returnHtml |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
if ($returnHtml) { |
|
156
|
|
|
return $html; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Converts HTML files to PDF |
|
162
|
|
|
* @param mixed $html_file_array could be an html file path or an array |
|
163
|
|
|
* with paths example: |
|
164
|
|
|
* /var/www/myfile.html or array('/myfile.html','myotherfile.html') or |
|
165
|
|
|
* even an indexed array with both 'title' and 'path' indexes |
|
166
|
|
|
* for each element like |
|
167
|
|
|
* array( |
|
168
|
|
|
* 0 => array('title'=>'Hello','path'=>'file.html'), |
|
169
|
|
|
* 1 => array('title'=>'Bye','path'=>'file2.html') |
|
170
|
|
|
* ); |
|
171
|
|
|
* @param string $pdf_name pdf name |
|
172
|
|
|
* @param string $course_code (if you are using html that are located in the document tool you must provide this) |
|
173
|
|
|
* @param bool $print_title add title |
|
174
|
|
|
* @param bool $complete_style show header and footer if true |
|
175
|
|
|
* @param bool $addStyle |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
|
|
public function html_to_pdf( |
|
180
|
|
|
$html_file_array, |
|
181
|
|
|
$pdf_name = '', |
|
182
|
|
|
$course_code = null, |
|
183
|
|
|
$print_title = false, |
|
184
|
|
|
$complete_style = true, |
|
185
|
|
|
$addStyle = true |
|
186
|
|
|
) { |
|
187
|
|
|
if (empty($html_file_array)) { |
|
188
|
|
|
return false; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if (is_array($html_file_array)) { |
|
192
|
|
|
if (count($html_file_array) == 0) { |
|
193
|
|
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
} else { |
|
196
|
|
|
if (!file_exists($html_file_array)) { |
|
197
|
|
|
return false; |
|
198
|
|
|
} |
|
199
|
|
|
// Converting the string into an array |
|
200
|
|
|
$html_file_array = array($html_file_array); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (!empty($course_code)) { |
|
204
|
|
|
$course_data = api_get_course_info($course_code); |
|
205
|
|
|
} else { |
|
206
|
|
|
$course_data = api_get_course_info(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Clean styles and javascript document |
|
210
|
|
|
$clean_search = array( |
|
211
|
|
|
'@<script[^>]*?>.*?</script>@si', |
|
212
|
|
|
'@<style[^>]*?>.*?</style>@si' |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
// Formatting the pdf |
|
216
|
|
|
self::format_pdf($course_data, $complete_style); |
|
217
|
|
|
|
|
218
|
|
|
$counter = 1; |
|
219
|
|
|
foreach ($html_file_array as $file) { |
|
220
|
|
|
//Add a page break per file |
|
221
|
|
|
$page_break = '<pagebreak>'; |
|
222
|
|
|
if ($counter == count($html_file_array)) { |
|
223
|
|
|
$page_break = ''; |
|
224
|
|
|
} |
|
225
|
|
|
$counter++; |
|
226
|
|
|
|
|
227
|
|
|
//if the array provided contained subarrays with 'title' entry, |
|
228
|
|
|
// then print the title in the PDF |
|
229
|
|
View Code Duplication |
if (is_array($file) && isset($file['title'])) { |
|
230
|
|
|
$html_title = $file['title']; |
|
231
|
|
|
$file = $file['path']; |
|
232
|
|
|
} else { |
|
233
|
|
|
//we suppose we've only been sent a file path |
|
234
|
|
|
$html_title = basename($file); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
if (empty($file) && !empty($html_title)) { |
|
238
|
|
|
//this is a chapter, print title & skip the rest |
|
239
|
|
|
if ($print_title) { |
|
240
|
|
|
$this->pdf->WriteHTML( |
|
241
|
|
|
'<html><body><h3>'.$html_title.'</h3></body></html>'.$page_break |
|
242
|
|
|
); |
|
243
|
|
|
} |
|
244
|
|
|
continue; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if (!file_exists($file)) { |
|
248
|
|
|
//the file doesn't exist, skip |
|
249
|
|
|
continue; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if ($addStyle) { |
|
253
|
|
|
$css_file = api_get_path(SYS_CSS_PATH).'/print.css'; |
|
254
|
|
|
$css = file_exists($css_file) ? @file_get_contents($css_file) : ''; |
|
255
|
|
|
$this->pdf->WriteHTML($css, 1); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
//it's not a chapter but the file exists, print its title |
|
259
|
|
|
if ($print_title) { |
|
260
|
|
|
$this->pdf->WriteHTML( |
|
261
|
|
|
'<html><body><h3>'.$html_title.'</h3></body></html>' |
|
262
|
|
|
); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
$file_info = pathinfo($file); |
|
266
|
|
|
$extension = $file_info['extension']; |
|
267
|
|
|
|
|
268
|
|
|
if (in_array($extension, array('html', 'htm'))) { |
|
269
|
|
|
$dirName = $file_info['dirname']; |
|
270
|
|
|
$filename = $file_info['basename']; |
|
271
|
|
|
$filename = str_replace('_', ' ', $filename); |
|
272
|
|
|
|
|
273
|
|
|
if ($extension === 'html') { |
|
274
|
|
|
$filename = basename($filename, '.html'); |
|
275
|
|
|
} elseif ($extension === 'htm') { |
|
276
|
|
|
$filename = basename($filename, '.htm'); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$document_html = @file_get_contents($file); |
|
280
|
|
|
$document_html = preg_replace($clean_search, '', $document_html); |
|
281
|
|
|
|
|
282
|
|
|
//absolute path for frames.css //TODO: necessary? |
|
283
|
|
|
$absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css'; |
|
284
|
|
|
$document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html); |
|
285
|
|
|
|
|
286
|
|
|
if (!empty($course_data['path'])) { |
|
287
|
|
|
$document_html = str_replace('../', '', $document_html); |
|
288
|
|
|
$document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/'; |
|
289
|
|
|
|
|
290
|
|
|
$doc = new DOMDocument(); |
|
291
|
|
|
$result = @$doc->loadHTML($document_html); |
|
292
|
|
|
|
|
293
|
|
|
//Fixing only images @todo do the same thing with other elements |
|
294
|
|
|
$elements = $doc->getElementsByTagName('img'); |
|
295
|
|
|
if (!empty($elements)) { |
|
296
|
|
|
foreach ($elements as $item) { |
|
297
|
|
|
$old_src = $item->getAttribute('src'); |
|
298
|
|
|
if (strpos($old_src, 'http') === false) { |
|
299
|
|
|
if (strpos($old_src, '/main/default_course_document') === false) { |
|
300
|
|
|
$old_src_fixed = ''; |
|
301
|
|
|
|
|
302
|
|
|
if (strpos($old_src, '/main/img') === false) { |
|
303
|
|
|
if (api_get_path(REL_PATH) != '/') { |
|
304
|
|
|
$old_src_fixed = str_replace( |
|
305
|
|
|
api_get_path(REL_PATH).'courses/'.$course_data['path'].'/document/', |
|
306
|
|
|
'', |
|
307
|
|
|
$old_src |
|
308
|
|
|
); |
|
309
|
|
|
|
|
310
|
|
|
// Try with the dirname if exists |
|
311
|
|
View Code Duplication |
if ($old_src_fixed == $old_src) { |
|
312
|
|
|
if (file_exists($dirName.'/'.$old_src)) { |
|
313
|
|
|
$document_path = ''; |
|
314
|
|
|
$old_src_fixed = $dirName.'/'.$old_src; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
} else { |
|
318
|
|
|
if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') !== false) { |
|
319
|
|
|
$old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src); |
|
320
|
|
View Code Duplication |
} else { |
|
321
|
|
|
// Try with the dirname if exists |
|
322
|
|
|
if (file_exists($dirName.'/'.$old_src)) { |
|
323
|
|
|
$document_path = ''; |
|
324
|
|
|
$old_src_fixed = $dirName.'/'.$old_src; |
|
325
|
|
|
} else { |
|
326
|
|
|
$document_path = ''; |
|
327
|
|
|
$old_src_fixed = $old_src; |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
$new_path = $document_path.$old_src_fixed; |
|
333
|
|
|
} else { |
|
334
|
|
|
$new_path = $old_src; |
|
335
|
|
|
} |
|
336
|
|
|
$document_html = str_replace($old_src, $new_path, $document_html); |
|
337
|
|
|
} |
|
338
|
|
|
} else { |
|
|
|
|
|
|
339
|
|
|
//Check if this is a complete URL |
|
340
|
|
|
/*if (strpos($old_src, 'courses/'.$course_data['path'].'/document/') === false) { |
|
341
|
|
|
|
|
342
|
|
|
} else { |
|
343
|
|
|
$old_src_fixed = str_replace(api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/', '', $old_src); |
|
344
|
|
|
$new_path = $document_path.$old_src_fixed; |
|
345
|
|
|
$document_html= str_replace($old_src, $new_path, $document_html); |
|
346
|
|
|
}*/ |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data. |
|
353
|
|
|
// TODO: Maybe it is better idea the title to be passed through |
|
354
|
|
|
$title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); |
|
355
|
|
|
// $_GET[] too, as it is done with file name. |
|
356
|
|
|
// At the moment the title is retrieved from the html document itself. |
|
357
|
|
|
//echo $document_html;exit; |
|
358
|
|
|
if (empty($title)) { |
|
359
|
|
|
$title = $filename; // Here file name is expected to contain ASCII symbols only. |
|
360
|
|
|
} |
|
361
|
|
|
if (!empty($document_html)) { |
|
362
|
|
|
$this->pdf->WriteHTML($document_html.$page_break); |
|
363
|
|
|
} |
|
364
|
|
|
} elseif (in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) { |
|
365
|
|
|
//Images |
|
366
|
|
|
$image = Display::img($file); |
|
367
|
|
|
$this->pdf->WriteHTML('<html><body>'.$image.'</body></html>'.$page_break); |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
View Code Duplication |
if (empty($pdf_name)) { |
|
372
|
|
|
$output_file = 'pdf_'.date('Y-m-d-his').'.pdf'; |
|
373
|
|
|
} else { |
|
374
|
|
|
$pdf_name = api_replace_dangerous_char($pdf_name); |
|
375
|
|
|
$output_file = $pdf_name.'.pdf'; |
|
376
|
|
|
} |
|
377
|
|
|
// F to save the pdf in a file |
|
378
|
|
|
$this->pdf->Output($output_file, 'D'); |
|
379
|
|
|
exit; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
/** |
|
383
|
|
|
* Converts an html string to PDF |
|
384
|
|
|
* @param string $document_html valid html |
|
385
|
|
|
* @param string $css CSS content of a CSS file |
|
386
|
|
|
* @param string $pdf_name pdf name |
|
387
|
|
|
* @param string $course_code course code |
|
388
|
|
|
* (if you are using html that are located in the document tool you must provide this) |
|
389
|
|
|
* @param string $outputMode the MPDF output mode can be: |
|
390
|
|
|
* 'I' (print on standard output), |
|
391
|
|
|
* 'D' (download file) (this is the default value), |
|
392
|
|
|
* 'F' (save to local file) or |
|
393
|
|
|
* 'S' (return as a string) |
|
394
|
|
|
* @return string Web path |
|
395
|
|
|
*/ |
|
396
|
|
|
public function content_to_pdf( |
|
397
|
|
|
$document_html, |
|
398
|
|
|
$css = '', |
|
399
|
|
|
$pdf_name = '', |
|
400
|
|
|
$course_code = null, |
|
401
|
|
|
$outputMode = 'D', |
|
402
|
|
|
$saveInFile = false, |
|
403
|
|
|
$fileToSave = null, |
|
404
|
|
|
$returnHtml = false |
|
405
|
|
|
) { |
|
406
|
|
|
global $_configuration; |
|
407
|
|
|
|
|
408
|
|
|
if (empty($document_html)) { |
|
409
|
|
|
return false; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
//clean styles and javascript document |
|
413
|
|
|
$clean_search = array( |
|
414
|
|
|
'@<script[^>]*?>.*?</script>@si', |
|
415
|
|
|
'@<style[^>]*?>.*?</style>@siU' |
|
416
|
|
|
); |
|
417
|
|
|
|
|
418
|
|
|
// Formatting the pdf |
|
419
|
|
|
$course_data = api_get_course_info($course_code); |
|
420
|
|
|
|
|
421
|
|
|
self::format_pdf($course_data); |
|
422
|
|
|
|
|
423
|
|
|
$document_html = preg_replace($clean_search, '', $document_html); |
|
424
|
|
|
|
|
425
|
|
|
//absolute path for frames.css //TODO: necessary? |
|
426
|
|
|
$absolute_css_path = api_get_path(WEB_CSS_PATH).api_get_setting('stylesheets').'/frames.css'; |
|
427
|
|
|
$document_html = str_replace('href="./css/frames.css"', 'href="'.$absolute_css_path.'"', $document_html); |
|
428
|
|
|
|
|
429
|
|
|
$document_html = str_replace('../../', '', $document_html); |
|
430
|
|
|
$document_html = str_replace('../', '', $document_html); |
|
431
|
|
|
$document_html = str_replace( |
|
432
|
|
|
(empty($_configuration['url_append']) ? '' : $_configuration['url_append'].'/').'courses/'.$course_code.'/document/', |
|
433
|
|
|
'', |
|
434
|
|
|
$document_html |
|
435
|
|
|
); |
|
436
|
|
|
|
|
437
|
|
|
if (!empty($course_data['path'])) { |
|
438
|
|
|
$document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/'; |
|
439
|
|
|
|
|
440
|
|
|
$doc = new DOMDocument(); |
|
441
|
|
|
$result = @$doc->loadHTML($document_html); |
|
442
|
|
|
|
|
443
|
|
|
//Fixing only images @todo do the same thing with other elements |
|
444
|
|
|
$elements = $doc->getElementsByTagName('img'); |
|
445
|
|
|
if (!empty($elements)) { |
|
446
|
|
|
foreach ($elements as $item) { |
|
447
|
|
|
$old_src = $item->getAttribute('src'); |
|
448
|
|
|
//$old_src= str_replace('../','',$old_src); |
|
449
|
|
|
if (strpos($old_src, 'http') === false) { |
|
450
|
|
|
if (strpos($old_src, '/main/default_course_document') === false) { |
|
451
|
|
|
if (strpos($old_src, '/main/inc/lib/') === false) { |
|
452
|
|
|
$old_src_fixed = str_replace(api_get_path(REL_COURSE_PATH).$course_data['path'].'/document/', '', $old_src); |
|
453
|
|
|
$old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed); |
|
454
|
|
|
$new_path = $document_path.$old_src_fixed; |
|
455
|
|
|
$document_html = str_replace($old_src, $new_path, $document_html); |
|
456
|
|
|
|
|
457
|
|
|
} |
|
458
|
|
|
} |
|
459
|
|
|
} |
|
460
|
|
|
} |
|
461
|
|
|
} |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
//replace relative path by absolute path for resources |
|
465
|
|
|
//$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply |
|
466
|
|
|
//$document_html= str_replace('src="/', 'temp_template_path', $document_html);// before save src templates not apply |
|
467
|
|
|
//$document_html= str_replace('src="/chamilo/main/default_course_document/', 'temp_template_path', $document_html);// before save src templates not apply |
|
468
|
|
|
|
|
469
|
|
|
//$src_http_www= 'src="'.api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/'; |
|
470
|
|
|
//$document_html= str_replace('src="',$src_http_www, $document_html); |
|
471
|
|
|
//$document_html= str_replace('temp_template_path', 'src="/main/default_course_document/', $document_html);// restore src templates |
|
472
|
|
|
|
|
473
|
|
|
api_set_encoding_html($document_html, 'UTF-8'); // The library mPDF expects UTF-8 encoded input data. |
|
474
|
|
|
$title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); // TODO: Maybe it is better idea the title to be passed through |
|
475
|
|
|
// $_GET[] too, as it is done with file name. |
|
476
|
|
|
// At the moment the title is retrieved from the html document itself. |
|
477
|
|
|
|
|
478
|
|
|
if ($returnHtml) { |
|
479
|
|
|
return "<style>$css</style>".$document_html; |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
if (!empty($css)) { |
|
483
|
|
|
$this->pdf->WriteHTML($css, 1); |
|
484
|
|
|
} |
|
485
|
|
|
$this->pdf->WriteHTML($document_html); |
|
486
|
|
|
|
|
487
|
|
View Code Duplication |
if (empty($pdf_name)) { |
|
488
|
|
|
$output_file = 'pdf_'.date('Y-m-d-his').'.pdf'; |
|
489
|
|
|
} else { |
|
490
|
|
|
$pdf_name = api_replace_dangerous_char($pdf_name); |
|
491
|
|
|
$output_file = $pdf_name.'.pdf'; |
|
492
|
|
|
} |
|
493
|
|
|
//$this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file |
|
494
|
|
|
|
|
495
|
|
|
if ($outputMode == 'F') { |
|
496
|
|
|
$output_file = api_get_path(SYS_ARCHIVE_PATH).$output_file; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
if ($saveInFile) { |
|
500
|
|
|
$fileToSave = !empty($fileToSave) ? $fileToSave : api_get_path(SYS_ARCHIVE_PATH).uniqid(); |
|
501
|
|
|
|
|
502
|
|
|
$this->pdf->Output( |
|
503
|
|
|
$fileToSave, |
|
504
|
|
|
$outputMode |
|
505
|
|
|
); // F to save the pdf in a file |
|
506
|
|
|
|
|
507
|
|
|
} else { |
|
508
|
|
|
$this->pdf->Output( |
|
509
|
|
|
$output_file, |
|
510
|
|
|
$outputMode |
|
511
|
|
|
); // F to save the pdf in a file |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
if ($outputMode != 'F') { |
|
515
|
|
|
exit; |
|
516
|
|
|
} |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
/** |
|
520
|
|
|
* Gets the watermark from the platform or a course |
|
521
|
|
|
* @param string course code (optional) |
|
522
|
|
|
* @param mixed web path of the watermark image, false if there is nothing to return |
|
523
|
|
|
*/ |
|
524
|
|
|
public static function get_watermark($course_code = null) |
|
525
|
|
|
{ |
|
526
|
|
|
$web_path = false; |
|
527
|
|
|
if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') { |
|
528
|
|
|
$course_info = api_get_course_info($course_code); |
|
529
|
|
|
$store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path |
|
530
|
|
|
if (file_exists($store_path)) { |
|
531
|
|
|
$web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; |
|
532
|
|
|
} |
|
533
|
|
View Code Duplication |
} else { |
|
534
|
|
|
$store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; // course path |
|
535
|
|
|
if (file_exists($store_path)) |
|
536
|
|
|
$web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; |
|
537
|
|
|
} |
|
538
|
|
|
return $web_path; |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Deletes the watermark from the Platform or Course |
|
543
|
|
|
* @param string $course_code course code (optional) |
|
544
|
|
|
* @param mixed web path of the watermark image, false if there is nothing to return |
|
545
|
|
|
* @return bool |
|
546
|
|
|
*/ |
|
547
|
|
|
public function delete_watermark($course_code = null) |
|
548
|
|
|
{ |
|
549
|
|
|
if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') { |
|
550
|
|
|
$course_info = api_get_course_info($course_code); |
|
551
|
|
|
// course path |
|
552
|
|
|
$store_path = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/'.api_get_current_access_url_id().'_pdf_watermark.png'; |
|
553
|
|
|
} else { |
|
554
|
|
|
// course path |
|
555
|
|
|
$store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; |
|
556
|
|
|
} |
|
557
|
|
|
if (file_exists($store_path)) { |
|
558
|
|
|
unlink($store_path); |
|
559
|
|
|
return true; |
|
560
|
|
|
} |
|
561
|
|
|
return false; |
|
562
|
|
|
} |
|
563
|
|
|
|
|
564
|
|
|
/** |
|
565
|
|
|
* Uploads the pdf watermark in the main/default_course_document directory or in the course directory |
|
566
|
|
|
* @param string $filename filename |
|
567
|
|
|
* @param string $source_file path of the file |
|
568
|
|
|
* @param string $course_code |
|
569
|
|
|
* @return mixed web path of the file if sucess, false otherwise |
|
570
|
|
|
*/ |
|
571
|
|
|
public function upload_watermark($filename, $source_file, $course_code = null) |
|
572
|
|
|
{ |
|
573
|
|
|
if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') { |
|
574
|
|
|
$course_info = api_get_course_info($course_code); |
|
575
|
|
|
$store_path = api_get_path(SYS_COURSE_PATH).$course_info['path']; // course path |
|
576
|
|
|
$web_path = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/pdf_watermark.png'; |
|
577
|
|
|
} else { |
|
578
|
|
|
$store_path = api_get_path(SYS_CODE_PATH).'default_course_document/images'; // course path |
|
579
|
|
|
$web_path = api_get_path(WEB_CODE_PATH).'default_course_document/images/'.api_get_current_access_url_id().'_pdf_watermark.png'; |
|
580
|
|
|
} |
|
581
|
|
|
$course_image = $store_path.'/'.api_get_current_access_url_id().'_pdf_watermark.png'; |
|
582
|
|
|
|
|
583
|
|
|
if (file_exists($course_image)) { |
|
584
|
|
|
@unlink($course_image); |
|
585
|
|
|
} |
|
586
|
|
|
$my_image = new Image($source_file); |
|
587
|
|
|
$result = $my_image->send_image($course_image, -1, 'png'); |
|
588
|
|
|
if ($result) { |
|
589
|
|
|
$result = $web_path; |
|
590
|
|
|
} |
|
591
|
|
|
return $result; |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
/** |
|
595
|
|
|
* Returns the default header |
|
596
|
|
|
*/ |
|
597
|
|
|
public function get_header($course_code = null) |
|
598
|
|
|
{ |
|
599
|
|
|
/*$header = api_get_setting('pdf_export_watermark_text'); |
|
600
|
|
|
if (!empty($course_code) && api_get_setting('pdf_export_watermark_by_course') == 'true') { |
|
601
|
|
|
$header = api_get_course_setting('pdf_export_watermark_text'); |
|
602
|
|
|
} |
|
603
|
|
|
return $header;*/ |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
/** |
|
607
|
|
|
* Sets the PDF footer |
|
608
|
|
|
*/ |
|
609
|
|
|
public function set_footer() |
|
610
|
|
|
{ |
|
611
|
|
|
$this->pdf->defaultfooterfontsize = 12; // in pts |
|
612
|
|
|
$this->pdf->defaultfooterfontstyle = 'B'; // blank, B, I, or BI |
|
613
|
|
|
$this->pdf->defaultfooterline = 1; // 1 to include line below header/above footer |
|
614
|
|
|
|
|
615
|
|
|
$view = new Template('', false, false, false, true, false, false); |
|
616
|
|
|
$template = $view->get_template('export/pdf_footer.tpl'); |
|
617
|
|
|
$footerHTML = $view->fetch($template); |
|
618
|
|
|
|
|
619
|
|
|
$this->pdf->SetHTMLFooter($footerHTML, 'E'); //Even pages |
|
620
|
|
|
$this->pdf->SetHTMLFooter($footerHTML, 'O'); //Odd pages |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Sets the PDF header |
|
625
|
|
|
* @param array $course_data |
|
626
|
|
|
*/ |
|
627
|
|
|
public function set_header($course_data) |
|
628
|
|
|
{ |
|
629
|
|
|
$this->pdf->defaultheaderfontsize = 10; // in pts |
|
630
|
|
|
$this->pdf->defaultheaderfontstyle = 'BI'; // blank, B, I, or BI |
|
631
|
|
|
$this->pdf->defaultheaderline = 1; // 1 to include line below header/above footer |
|
632
|
|
|
|
|
633
|
|
|
$userId = api_get_user_id(); |
|
634
|
|
|
|
|
635
|
|
|
if (!empty($course_data['code'])) { |
|
636
|
|
|
$teacher_list = CourseManager::get_teacher_list_from_course_code($course_data['code']); |
|
637
|
|
|
|
|
638
|
|
|
$teachers = ''; |
|
639
|
|
View Code Duplication |
if (!empty($teacher_list)) { |
|
640
|
|
|
foreach ($teacher_list as $teacher) { |
|
641
|
|
|
if ($teacher['user_id'] != $userId) { |
|
642
|
|
|
continue; |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
// Do not show the teacher list see BT#4080 only the current teacher name |
|
646
|
|
|
$teachers = api_get_person_name($teacher['firstname'], $teacher['lastname']); |
|
647
|
|
|
} |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
$organization = ChamiloApi::getPlatformLogo(); |
|
651
|
|
|
// Use custom logo image. |
|
652
|
|
|
$pdfLogo = api_get_setting('pdf_logo_header'); |
|
653
|
|
|
if ($pdfLogo === 'true') { |
|
654
|
|
|
$visualTheme = api_get_visual_theme(); |
|
655
|
|
|
$img = api_get_path(SYS_CSS_PATH).'themes/'.$visualTheme.'/images/pdf_logo_header.png'; |
|
656
|
|
|
if (file_exists($img)) { |
|
657
|
|
|
$img = api_get_path(WEB_CSS_PATH).'themes/'.$visualTheme.'/images/pdf_logo_header.png'; |
|
658
|
|
|
$organization = "<img src='$img'>"; |
|
659
|
|
|
} |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
$view = new Template('', false, false, false, true, false, false); |
|
663
|
|
|
$view->assign('teacher_name', $teachers); |
|
664
|
|
|
$view->assign('organization', $organization); |
|
665
|
|
|
$template = $view->get_template('export/pdf_header.tpl'); |
|
666
|
|
|
$headerHTML = $view->fetch($template); |
|
667
|
|
|
|
|
668
|
|
|
$this->pdf->SetHTMLHeader($headerHTML, 'E'); |
|
669
|
|
|
$this->pdf->SetHTMLHeader($headerHTML, 'O'); |
|
670
|
|
|
} |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* @param array $header html content |
|
675
|
|
|
*/ |
|
676
|
|
|
public function set_custom_header($header) |
|
677
|
|
|
{ |
|
678
|
|
|
$this->custom_header = $header; |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
/** |
|
682
|
|
|
* @param array $footer html content |
|
683
|
|
|
*/ |
|
684
|
|
|
public function set_custom_footer($footer) |
|
685
|
|
|
{ |
|
686
|
|
|
$this->custom_footer = $footer; |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
/** |
|
690
|
|
|
* Pre-formats a PDF to the right size and, if not stated otherwise, with |
|
691
|
|
|
* header, footer and watermark (if any) |
|
692
|
|
|
* @param array $course_data General course information (to fill headers) |
|
693
|
|
|
* @param bool $complete Whether we want headers, footers and watermark or not |
|
694
|
|
|
*/ |
|
695
|
|
|
public function format_pdf($course_data, $complete = true) |
|
696
|
|
|
{ |
|
697
|
|
|
if ($complete === false) { |
|
698
|
|
|
error_log('Asked with no decoration'); |
|
699
|
|
|
} |
|
700
|
|
|
$course_code = null; |
|
701
|
|
|
if (!empty($course_data)) { |
|
702
|
|
|
$course_code = $course_data['code']; |
|
703
|
|
|
} |
|
704
|
|
|
|
|
705
|
|
|
/*$pdf->SetAuthor('Documents Chamilo'); |
|
706
|
|
|
$pdf->SetTitle('title'); |
|
707
|
|
|
$pdf->SetSubject('Exported from Chamilo Documents'); |
|
708
|
|
|
$pdf->SetKeywords('Chamilo Documents'); |
|
709
|
|
|
*/ |
|
710
|
|
|
// TODO: To be read from the html document. |
|
711
|
|
|
$this->pdf->directionality = api_get_text_direction(); |
|
712
|
|
|
$this->pdf->useOnlyCoreFonts = true; |
|
713
|
|
|
// Use different Odd/Even headers and footers and mirror margins |
|
714
|
|
|
$this->pdf->mirrorMargins = 1; |
|
715
|
|
|
|
|
716
|
|
|
// Add decoration only if not stated otherwise |
|
717
|
|
|
if ($complete) { |
|
718
|
|
|
// Adding watermark |
|
719
|
|
|
if (api_get_setting('pdf_export_watermark_enable') == 'true') { |
|
720
|
|
|
$watermark_file = self::get_watermark($course_code); |
|
721
|
|
|
|
|
722
|
|
|
if ($watermark_file) { |
|
723
|
|
|
//http://mpdf1.com/manual/index.php?tid=269&searchstring=watermark |
|
724
|
|
|
$this->pdf->SetWatermarkImage($watermark_file); |
|
725
|
|
|
$this->pdf->showWatermarkImage = true; |
|
726
|
|
|
} else { |
|
727
|
|
|
$watermark_file = self::get_watermark(null); |
|
728
|
|
|
if ($watermark_file) { |
|
729
|
|
|
$this->pdf->SetWatermarkImage($watermark_file); |
|
730
|
|
|
$this->pdf->showWatermarkImage = true; |
|
731
|
|
|
} |
|
732
|
|
|
} |
|
733
|
|
|
if ($course_code) { |
|
734
|
|
|
$watermark_text = api_get_course_setting('pdf_export_watermark_text'); |
|
735
|
|
|
if (empty($watermark_text)) { |
|
736
|
|
|
$watermark_text = api_get_setting('pdf_export_watermark_text'); |
|
737
|
|
|
} |
|
738
|
|
|
} else { |
|
739
|
|
|
$watermark_text = api_get_setting('pdf_export_watermark_text'); |
|
740
|
|
|
} |
|
741
|
|
|
if (!empty($watermark_text)) { |
|
742
|
|
|
$this->pdf->SetWatermarkText( |
|
743
|
|
|
strcode2utf($watermark_text), |
|
744
|
|
|
0.1 |
|
745
|
|
|
); |
|
746
|
|
|
$this->pdf->showWatermarkText = true; |
|
747
|
|
|
} |
|
748
|
|
|
} |
|
749
|
|
|
|
|
750
|
|
|
if (empty($this->custom_header)) { |
|
751
|
|
|
self::set_header($course_data); |
|
752
|
|
|
} else { |
|
753
|
|
|
$this->pdf->SetHTMLHeader($this->custom_header, 'E'); |
|
754
|
|
|
$this->pdf->SetHTMLHeader($this->custom_header, 'O'); |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
if (empty($this->custom_footer)) { |
|
758
|
|
|
self::set_footer(); |
|
759
|
|
|
} else { |
|
760
|
|
|
$this->pdf->SetHTMLFooter($this->custom_footer); |
|
761
|
|
|
} |
|
762
|
|
|
} |
|
763
|
|
|
} |
|
764
|
|
|
} |
|
765
|
|
|
|
This check looks for the
elsebranches ofifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elsebranches can be removed.could be turned into
This is much more concise to read.