在 yii2 中添加自定义字体 kartik yii-mpdf 后,config_custom.php 应该放在哪里

问题描述 投票:0回答:1

创建自定义配置文件以更改 mpdf-yii2 中的字体类型后,我应该将该文件放在哪里以及如何调用它。 这是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'
]);

这是 /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',
            ],
        ],

以及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();

我正在尝试此代码添加了选项

      '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',
            ],
yii2 mpdf kartik-v
1个回答
0
投票

查看

kartik\mpdf\Pdf
类源代码。它创建自己的
Mpdf
对象实例。因此,当您使用
custom_config.php
时,在
Yii::$app->pdf
中创建自己的实例不会对您有帮助。

看看如何在

Mpdf
类中创建
Pdf
实例,在
options
组件属性覆盖某些值后,其
Mpdf
属性中设置的任何内容都将作为构造函数参数传递到
Pdf
中。这意味着您在配置
Mpdf
组件时,需要在
custom_config.php
属性中设置要传递给
options
中的
Pdf
构造函数的任何内容。

为了保持您的

config/web.php
干净,您可以在额外文件中准备选项,例如
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,
];

如您所见,我省略了

mode
format
tempDir
default_font
选项,因为这些选项被
Pdf
组件属性覆盖,因此我们需要在那里设置它们。 您不需要 require
vendor/autoload.php
,因为在加载配置之前它已经包含在
web/index.php
文件中。

现在我们需要在

Pdf
中的
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
],

这应该确保

Mpdf
组件中的
Pdf
实例是使用您设置的配置创建的。

© www.soinside.com 2019 - 2024. All rights reserved.