Use mPDF v8

pull/4187/head^2
Angel Fernando Quiroz Campos 4 years ago
parent a27203e5f2
commit 935f037972
  1. 2
      composer.json
  2. 55
      main/inc/lib/pdf.lib.php

@ -80,7 +80,7 @@
"media-alchemyst/media-alchemyst": "~0.5", "media-alchemyst/media-alchemyst": "~0.5",
"michelf/php-markdown": "~1.7", "michelf/php-markdown": "~1.7",
"monolog/monolog": "~1.0", "monolog/monolog": "~1.0",
"mpdf/mpdf": "6.1.*", "mpdf/mpdf": "^8.0",
"ocramius/proxy-manager": "~1.0|2.0.*", "ocramius/proxy-manager": "~1.0|2.0.*",
"onelogin/php-saml": "^3.0", "onelogin/php-saml": "^3.0",
"packbackbooks/lti-1p3-tool": "^1.1", "packbackbooks/lti-1p3-tool": "^1.1",

@ -2,13 +2,16 @@
/* See license terms in /license.txt */ /* See license terms in /license.txt */
use Chamilo\CoreBundle\Component\Utils\ChamiloApi; use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
use Mpdf\Mpdf;
use Mpdf\MpdfException;
use Mpdf\Utils\UtfString;
/** /**
* Class PDF. * Class PDF.
*/ */
class PDF class PDF
{ {
/** @var mPDF */ /** @var Mpdf */
public $pdf; public $pdf;
public $custom_header = []; public $custom_header = [];
public $custom_footer = []; public $custom_footer = [];
@ -16,13 +19,15 @@ class PDF
public $template; public $template;
/** /**
* Creates the mPDF object. * Creates the Mpdf object.
* *
* @param string $pageFormat format A4 A4-L see * @param string $pageFormat format A4 A4-L see
* http://mpdf1.com/manual/index.php?tid=184&searchstring=format * http://mpdf1.com/manual/index.php?tid=184&searchstring=format
* @param string $orientation orientation "P" = Portrait "L" = Landscape * @param string $orientation orientation "P" = Portrait "L" = Landscape
* @param array $params * @param array $params
* @param Template $template * @param Template $template
*
* @throws MpdfException
*/ */
public function __construct( public function __construct(
$pageFormat = 'A4', $pageFormat = 'A4',
@ -31,7 +36,7 @@ class PDF
$template = null $template = null
) { ) {
$this->template = $template; $this->template = $template;
/* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=mPDF */ /* More info @ http://mpdf1.com/manual/index.php?tid=184&searchstring=Mpdf */
if (!in_array($orientation, ['P', 'L'])) { if (!in_array($orientation, ['P', 'L'])) {
$orientation = 'P'; $orientation = 'P';
} }
@ -58,18 +63,21 @@ class PDF
$this->params['pdf_date'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_TIME_FORMAT_LONG); $this->params['pdf_date'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_TIME_FORMAT_LONG);
$this->params['pdf_date_only'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_FORMAT_LONG); $this->params['pdf_date_only'] = isset($params['pdf_date']) ? $params['pdf_date'] : api_format_date($localTime, DATE_FORMAT_LONG);
@$this->pdf = new mPDF( $this->pdf = new Mpdf(
'UTF-8', [
$pageFormat, 'mode' => 'UTF-8',
'', 'format' => $pageFormat,
'', 'default_font_size' => '',
$params['left'], 'default_font' => '',
$params['right'], 'margin_left' => $params['left'],
$params['top'], 'margin_right' => $params['right'],
$params['bottom'], 'margin_top' => $params['top'],
8, 'margin_bottom' => $params['bottom'],
8, 'margin_header' => 8,
$orientation 'margin_footer' => 8,
'orientation' => $orientation,
'tempDir' => api_get_path(SYS_ARCHIVE_PATH).'mpdf/',
]
); );
$this->pdf->margin_footer = $params['margin_footer']; $this->pdf->margin_footer = $params['margin_footer'];
@ -92,7 +100,9 @@ class PDF
* @param bool $addDefaultCss (bootstrap/default/base.css) * @param bool $addDefaultCss (bootstrap/default/base.css)
* @param array * @param array
* *
* @return string * @throws MpdfException
*
* @return string|null
*/ */
public function html_to_pdf_with_template( public function html_to_pdf_with_template(
$content, $content,
@ -192,7 +202,7 @@ class PDF
* @param string $mainTitle * @param string $mainTitle
* @param bool $generateToFile Optional. When it is TRUE, then the output file is move to app/cache * @param bool $generateToFile Optional. When it is TRUE, then the output file is move to app/cache
* *
* @throws \MpdfException * @throws MpdfException
* *
* @return false|null * @return false|null
*/ */
@ -342,7 +352,7 @@ class PDF
$document_html = self::fixImagesPaths($document_html, $course_data, $dirName); $document_html = self::fixImagesPaths($document_html, $course_data, $dirName);
// The library mPDF expects UTF-8 encoded input data. // The library Mpdf expects UTF-8 encoded input data.
api_set_encoding_html($document_html, 'UTF-8'); api_set_encoding_html($document_html, 'UTF-8');
// TODO: Maybe it is better idea the title to be passed through // TODO: Maybe it is better idea the title to be passed through
$title = api_get_title_html($document_html, 'UTF-8', 'UTF-8'); $title = api_get_title_html($document_html, 'UTF-8', 'UTF-8');
@ -394,13 +404,13 @@ class PDF
* @param bool $addDefaultCss * @param bool $addDefaultCss
* @param bool $completeHeader * @param bool $completeHeader
* *
* @throws MpdfException
*
* 'I' (print on standard output), * 'I' (print on standard output),
* 'D' (download file) (this is the default value), * 'D' (download file) (this is the default value),
* 'F' (save to local file) or * 'F' (save to local file) or
* 'S' (return as a string) * 'S' (return as a string)
* *
* @throws MpdfException
*
* @return string Web path * @return string Web path
*/ */
public function content_to_pdf( public function content_to_pdf(
@ -503,7 +513,7 @@ class PDF
$document_html = str_replace(api_get_path(WEB_UPLOAD_PATH), api_get_path(SYS_UPLOAD_PATH), $document_html); $document_html = str_replace(api_get_path(WEB_UPLOAD_PATH), api_get_path(SYS_UPLOAD_PATH), $document_html);
$document_html = str_replace(api_get_path(WEB_ARCHIVE_PATH), api_get_path(SYS_ARCHIVE_PATH), $document_html); $document_html = str_replace(api_get_path(WEB_ARCHIVE_PATH), api_get_path(SYS_ARCHIVE_PATH), $document_html);
// The library mPDF expects UTF-8 encoded input data. // The library Mpdf expects UTF-8 encoded input data.
api_set_encoding_html($document_html, 'UTF-8'); api_set_encoding_html($document_html, 'UTF-8');
// At the moment the title is retrieved from the html document itself. // At the moment the title is retrieved from the html document itself.
if ($returnHtml) { if ($returnHtml) {
@ -787,7 +797,6 @@ class PDF
*/ */
// TODO: To be read from the html document. // TODO: To be read from the html document.
$this->pdf->directionality = api_get_text_direction(); $this->pdf->directionality = api_get_text_direction();
$this->pdf->useOnlyCoreFonts = true;
// Use different Odd/Even headers and footers and mirror margins // Use different Odd/Even headers and footers and mirror margins
$this->pdf->mirrorMargins = 1; $this->pdf->mirrorMargins = 1;
@ -819,7 +828,7 @@ class PDF
if (!empty($watermark_text)) { if (!empty($watermark_text)) {
$this->pdf->SetWatermarkText( $this->pdf->SetWatermarkText(
strcode2utf($watermark_text), UtfString::strcode2utf($watermark_text),
0.1 0.1
); );
$this->pdf->showWatermarkText = true; $this->pdf->showWatermarkText = true;

Loading…
Cancel
Save