I have reinstalled my Yii2 application using newest server stack from Bitnami. But there is one remaining issue that I cannot resolve in any way.
Mpdf does not see my custom fonts despite having defined this before Pdf instantiation:
define("_MPDF_SYSTEM_TTFONTS", Yii::getAlias('@webroot/fonts/'));
I am getting this error:
2020-11-28 15:29:08 [5.173.161.70][-][is46fr5fu08dvg8lthf00obqio][error][Mpdf\MpdfException:1] Mpdf\MpdfException: Cannot find TTF TrueType font file "Roboto-Regular.ttf" in configured font directories. in /opt/bitnami/hrpanel2/vendor/mpdf/mpdf/src/Fonts/FontFileFinder.php:33
Stack trace:
#0 /opt/bitnami/hrpanel2/vendor/mpdf/mpdf/src/Mpdf.php(3861): Mpdf\Fonts\FontFileFinder->findFontFile()
#1 /opt/bitnami/hrpanel2/vendor/mpdf/mpdf/src/Mpdf.php(4154): Mpdf\Mpdf->AddFont()
#2 /opt/bitnami/hrpanel2/vendor/mpdf/mpdf/src/Mpdf.php(10875): Mpdf\Mpdf->SetFont()
#3 /opt/bitnami/hrpanel2/vendor/mpdf/mpdf/src/Mpdf.php(1483): Mpdf\Mpdf->SetDefaultFont()
#4 /opt/bitnami/hrpanel2/vendor/kartik-v/yii2-mpdf/src/Pdf.php(271): Mpdf\Mpdf->__construct()
#5 /opt/bitnami/hrpanel2/vendor/kartik-v/yii2-mpdf/src/Pdf.php(247): kartik\mpdf\Pdf->setApi()
#6 /opt/bitnami/hrpanel2/vendor/kartik-v/yii2-mpdf/src/Pdf.php(359): kartik\mpdf\Pdf->getApi()
#7 /opt/bitnami/hrpanel2/vendor/kartik-v/yii2-mpdf/src/Pdf.php(236): kartik\mpdf\Pdf->output()
#8 /opt/bitnami/hrpanel2/common/models/Report.php(208): kartik\mpdf\Pdf->render()
#9 /opt/bitnami/hrpanel2/frontend/modules/urzad/controllers/ReportController.php(54): common\models\Report->download()
I have also tried stating absolute path instead of @webroot but it did not change anything.
I suppose this has stopped working because of the updated components, but I cannot figure out how to correct this. Any hints will be appreciated.
Here is my composer.json:
"name": "yiisoft/yii2-app-advanced",
"description": "Yii 2 Advanced Application Template",
"keywords": ["yii2", "framework", "advanced", "application template"],
"homepage": "http://www.yiiframework.com/",
"type": "project",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"minimum-stability": "stable",
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "*",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "*",
"lajax/yii2-translate-manager": "1.*",
"kartik-v/yii2-editable": "*",
"mongosoft/yii2-soap-server": "*",
"phpoffice/phpexcel": "*",
"kartik-v/yii2-mpdf": "*",
"kartik-v/yii2-grid": "@dev",
"yiisoft/yii2-jui": "~2.0.0",
"2amigos/yii2-ckeditor-widget" : "~1.0",
"kartik-v/yii2-widget-switchinput": "*",
"philippfrenzel/yii2fullcalendar":"*",
"yiisoft/yii2-imagine": "~2.0.0",
"kartik-v/yii2-detail-view": "*",
"kartik-v/yii2-widget-datepicker": "@dev",
"tuyakhov/yii2-youtube": "*"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",
"yiisoft/yii2-debug": "*",
"yiisoft/yii2-gii": "*",
"yiisoft/yii2-faker": "*",
"yiisoft/yii2-swiftmailer": "*",
"lajax/yii2-translate-manager": "1.*",
"kartik-v/yii2-editable": "*",
"mongosoft/yii2-soap-server": "*",
"phpoffice/phpexcel": "*",
"kartik-v/yii2-mpdf": "*",
"kartik-v/yii2-grid": "@dev",
"yiisoft/yii2-jui": "~2.0.0",
"2amigos/yii2-ckeditor-widget" : "~1.0",
"kartik-v/yii2-widget-switchinput": "*",
"philippfrenzel/yii2fullcalendar":"*",
"yiisoft/yii2-imagine": "~2.0.0",
"kartik-v/yii2-detail-view": "*",
"kartik-v/yii2-widget-datepicker": "@dev",
"tuyakhov/yii2-youtube": "*"
},
"config": {
"process-timeout": 1800
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
Here is complete code of the function with Pdf definition:
define("_MPDF_SYSTEM_TTFONTS", Yii::getAlias('@webroot/fonts/'));
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
$content = Yii::$app->controller->renderPartial($view, $data);
$header = Yii::$app->controller->renderPartial($headerView, $data);
$cssInline = file_get_contents(Yii::getAlias('@webroot/source/css/report.css'));
$pdf = new Pdf([
'mode' => Pdf::MODE_UTF8,
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => $destination,
'content' => $content,
'cssInline' => $cssInline,
'cssFile' => Yii::getAlias('@webroot/source/css/common_rep.css'),
'defaultFont' => 'roboto',
'marginTop' => isset($margins[0]) ? $margins[0] : '',
'marginRight' => isset($margins[1]) ? $margins[1] : '',
'marginBottom' => isset($margins[2]) ? $margins[2] : '',
'marginLeft' => isset($margins[3]) ? $margins[3] : '',
'marginHeader' => isset($margins[4]) ? $margins[4] : '',
'options' => [
'title' => $this->getDocTitle(),
'img_dpi' => 300,
'debug' => true,
'fontdata' => [
"dejavusanscondensed" => [
'R' => "Roboto-Regular.ttf",
'I' => "Roboto-Regular.ttf",
'B' => "Roboto-Bold.ttf",
],
]
],
'methods' => [
'SetHTMLHeader' => [$header],
'setHTMLFooter'=>['<table width="100%"><tr><td width="25%"></td><td width="50%" align="center"><img src="/img/logo-grey.png"/></td><td width="25%" align="right" valign="bottom">{PAGENO}</td></tr></table>'],
]
]);
$pdf->filename = $this->getDocFilename(). '.pdf';
return $pdf->render();
Your dependency of kartik-v/yii2-mpdf
without a constraint installed the last mPDF version 8.x which handles fonts differently.
Quick solution: change your composer constraint to "kartik-v/yii2-mpdf": "1.0.1"
and do a composer update, which will install mPDF 6.x where _MPDF_SYSTEM_TTFONTS
still works.
If you need newer PHP support, first change your PHP needs in your composer.json file - that will rule out a few potentialy non-compatible libraries on composer update.
Then install the last version of kartik-v/yii2-mpdf
(also with an upper version bound) and set up its options
key like this:
$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$pdf = new Pdf([
...
'options' => [
...
'fontDir' => array_merge($fontDirs, [
Yii::getAlias('@webroot/fonts/'),
]),
'fontdata' => array_merge($fontData, [
"dejavusanscondensed" => [
'R' => "Roboto-Regular.ttf",
'I' => "Roboto-Regular.ttf",
'B' => "Roboto-Bold.ttf",
],
]),
'default_font' => 'dejavusanscondensed'
],
...
]);
Maybe name the font "roboto" correctly to prevent confusion in the future.
Consult the documentation for further details. https://mpdf.github.io/fonts-languages/fonts-in-mpdf-7-x.html
Generally, you don't want to have asterisks *
as constraints in your composer.json. Always include upper bound to prevent these changes in behaviour.