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\Pdfclass source code. It creates its own instance ofMpdfobject. So creating your own instance incustom_config.phpwon't help you when you work withYii::$app->pdf.Looking at how
Mpdfinstance is created insidePdfclass, whatever is set in itsoptionsproperty will be passed intoMpdfas constructor parameter after some values being overwritten byPdfcomponent properties. That means that whatever you would pass toMpdfconstructor in yourcustom_config.phpneeds to be set up inoptionsproperty when configuringPdfcomponent.To keep your
config/web.phpclean you can prepare options in extra file, for exampleconfig/pdf.php:As you can see I've left out
mode,format,tempDiranddefault_fontoptions, because these are overwritten byPdfcomponent properties so we need to set them there. You don't need to requirevendor/autoload.phpbecause that is already included inweb/index.phpfile before config is loaded.Now we need to use this file in your
Pdfcomponent setting inconfig/web.php.This should make sure the
Mpdfinstance inPdfcomponent is created with configuration you've set up.