0

i am working with mpdf library with php to create dynamic PDF. PDF is creating with English characters but when i try to use hindi language's character, it prints ??????. Please suggest me that what should i do.I will be thank full to all.

<?php

require_once('config.php');
require_once __DIR__ . '/lib/functions.php';
require_once __DIR__ . '/lib/mpdf/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf(['utf-8', 'A4-C']); // New PDF object with encoding & page size
$mpdf->text_input_as_HTML = TRUE;

$userId = $_SESSION['user_id'];

$user = getsingledataByfeild('user_id', $userId, 'registered_user');
$Id = $_SESSION['did'];
$feeDetails = getsingledataByfeild('id', $Id, 'fee_calculation');
$authname = getdataByConditions(['id' => $feeDetails['authority']], 'development_authority');

$html = '<table class="table">
    <tr>
        <td><label> अथॉरिटी का नाम: <span>*</span></label></td>
        <td>' . $authname[0]['authority_name'] . '</td>
    </tr>
    <tr>
        <td><label> आवेदक का नाम  <span>*</span></label></td>
        <td>' . $feeDetails['applicant_name'] . '</td>
    </tr>
    <tr>
        <td><label> आवदेक का पता  : <span>*</span></label></td>
        <td>' . $feeDetails['applicant_address'] . '</td>
    </tr>
    <tr>
        <td><label> भूखण्ड संख्या : <span>*</span></label></td>
        <td>' . $feeDetails['land_no'] . '</td>
    </tr>
    <tr>
        <td><label> योजना का नाम : <span>*</span></label></td>
        <td>' . $feeDetails['plan_name'] . '</td>
    </tr>
    </table>'; 

$mpdf->setAutoTopMargin = 'stretch'; // Set pdf top margin to stretch to avoid content overlapping
$mpdf->setAutoBottomMargin = 'stretch'; // Set pdf bottom margin to stretch to avoid content overlapping

$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($html);
$mpdf->Output();
Finwe
  • 6,372
  • 2
  • 29
  • 44
Mohd Hasan
  • 324
  • 1
  • 4
  • 17
  • Have you checked this post: http://shishtpal.blogspot.com/2017/04/how-to-use-mangal-font-with-mpdf-php.html – Lovepreet Singh Jun 26 '18 at 08:42
  • I check and follow but still not working @LovepreetSingh – Mohd Hasan Jun 26 '18 at 10:38
  • 1
    You have three problems: (1.) **encoding:** if your PHP file is saved using the wrong encoding, the Hindi characters will become corrupted. (2) **font:** you need a font that supports those characters. I don't see you defining one. (3.) **ligatures:** even if the characters are rendered, they'll be wrong because Devanagari script isn't supported. Only a few PDF libraries support this. See [this white paper](https://developers.itextpdf.com/sites/default/files/attachments/Whitepaper_pdfCalligraph_2017.pdf) and the [iText 7 pdfCalligraph](https://itextpdf.com/itext7/pdfcalligraph) page. – Bruno Lowagie Jun 26 '18 at 12:28
  • thanks for your comment., i will try your suggession. My project is php based, so i can't change. – Mohd Hasan Jun 26 '18 at 12:56

4 Answers4

11
<?php

require_once( 'mpdf/mpdf.php');
$stylesheet = file_get_contents('css/style.css');

$html = '<table class="table">
    <tr>
        <td><label> अथॉरिटी का नाम: <span>*</span></label></td>
        <td>' . $authname[0]['authority_name'] . '</td>
    </tr>
    <tr>
        <td><label> आवेदक का नाम  <span>*</span></label></td>
        <td>' . $feeDetails['applicant_name'] . '</td>
    </tr>
    <tr>
        <td><label> आवदेक का पता  : <span>*</span></label></td>
        <td>' . $feeDetails['applicant_address'] . '</td>
    </tr>
    <tr>
        <td><label> भूखण्ड संख्या : <span>*</span></label></td>
        <td>' . $feeDetails['land_no'] . '</td>
    </tr>
    <tr>
        <td><label> योजना का नाम : <span>*</span></label></td>
        <td>' . $feeDetails['plan_name'] . '</td>
    </tr>
    </table>';


$mpdf = new mPDF('utf-8', 'A4-C');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($html);

//call watermark content aand image
$mpdf->SetWatermarkText('phpflow.COM');
$mpdf->showWatermarkText = true;
$mpdf->watermarkTextAlpha = 0.1;
$mpdf->Output("phpflow.pdf", 'F');

$mpdf->Output();

exit;
?>  

Create a CSS file on root and add following code in CSS:

p, td { font-family: freeserif; }

Working fine for me, check the below link...

Click here

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 1
    There is no mpdf folder inside vendor directory. actually ttffonts folder is located outside of vendor. – Mohd Hasan Jun 27 '18 at 05:32
  • 1
    find the config_fonts.php in your mpdf folder root and ttfonts fonts folder inside mpdf folder. You can add this php code $this->fontdata = array( "mangal" => array( 'R' => "MANGAL.TTF", 'useOTL' => 0xFF, ), .... ); – Virendra Yaduvanshi Jun 27 '18 at 05:57
  • i did'nt find any config_fonts.php in root directory – Mohd Hasan Jun 27 '18 at 06:07
0

You have to set a font supporting hindi characters, in your case especially for td elements. Freeserif works for me.

<style>
    p, td { font-family: freeserif; }
</style>

BTW, your mPDF constructor is off, the config array has to have keys (mode, format). Also. A4-C is not a valid format, the letter after the hyphen must be a P(ortrait) or a L(andscape).

Finwe
  • 6,372
  • 2
  • 29
  • 44
0
<style>
   body, p, td, div { font-family: freesans; }
</style>
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I added these code:

$mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir().DIRECTORY_SEPARATOR.'mpdf','default_font' => 'serif', 'format' => 'A4-L']);
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;

Works fine for me.