【问题标题】:how to show HINDI font correctly by using mpdf library or dompdf in php?如何在 php 中使用 mpdf 库或 dompdf 正确显示 HINDI 字体?
【发布时间】:2020-07-01 23:38:02
【问题描述】:

首先我尝试了 MPDF,但印地语字体无法正常工作。我正确添加并转换了 utf8。连接的自定义“MANGAL”字体在 MPDF 库中仍然不起作用 之后我尝试使用 DOMPDF 并再次与这个库出现同样的问题。

我附上了下面给出的代码,请看一下

DOMPDF 代码

<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf as Dompdf;
use Dompdf\Options;

define("DOMPDF_UNICODE_ENABLED", true);
define("DOMPDF_ENABLE_REMOTE", false);
define("DOMPDF_ENABLE_CSS_FLOAT", false);
define ("DOMPDF_ENABLE_FONTSUBSETTING",false);
define("DOMPDF_ENABLE_HTML5PARSER",false);

define("DEBUGCSS",false);
define("DEBUG_LAYOUT",false);
define("DEBUG_LAYOUT_LINES",true);
define("DEBUG_LAYOUT_BLOCKS",true);
define("DEBUG_LAYOUT_INLINE",true);	
define("DEBUG_LAYOUT_PADDINGBOX	",true);

ob_start();


?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    
<meta content="utf-8" http-equiv="encoding">
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
     <style type="text/css" >
      @font-face {
            font-family: 'Noto Sans', sans-serif;
            font-style: normal;
            font-weight: 400;
        }
     body {
    font-family: DejaVu Sans;
}
        * {
      font-family: "DejaVu Sans Mono", monospace;
    }
  .wrapper
  {
    border: 1pt solid black;
    width: 100%;
    height: 100%;
  }


@font-face {
  font-family: "mangal";
  font-style: normal;
  font-weight: normal;
  src: url(Mangal.ttf) format("truetype");
}
        *{outline: none;box-sizing: border-box;}
        
</style>
<style>
.head{
    text-align: center;
    position: relative;


}

.subhead{
    text-transform: uppercase;
    border-bottom: 2px solid #2f2f2f;
    border-top: 2px solid #2f2f2f;
    padding: 12px 0 12px 0;

}

.content{
    font-size: 0;
    line-height: 0;
    word-spacing: -.31em;
    display: inline-block;
    margin: 30px 2% 0 2%;


}

table {
  table-layout: fixed;
}
table td {height:200px;margin:5px;}
table th {height:200px;}
p{padding:4px;}
h2{padding:4px;}
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{ font-family: "mangal", serif;font-size:66px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{ font-family: "mangal", serif;font-size:16px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
</head>
<body>
<div id="wrapper">
    <div class="container m-b-30">
        <div class="row">

            <div id="content" class="col-sm-12 m-b-30">

                <div class="row">
             <div class="head" style="text-align: center;
    position: relative;">
    <div class="headerobjectswrapper">
        <header style="font-size:25px;
    font-weight: bold;
    padding: 20px;
    text-transform: UPPERCASE;
    clear: both;">TITLE</header>
    </div>

    <div class="subhead">Jabalpur,MP - Tuesday March 3, 2020</div>
</div>
<table class="tg" style="width:100%" border="1" cellspacing="0">
    
    <tbody>
  <tr style="width:1000px">
    <th class="tg-c3ow"> <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" style="width:270px;height:100px"/></th>
    <th class="tg-0lax"><img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" style="width:270px;height:100px"/></th>
    <th class="tg-0lax"><img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" style="width:270px;height:100px"/></th>
    <th class="tg-0lax"><img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" style="width:270px;height:100px"/></th>
    <th class="tg-0lax"><img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" style="width:270px;height:100px"/></th>
    <th class="tg-0lax"><img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" style="width:270px;height:100px"/></th>
  </tr>
  <tr>
      <td class="tg-0lax" >
          <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" class="img-responsive" style="width:670px;height:600px"/>
      </td>
    <td colspan="4" class="tg-0lax" style=" font-size:66px;"><h2 style="text-align:center; font-size:66px; font-family: "mangal", serif;" lang="hi">
    गौ नर्मदा कुंभ का नर्मदा पूजन व कन्या पूजन के साथ भव्य शुभारम्भ
</h2>
</td>
<td class="tg-0lax" >
          <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" class="img-responsive" style="width:670px;height:600px"/>
      </td>
</tr>
 
<tr>
    
<td class="tg-0lax" >
    <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" class="img-responsive" style="width:670px;height:600px"/>
</td>
      <td class="tg-0lax" colspan="4">
          <h3  style="background: darkblue;color:#fff;padding: 10px;text-align:center; font-size:66px; font-family:"mangal", serif;">
   <span>आधे घंटे में बिक गये कैदियों द्वारा बनाये गये मास्क जबलपुर मेंान
</span> 
</h3>
      </td>
      <td class="tg-0lax" >
          <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" class="img-responsive" style="width:670px;height:600px"/>
      </td>
  </tr>
  <tr>
    <td class="tg-0lax" style=" font-size:66px;margin: 5px;" colspan="2"><h2>When darkness overspreads my eyes</h2><br><p style="margin: 3px; font-family: "mangal", serif;">
       जबलपुर, कलेक्टर भरत यादव की पहल पर केन्द्रीय जेल के बंदियों द्वारा बनाये गये मास्क आज आधा घंटे के भीतर ही हाथों-हाथ बिक गये ।  स्वास्थ्य विभाग की मापदंडों के मुताबिक सूती कपड़े से बनाये गये ये मास्क कलेकटर कार्यालय के मार्गदर्शन कक्ष और कलेकट्रेट भवन में महिला स्व-सहायता समूहों के उत्पादों के प्रदर्शन के लिए स्थापित कृतिका आउटलेट पर विक्रय हेतु रखे गये थे । 

     आम नागरिकों को सात रूपये की दर पर उपलब्ध कराये जा रहे मास्क की बिक्री की शुरूआत कलेक्टर भरत यादव ने की । केन्द्रीय जेल के कैदियों के साथ-साथ वृद्धाश्रम के वृद्धजनों द्वारा तैयार किये गये मास्क भी कलेक्टर कार्यालय के मार्गदर्शन कक्ष में बिक्री हेतु रखे गये थे । संयुक्त संचालक सामाजिक न्याय के मुताबिक नागरिकों को मंगलवार को भी कलेक्टर कार्यालय के मार्गदर्शन कक्ष और कृतिका आउटलेट से सात रूपये की दर पर मास्क उपलब्ध कराये जायेंगे । 

आज से रियायती दर पर छोटे पैक में मिलेंगे हैंड सेनिटाइजर:

     जिला प्रशासन ने आम नागरिकों को कम कीमत पर हैण्ड सेनिटाइजर उपलब्ध कराने की पहल भी की है । हैण्ड सेनिटाइजर मंगलवार से रेडक्रॉस द्वारा संचालित वृद्धाश्रम तथा कलेक्ट्रेट स्थित “कृतिका” आउटलेट एवं कलेक्टर कार्यालय के मार्गदर्शन कक्ष से 30 रूपये की न्यूनतम कीमत पर आम नागरिकों को उपलब्ध कराये जायेंगे । 

     औद्योगिक क्षेत्र रिछाई स्थित हेलाईड केमिकल प्राइवेट लिमिटेड द्वारा निर्मित ये हैण्ड सेनिटाइजर 50 मिली लीटर के पैक में होंगे ।  इन्हें आसानी से पैंट या शर्ट के पॉकेट अथवा पर्स में रखा जा सकेगा । 
        
    </p></td>
    
    <td class="tg-0lax" style=" font-size:66px;"  colspan="2"><h2>When darkness overspreads my eyes</h2> <br>
    <p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "what is happened to me?" he thought. It was not a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
    
    <td class="tg-0lax" style=" font-size:66px;" colspan="2"><h2>When darkness overspreads my eyes</h2><br>
    <p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
   
  </tr>
  <tr>
    <td class="tg-0lax" colspan="3"><img src="https://www.think4unitynews.com/newportal2/uploads/blocks/block_5e5370d6828e8.jpeg" class="img-responsive" style="width:1000px;height:600px"/></td>
    <td class="tg-0lax" colspan="3"><h2>When darkness overspreads my eyes</h2><p><br>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
   
  </tr>
  <tr>
    <td class="tg-0lax" style=" font-size:66px;" colspan="2"><h2>When darkness overspreads my eyes</h2><br><p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
    
    <td class="tg-0lax" style=" font-size:66px;"  colspan="2"><h2>When darkness overspreads my eyes</h2><br><p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
    
    <td class="tg-0lax" style=" font-size:66px;" colspan="2"><h2>When darkness overspreads my eyes</h2><br><p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
   
  </tr>
  <tr>
    <td class="tg-0lax" colspan="2"><img src="https://www.think4unitynews.com/newportal2/uploads/blocks/block_5e5370d6828e8.jpeg" class="img-responsive" style="width:1070px;height:600px"/></td>
    <td class="tg-0lax" colspan="2"><h2>When darkness overspreads my eyes</h2><p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
   <td class="tg-0lax" colspan="2"><img src="https://www.think4unitynews.com/newportal2/uploads/images/image_750x_5ddc877dea79b.jpg" class="img-responsive" style="width:1070px;height:600px"/></td>
  </tr>
  
  <tr>
    <td class="tg-0lax" colspan="2"><img src="https://www.think4unitynews.com/newportal2/uploads/blocks/block_5e5370d6828e8.jpeg"  style="width:870px;height:900px"/></td>
    <td class="tg-0lax" colspan="2"><img src="https://www.think4unitynews.com/newportal2/uploads/images/image_750x_5d790ca6d8c72.jpg"  style="width:870px;height:900px"/></td>
   <td class="tg-0lax" colspan="2"><img src="https://www.think4unitynews.com/newportal2/uploads/images/image_750x_5ddc877dea79b.jpg"  style="width:870px;height:900px"/></td>
  </tr>
  <tr>
     <td class="tg-0lax" colspan="2"><h2>When darkness overspreads my eyes</h2><p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
    <td class="tg-0lax" colspan="2"><h2>When darkness overspreads my eyes</h2><p>
        His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. what is happened to me? His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather.
        
    </p></td>
 
  </tr>
  <tr>
    
<td class="tg-0lax" >
    <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" class="img-responsive"/>
</td>
      <td class="tg-0lax" colspan="4">
          <h3 style="background: darkblue;color:#fff;padding: 10px;text-align:center; font-size:66px;">
    इंजीनियर डी राधाकृष्णन जो चेयरमैन त्रिपुरा रेगुलेटरी कमिशन अगरतला का व्याख्यान

</h3>
      </td>
      <td class="tg-0lax" >
          <img src="https://i.pinimg.com/originals/b4/b9/aa/b4b9aa0ca93e9687a1d3b3ae1982cecc.jpg" class="img-responsive"/>
      </td>
  </tr>
  
  
  </tbody>
</table>

    <div class="head" style="text-align: center;
    position: relative;">
   

  
</div>
</div>
</div>
</div>
</div>
</div></body>
</html>


<?php
$html = ob_get_clean();
$dompdf = new DOMPDF(array('enable_remote' => true));
$dompdf->setPaper('A4', 'portrait');

$options = new Options();
$options->set('defaultFont', 'DejaVuSans');
//$html1 = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$dompdf->load_html($html);
$dompdf->render();
// $font = $dompdf->getFontMetrics()->get_font("serif", "bold");
// $dompdf->getCanvas()->page_text(72, 18, "                                        Testing", $font, 16, array(0.21,0.11,0.46));
//For view
$dompdf->stream("",array("Attachment" => false));
// for download
//$dompdf->stream("sample.pdf");
?> 

MPDF 代码

$html2 = mb_convert_encoding($html1, 'UTF-8', 'cp1252');
        $html3 = utf8_encode($html2);
        
        $html4=iconv('UTF-8', 'ISO-8859-1',  $html3);
        
        $html=mb_substr($html4, 0, 24090, "UTF-8");
        
        //$html=iconv('UTF-8', 'windows-1252', $html2);
        
        
       
        //this the the PDF filename that user will get to download
        $pdfFilePath ="download.pdf";
   
   


        //load mPDF library
        $this->load->library('m_pdf');
        set_time_limit(600);
        ini_set("memory_limit","256M");
        
        $this->m_pdf->pdf->allow_charset_conversion=true;
        
$this->m_pdf->pdf->charset_in='UTF-8';
$this->m_pdf->pdf->autoScriptToLang = true;
$this->m_pdf->pdf->autoLangToFont = true;



 // $this->m_pdf->pdf->autoLangToFont = true;
 $this->m_pdf->pdf->fonttrans['freeserif'] = 'dejavusanscondensed';
        //$this->m_pdf->pdf->SetWatermarkText('10 Rupees Only',0.1);
      
      $this->m_pdf->pdf->SetWatermarkText('T4U NEWSPAPER',0.15, '', array(10,40));

      $this->m_pdf->pdf->SetDisplayMode('fullpage');
      
$this->m_pdf->pdf->WriteHTML($stylesheet,1);
      
      // $this->m_pdf->pdf->showWatermarkImage = true;
        
      $this->m_pdf->pdf->showWatermarkText = true;
      ob_start();
     // $html = ob_get_contents();
ob_end_clean();
    $this->m_pdf->pdf->WriteHTML($html);
        
     
        //download it.
    //   $this->m_pdf->pdf->Output($pdfFilePath, "D");     
     $this->m_pdf->pdf->Output();  

两种情况下的 HTML 表格是相同的。

服务器php版本为7+

PDF 屏幕截图

请告诉我如何正确显示印地语字体

谢谢

【问题讨论】:

    标签: pdf fpdf dompdf mpdf


    【解决方案1】:
    font-family: freeserif; 
    

    这对于马拉地语和印地语的 mpdf 非常有用!!!

    【讨论】:

      【解决方案2】:

      对所有标签使用以下字体

      字体系列:freeserif;

      它将显示您的字体。 我已经在 mpdf 库中对其进行了测试,对我来说效果很好。

      【讨论】:

        猜你喜欢
        • 2019-07-15
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2020-03-05
        • 1970-01-01
        • 2018-06-12
        相关资源
        最近更新 更多