การเพิ่ม Font ใน mPDF (Yii2 กับ kartik mpdf และ mPDF version 7.x)

wave
มานพ กองอุ่น 14 พ.ค. 2019 15:23:15 8,117

ในบทเรียนรู้นี้เรามาเพิ่ม Font ใน mPDF version 7 กัน โดย Concept คือ เพิ่ม Font Angsana และ Sarabun โดยมีขั้นตอนดังนี้

1.นำ Font Angsana.ttf และ Sarabun.ttf วางใน web/fonts/

2.ในหน้าสำหรับ Generate PDF เพิ่มการตั้งค่า Fonts ดังนี้

use kartik\mpdf\Pdf;
use Mpdf\Config\ConfigVariables;
use Mpdf\Config\FontVariables;
//...
public function actionPdf(){
$content = $this->renderPatial('_test_pdf');
$pdf = new Pdf([
            // set to use core fonts only
            'mode' => Pdf::MODE_UTF8,
            // A4 paper format
            'format' => Pdf::FORMAT_A4,
            // portrait orientation
            'orientation' => Pdf::ORIENT_PORTRAIT,
            // stream to browser inline
            'destination' => Pdf::DEST_BROWSER,
            // your html content input
            'content' => $content,
            // format content from your own css file if needed or use the
            // enhanced bootstrap css built by Krajee for mPDF formatting
            'cssFile' => '@frontend/web/css/pdf.css',
            // any css to be embedded if required
            'cssInline' => '.table-border table{ border-collapse: collapse;} .table-border td { border: thin solid #666666; }',
            // set mPDF properties on the fly
            'options' => ['title' => 'Preview Report'],
            // call mPDF methods on the fly
            'methods' => [
                //'SetHeader'=>[''],
                //'SetFooter'=>['Page {PAGENO}/{nbpg}'],
            ],
            'marginLeft' => 10,
            'marginRight' => 10,
            'marginTop' => 10,
            'marginBottom' => 10,
            'marginFooter' => 5
        ]);

        $defaultConfig = (new ConfigVariables())->getDefaults();
        $fontDirs = $defaultConfig['fontDir'];

        $defaultFontConfig = (new FontVariables())->getDefaults();
        $fontData = $defaultFontConfig['fontdata'];

        $pdf->options['fontDir'] = array_merge($fontDirs, [
            Yii::getAlias('@webroot').'/fonts'
        ]);



        $pdf->options['fontdata'] = $fontData + [
                'angsana' => [
                    'R' => 'Angsana.ttf',
                    'TTCfontID' => [
                        'R' => 1,
                    ],
                ],
                'sarabun' => [
                    'R' => 'Sarabun.ttf',
                ]
            ];

        // return the pdf output as per the destination setting
        return $pdf->render();
}

ไฟล์ _test_pdf.php

<div style="font-family:angsana;">

เนื้อหา PDF

</div>

3.ทดลอง Run จะได้หน้าเว็บที่เป็นภาษาตามต้องการ


ความคิดเห็น

หากบทเรียนรู้มีความผิดพลาดประการใด หรือมีข้อเสนอแนะกรุณาแจ้ง contact@programmerthailand.com

เขียนบทเรียนรู้ของคุณ

รายละเอียด
  • ดู 8,117
  • รักเลย 0
  • หมวดหมู่ Yii Framework 2 (Yii2)
  • เขียนเมื่อ
  • แก้ไขเมื่อ
  • Tags mPDF font add font
ข้อมูลผู้เขียน
มานพ กองอุ่น

มานพ กองอุ่น

เป็นสมาชิกเมื่อ: 18 ธ.ค. 2009

เนื้อหาที่เกี่ยวข้อง