Skip to content

Commit 935f037

Browse files
committed
Use mPDF v8
1 parent a27203e commit 935f037

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"media-alchemyst/media-alchemyst": "~0.5",
8181
"michelf/php-markdown": "~1.7",
8282
"monolog/monolog": "~1.0",
83-
"mpdf/mpdf": "6.1.*",
83+
"mpdf/mpdf": "^8.0",
8484
"ocramius/proxy-manager": "~1.0|2.0.*",
8585
"onelogin/php-saml": "^3.0",
8686
"packbackbooks/lti-1p3-tool": "^1.1",

main/inc/lib/pdf.lib.php

+32-23
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
/* See license terms in /license.txt */
33

44
use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
5+
use Mpdf\Mpdf;
6+
use Mpdf\MpdfException;
7+
use Mpdf\Utils\UtfString;
58

69
/**
710
* Class PDF.
811
*/
912
class PDF
1013
{
11-
/** @var mPDF */
14+
/** @var Mpdf */
1215
public $pdf;
1316
public $custom_header = [];
1417
public $custom_footer = [];
1518
public $params = [];
1619
public $template;
1720

1821
/**
19-
* Creates the mPDF object.
22+
* Creates the Mpdf object.
2023
*
2124
* @param string $pageFormat format A4 A4-L see
2225
* http://mpdf1.com/manual/index.php?tid=184&searchstring=format
2326
* @param string $orientation orientation "P" = Portrait "L" = Landscape
2427
* @param array $params
2528
* @param Template $template
29+
*
30+
* @throws MpdfException
2631
*/
2732
public function __construct(
2833
$pageFormat = 'A4',
@@ -31,7 +36,7 @@ public function __construct(
3136
$template = null
3237
) {
3338
$this->template = $template;
34-
/* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF */
39+
/* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=Mpdf */
3540
if (!in_array($orientation, ['P', 'L'])) {
3641
$orientation = 'P';
3742
}
@@ -58,18 +63,21 @@ public function __construct(
5863
$this->params['pdf_date'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_TIME_FORMAT_LONG);
5964
$this->params['pdf_date_only'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_FORMAT_LONG);
6065

61-
@$this->pdf = new mPDF(
62-
'UTF-8',
63-
$pageFormat,
64-
'',
65-
'',
66-
$params['left'],
67-
$params['right'],
68-
$params['top'],
69-
$params['bottom'],
70-
8,
71-
8,
72-
$orientation
66+
$this->pdf = new Mpdf(
67+
[
68+
'mode' => 'UTF-8',
69+
'format' => $pageFormat,
70+
'default_font_size' => '',
71+
'default_font' => '',
72+
'margin_left' => $params['left'],
73+
'margin_right' => $params['right'],
74+
'margin_top' => $params['top'],
75+
'margin_bottom' => $params['bottom'],
76+
'margin_header' => 8,
77+
'margin_footer' => 8,
78+
'orientation' => $orientation,
79+
'tempDir' => api_get_path(SYS_ARCHIVE_PATH).'mpdf/',
80+
]
7381
);
7482

7583
$this->pdf->margin_footer = $params['margin_footer'];
@@ -92,7 +100,9 @@ public function __construct(
92100
* @param bool $addDefaultCss (bootstrap/default/base.css)
93101
* @param array
94102
*
95-
* @return string
103+
* @throws MpdfException
104+
*
105+
* @return string|null
96106
*/
97107
public function html_to_pdf_with_template(
98108
$content,
@@ -192,7 +202,7 @@ public function html_to_pdf_with_template(
192202
* @param string $mainTitle
193203
* @param bool $generateToFile Optional. When it is TRUE, then the output file is move to app/cache
194204
*
195-
* @throws \MpdfException
205+
* @throws MpdfException
196206
*
197207
* @return false|null
198208
*/
@@ -342,7 +352,7 @@ public function html_to_pdf(
342352

343353
$document_html = self::fixImagesPaths($document_html, $course_data, $dirName);
344354

345-
// The library mPDF expects UTF-8 encoded input data.
355+
// The library Mpdf expects UTF-8 encoded input data.
346356
api_set_encoding_html($document_html, 'UTF-8');
347357
// TODO: Maybe it is better idea the title to be passed through
348358
$title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
@@ -394,13 +404,13 @@ public function html_to_pdf(
394404
* @param bool $addDefaultCss
395405
* @param bool $completeHeader
396406
*
407+
* @throws MpdfException
408+
*
397409
* 'I' (print on standard output),
398410
* 'D' (download file) (this is the default value),
399411
* 'F' (save to local file) or
400412
* 'S' (return as a string)
401413
*
402-
* @throws MpdfException
403-
*
404414
* @return string Web path
405415
*/
406416
public function content_to_pdf(
@@ -503,7 +513,7 @@ public function content_to_pdf(
503513
$document_html = str_replace(api_get_path(WEB_UPLOAD_PATH), api_get_path(SYS_UPLOAD_PATH), $document_html);
504514
$document_html = str_replace(api_get_path(WEB_ARCHIVE_PATH), api_get_path(SYS_ARCHIVE_PATH), $document_html);
505515

506-
// The library mPDF expects UTF-8 encoded input data.
516+
// The library Mpdf expects UTF-8 encoded input data.
507517
api_set_encoding_html($document_html, 'UTF-8');
508518
// At the moment the title is retrieved from the html document itself.
509519
if ($returnHtml) {
@@ -787,7 +797,6 @@ public function format_pdf($courseInfo, $complete = true)
787797
*/
788798
// TODO: To be read from the html document.
789799
$this->pdf->directionality = api_get_text_direction();
790-
$this->pdf->useOnlyCoreFonts = true;
791800
// Use different Odd/Even headers and footers and mirror margins
792801
$this->pdf->mirrorMargins = 1;
793802

@@ -819,7 +828,7 @@ public function format_pdf($courseInfo, $complete = true)
819828

820829
if (!empty($watermark_text)) {
821830
$this->pdf->SetWatermarkText(
822-
strcode2utf($watermark_text),
831+
UtfString::strcode2utf($watermark_text),
823832
0.1
824833
);
825834
$this->pdf->showWatermarkText = true;

0 commit comments

Comments
 (0)