Dompdf中古吉拉特语和印地语文本显示不正常。

huangapple go评论47阅读模式
英文:

Dompdf Gujarati and Hindi text not showing properly

问题

在我的Laravel项目中,我正在使用DOMPDF从HTML Blade文件生成PDF。我想根据语言选择显示印地语或古吉拉特语的文本。以下是我的控制器代码:

$pdf=PDF::loadView('my-htmlt.pdffile',['header'=>$header,'data'=>$data,'footer'=>$footer]);        
$name=$header->name."_".date('YmdHis');

if(isset($view) && $view!=''){
    return $pdf->stream($name.'.pdf');
} else {
    return $pdf->download($name.'.pdf');
}

这是Blade文件中的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{$header->name}}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<style type="text/css">
    @page{
        padding: 15px;
    }
    <?php if($header->lang== 'Guj'){ ?>    
    * {
        font-family: 'noto serif gujarati', sans-serif;
    }
    <?php } else if($header->lang == 'Hindi'){ ?>
    * { 
        font-family: 'tiro devanagari hindi', sans-serif; 
    }
    <?php } ?>
</style>
<body>{{从数据库中获取的内容,可能是印地语或古吉拉特语}}</body>
</html>

我已经下载了Noto Serif Gujarati和Tiro Devanagari的ttf字体文件,并将其复制到项目文件夹下的storage/fonts文件夹中。我还尝试过使用带有ttf文件路径的font-face CSS。但结果是相同的,因此我决定使用更简洁的代码。

我在数据库中设置了utf8_unicode_ci的排序规则和字符集,并可以正确从数据库中获取文本,也测试过打印HTML视图。

现在的问题是文本以印地语或古吉拉特语显示,但并未按照我的要求正确显示。例如,我想要显示类似于“ચિત્ર જોઈ તેનો પહેલો મૂળાક્ષર શોધો”的文本。但显示结果却是这样的:
Dompdf中古吉拉特语和印地语文本显示不正常。

同样,在印地语中,我想要显示类似于“कौन सा जीव धीमी गति से चलता है?”的文本。但显示结果却是这样的:
Dompdf中古吉拉特语和印地语文本显示不正常。

有人能提供一个解决方案,以在PDF中显示正确的文本吗?

英文:

In my laravel project I am using DOMPDF to generate pdf from html blade file. I want to show text in Hindi or Gujarati as per language selection. Here is my code of controller.

 $pdf=PDF::loadView(&#39;my-htmlt.pdffile&#39;,[&#39;header&#39;=&gt;$header,&#39;data&#39;=&gt;$data,&#39;footer&#39;=&gt;$footer]);        
    $name=$header-&gt;name.&quot;_&quot;.date(&#39;YmdHis&#39;);

    if(isset($view) &amp;&amp; $view!=&#39;&#39;){
        return $pdf-&gt;stream($name.&#39;.pdf&#39;);
    } else {
        return $pdf-&gt;download($name.&#39;.pdf&#39;);
    }

Here is the code in blade file.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;title&gt;{{$header-&gt;name}}&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;/head&gt;

&lt;style type=&quot;text/css&quot;&gt;
    @page{
	    padding: 15px;
    }
    &lt;?php if($header-&gt;lang== &#39;Guj&#39;){ ?&gt;	
    * {
	    font-family: &#39;noto serif gujarati&#39;, sans-serif;
      }
    &lt;?php } else if($header-&gt;lang == &#39;Hindi&#39;){ ?&gt;
    * { 
	    font-family: &#39;tiro devanagari hindi&#39;, sans-serif; 
      }
    &lt;?php } ?&gt;
   &lt;/style&gt;
   &lt;body&gt;{{Content fetched from database either in Hindi or Gujarati}}&lt;/body&gt;
&lt;/html&gt;

I have downloaded ttf font files for Noto Serif Gujarati and Tiro Devnagari and copy under projectfolder/storage/fonts Folder. I have also tried using face font css with file path of ttf file. But result is same so, working with less code.
I have set collation and charset as utf8_unicode_ci in database and getting text properly from database, tested printing html view.
Now issue is that text is showing in Hindi or gujarati but text is not showing properly as per my requirement.
For example I want to show text like "ચિત્ર જોઈ તેનો પહેલો મૂળાક્ષર શોધો". But it is showing like this
Dompdf中古吉拉特语和印地语文本显示不正常。

Same way in hindi I want to show text like "कौन सा जीव धीमी गति से चलता है ?" But it is showing like this Dompdf中古吉拉特语和印地语文本显示不正常。

Can anyone have solution to show proper text in PDF?

答案1

得分: 0

经过多次努力后,我选择了另一种选项。我正在使用以下代码呈现我的视图:

return view('my-html.pdffile', ['header' => $header, 'data' => $data, 'footer' => $footer]);

在我的 Blade 文件 "my-html.pdffile" 中,我允许用户使用以下 window.print 脚本下载 PDF 文件:

<script>
	var is_chrome = function () { return Boolean(window.chrome); }
	if(is_chrome){
		window.print();
		//  setTimeout(function(){window.close();}, 10000);
		//  给他们10秒钟来打印,然后关闭
	}else{
		window.print();
	}
</script>

现在,我可以根据我的需求下载 PDF 文件,而无需使用任何字体或下载任何字体库。

英文:

So, After putting lot of efforts I have taken another option. I am rendering my view with

return view(&#39;my-html.pdffile&#39;,[&#39;header&#39;=&gt;$header,&#39;data&#39;=&gt;$data,&#39;footer&#39;=&gt;$footer]);

And in my-html.pdffile Blade file I am allowing user to download pdf with window.print script like this.

&lt;script&gt;
var is_chrome = function () { return Boolean(window.chrome); }
if(is_chrome){
	window.print();
	//  setTimeout(function(){window.close();}, 10000);
	//  give them 10 seconds to print, then close
}else{
	window.print();
}
&lt;/script&gt;

Now I can download pdf as per my requirement with all the languages without using any fonts or downloading any font libraries.

huangapple
  • 本文由 发表于 2023年5月26日 13:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定