After i create the custom config file for change the font type in mpdf-yii2 where should i put this file and how i can call it. this is the custom_config.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdfConfig = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'format' => [190, 236],
'tempDir' => __DIR__ . '/web/css/fonts',
'fontDir' => array_merge($fontDirs, [
__DIR__ . 'web/css/fonts/tajawal'
]),
'fontdata' => $fontData + [
'tajawal' => [
'R' => "Tajawal-Regular.ttf",
'L' => "Tajawal-Light.ttf",
'B' => "Tajawal-Bold.ttf",
'useOTL' => 0xFF,
'useKashida' => 75,
],
],
'default_font' => 'tajawal'
]);
this is configuration for this library in /config/web.php
'pdf' => [
'class' => Pdf::classname(),
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.row { margin: 5px 0px 0px 0px !important; padding: 0px !important; }',
'methods' => [
'SetTitle' => 'Title',
],
],
and this code in controller.php
$pdf = Yii::$app->pdf;
$pdf->methods = [
'SetTitle' => $pdfTitle ,
];
$mpdf = $pdf->api;
$mpdf->setAutoBottomMargin = 'pad';
if($language == 0) {
$mpdf->SetDirectionality('ltr');
} else {
$mpdf->SetDirectionality('rtl');
}
$pdf->content = $content;
return $pdf->render();
I am try it this code added the options
'pdf' => [
'class' => Pdf::classname(),
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
// 'marginLeft'=>0,
// 'marginRight'=>0,
// 'marginFooter'=>20,
'cssInline' => '.row { margin: 5px 0px 0px 0px !important; padding: 0px !important; }',
'methods' => [
'SetTitle' => 'Title',
],
'options' => [
'config' => $_SERVER['DOCUMENT_ROOT'].'/css/fonts/config-fonts.php',
],
Looking at kartik\mpdf\Pdf
class source code. It creates its own instance of Mpdf
object. So creating your own instance in custom_config.php
won't help you when you work with Yii::$app->pdf
.
Looking at how Mpdf
instance is created inside Pdf
class, whatever is set in its options
property will be passed into Mpdf
as constructor parameter after some values being overwritten by Pdf
component properties. That means that whatever you would pass to Mpdf
constructor in your custom_config.php
needs to be set up in options
property when configuring Pdf
component.
To keep your config/web.php
clean you can prepare options in extra file, for example config/pdf.php
:
$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
return [
// your font configurations
'fontDir' => array_merge($fontDirs, [
dirname(__DIR__) . '/web/css/fonts/tajawal', // the dirname() is used here because __DIR__ points to `config` directory.
]),
'fontdata' => $fontData + [
'tajawal' => [
'R' => "Tajawal-Regular.ttf",
'L' => "Tajawal-Light.ttf",
'B' => "Tajawal-Bold.ttf",
'useOTL' => 0xFF,
'useKashida' => 75,
],
],
// keep defaults that are set up in Pdf::$options
'autoScriptToLang' => true,
'ignore_invalid_utf8' => true,
'tabSpaces' => 4,
];
As you can see I've left out mode
, format
, tempDir
and default_font
options, because these are overwritten by Pdf
component properties so we need to set them there.
You don't need to require vendor/autoload.php
because that is already included in web/index.php
file before config is loaded.
Now we need to use this file in your Pdf
component setting in config/web.php
.
'pdf' => [
'class' => Pdf::classname(),
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.row { margin: 5px 0px 0px 0px !important; padding: 0px !important; }',
'methods' => [
'SetTitle' => 'Title',
],
'mode' => Pdf::MODE_UTF8, // we will use constant defined in Pdf class instead of plain text
'tempPath' => dirname(__DIR__) . '/web/css/fonts', // tempDir uses tempPath property instead. Make sure the path to temp dir matches actual directory structure
'defaultFont' => 'tajawal',
'options' => require __DIR__ . '/pdf.php', // here we use the extra file with pdf configuration we've prepared
],
This should make sure the Mpdf
instance in Pdf
component is created with configuration you've set up.