4/16/2017

How to use Mangal Font with `mPDF PHP` Library


Similar Questions

  • How to use Hindi with mPDF
  • How to use Custom Font with mPDF
  • How to use UTF-8 Font with mPDF
  • How to use UTF-8 Data with mPDF

Simple Project Structure

app
    assets
        css
        js
        img
    bin
    bower_components
    vendor
        mpdf
            mpdf

Steps

  1. First Copy and Paste Custom Font to ./vendor/mpdf/mpdf/ttfonts Directory
    So Full path to font will be ./vendor/mpdf/mpdf/ttfonts/MANGAL.TTF

  2. Update Fonts Config File of mPDF Library ./vendor/mpdf/mpdf/config_fonts.php

$this->fontdata = array(
    "mangal" => array(
        'R' => "MANGAL.TTF",
        'useOTL' => 0xFF,
    ),

    ....

);

  1. Update HTML-DOM/CSS property for font-family
    So find and replace font-family:{old_font} to font-family:mangal

  2. You can create basic CSS Rules for it

body {
    font-family: mangal;
}

// OR

.hindi {
    font-family: mangal;
}

First Install mPDF library using Composer

php /home/ssp/composer.phar require mpdf/mpdf

First Demo Normal Case

<?php 
// Using mPDF Library
$mpdf = new mPDF(''); 

// Normal Case
$test_case1 = '
<head>
<title>Document in Hindi</title>
</head>
<body>
<p>यह एक डेमो टेस्ट है |</p>
</body></html>';

// Set some flags for mPDF
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;

// Read and Parse HTML Content
$mpdf->WriteHTML($test_case1);
// Display PDF to Browser
$mpdf->Output();
exit;

Using Custom Style for any HTML Element

<?php 
$test_case2 = '
<head>
<title>Document in Hindi</title>
</head>
<body style="font-family: mangal; font-size: 20px;">
<p>यह एक डेमो टेस्ट है |</p>
</body></html>';

$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;

$mpdf->WriteHTML($test_case2);
$mpdf->Output();
exit;

Using Custom Stylesheet (CSS)

<?php 
$test_case3 = '
<head>
<title>Document in Hindi</title>
<style>
body {
    font-family: mangal;
    font-size: 15px;
    color: red;
}
</style>
</head>
<body style="font-family: mangal; font-size: 20px;">
<p>यह एक डेमो टेस्ट है |</p>
</body></html>';

$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;

$mpdf->WriteHTML($test_case3);
$mpdf->Output();
exit;

Download Fonts for Hindi India

  1. http://indiatyping.com/index.php/download/hindi-fonts
  2. http://indiatyping.com/index.php/download/mangal-font
Post a Comment