php - 孤立的 PHP FPDF 阿拉伯文文本
问题描述
我正在尝试生成阿拉伯语的 pdf 文档,我使用 fpdf 以英语格式生成相同的文档。在做了一些研究之后,我最终用 tfpdf 和 tcpdf 替换了 fpdf。后来弄乱了我的 pdf 布局,并且不能 100% 使用阿拉伯语单词。所以我选择了 tfpdf。
经过对 tfpdf 的更多研究,我最终得到了以下代码:
//to add custom font from unicode folder
$pdf->AddFont('Tajawal', '', 'Tajawal-Medium.ttf', true);
//to set the custom font
$pdf->SetFont('Tajawal','',18,true);
//helper function to rever the arabic string since I get the output reversed in the pdf
function utf8_strrev($str){
preg_match_all('/./us', $str, $ar);
return implode(array_reverse($ar[0]));
}
//$answer comes from a php and is an arabic string
$pdf->MultiCell(0 , 7, utf8_strrev($answer),1);
如图所示(来自生成的 pdf 的屏幕截图),阿拉伯字母是分开的/不相交的,但由于反向辅助功能,它们具有正确的顺序。我们如何在 tfpdf 中解决这个问题?
附加信息
我使用的字体是 Tajawal,它是一种阿拉伯 google 字体:https ://fonts.google.com/specimen/Tajawal?subset=arabic
我已将这些添加到我的 html 文档中:
ini_set('default_charset', 'UTF-8');
<html dir="rtl" lang="ar">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
我还尝试在数据库中插入相同的阿拉伯语字段,它们运行良好。
解决方案
经过一段时间的尝试,我发现最好的解决方案是使用TCPDF for laravel而不是 FPDF。你可以从这个小例子开始构建你的代码:
$html_content = '<h1 dir="rtl">يسبي</h1>';
PDF::SetTitle('Sample PDF');
PDF::SetFont('aealarabiya', '', 18);
PDF::AddPage();
PDF::writeHTML($html_content, true, false, true, false, '');
PDF::Output('SamplePDF.pdf');
您还可以查看此链接以获取有关用于生成阿拉伯 pdf 的 TCPDF 库的更多详细信息。
推荐阅读
- php - WooCommerce - 如果金额大于 x,则将每个运费设置为 0(我有 90% 的工作片段但无法修改)
- javascript - 有没有办法一次捕获多个相同类型的事件?
- ruby-on-rails - 与名称中最后一个字母 s 的 Leagcy 表连接
- c++ - 如何在遍历时添加要设置的元素?
- json - JSON.parse 从 Web api 返回 undefined
- xamarin - Xamarin 使用 Skia Sharp 形成 Dotted Circle
- javascript - 谷歌分析没有收集任何网站速度数据
- cplex - 使用 cplex OPL 对时空网络进行建模
- git - 除了最后一次提交删除之前的所有提交
- c# - 使用 Excel 互操作时忽略 Excel 中设置的日期格式