英文:
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视图。
现在的问题是文本以印地语或古吉拉特语显示,但并未按照我的要求正确显示。例如,我想要显示类似于“ચિત્ર જોઈ તેનો પહેલો મૂળાક્ષર શોધો”的文本。但显示结果却是这样的:
同样,在印地语中,我想要显示类似于“कौन सा जीव धीमी गति से चलता है?”的文本。但显示结果却是这样的:
有人能提供一个解决方案,以在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('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');
}
Here is the code in blade file.
<!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>{{Content fetched from database either in Hindi or Gujarati}}</body>
</html>
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
Same way in hindi I want to show text like "कौन सा जीव धीमी गति से चलता है ?" But it is showing like this
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('my-html.pdffile',['header'=>$header,'data'=>$data,'footer'=>$footer]);
And in my-html.pdffile Blade file I am allowing user to download pdf with window.print script like this.
<script>
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();
}
</script>
Now I can download pdf as per my requirement with all the languages without using any fonts or downloading any font libraries.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论