php uses mpdf to configure font styles for specified fields

Keywords: PHP github

Two days ago, I was working on a pdf export function. The plug-in used was kartik-v/yii2-mpdf, and the plug-in used mpdf.

composer required kartik-v/yii2-mpdf

mpdf document address: https://mpdf.github.io/

There is a requirement that a specified field be set to a specified font in the exported pdf. Other information does not use this font.

At first, I added font data of mpdf to the font file I needed to add.

//Configure font directory
'fontDir' => array_merge($fontDirs, [
    Yii::getAlias('@common/pdf/fonts'),
]),
//Configure fonts
'fontdata' => ArrayHelper::merge($fontData, [
    'sun-exta' => [
        'R' => 'my.ttf',
        'sip-ext' => 'sun-extb',
    ]
]),

But when I configure the above configuration, I find that all the information of pdf has changed to the specified font, which is the global configuration font.

So I thought I'd configure a font that I didn't need to use, and then quote it on the page.

So I configure a font:

'fontdata' => ArrayHelper::merge($fontData, [   
    'my' => [
        'R' => 'my.ttf',
    ],
]),

After configuration, add the following attributes to the pdf file we need to generate

style="font-family: my;"

Add it and test it later.

But we found that:

My other information became a little box.

It looks like I didn't set my global font, so I did the global font configuration again.

As follows:

//Configuration font file directory
 'fontDir' => array_merge($fontDirs, [
    Yii::getAlias('@common/pdf/fonts'),
]),
'fontdata' => ArrayHelper::merge($fontData, [
    //Configure global fonts
    'sun-exta' => [
        'R' => 'msyh.ttf',
        'sip-ext' => 'sun-extb',
    ],
    //Configure fonts for specified information
    'my' => [
        'R' => 'my.ttf',
    ],
]),

When the configuration is complete, no effect is found, so the global font style is introduced into the pdf file that needs to be generated

body { font-family: sun-exta;}

Success after introduction

Full configuration using kartik-v/yii2-mpdf

$pdf = new Pdf([
    'mode' => Pdf::MODE_UTF8,
    'cssFile' => '@common/pdf/assets/score-mpdf.css',//css file address
    //mpdf configuration
    'options' => [
        'debug' => true,
        'autoScriptToLang' => true,
        'autoLangToFont' => true,
        'ignore_invalid_utf8' => true,
        'baseScript' => \Mpdf\Ucdn::SCRIPT_HAN,
        'tabSpaces' => 4,
        'fontDir' => array_merge($fontDirs, [
            Yii::getAlias('@common/pdf/fonts'),//Font file directory
        ]),
        'fontdata' => ArrayHelper::merge($fontData, [
            'sun-exta' => [
                'R' => 'msyh.ttf',
                'sip-ext' => 'sun-extb',
            ],
            'my' => [
                'R' => 'my.ttf',
            ],
        ]),
    ]
]);
$content = $this->renderPartial('@common/pdf/score.php', [
    'user' => $model,//Information transmitted
]);
$fileName = 'test pdf.pdf';
return $pdf->output($content, $fileName, Pdf::DEST_DOWNLOAD);

Depending on the above, you can configure the font style for the information specified in pdf

Posted by nano on Tue, 30 Jul 2019 23:04:26 -0700