首页 > 解决方案 > 孤立的 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">

我还尝试在数据库中插入相同的阿拉伯语字段,它们运行良好。

标签: phppdf-generationfpdf

解决方案


经过一段时间的尝试,我发现最好的解决方案是使用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 库的更多详细信息。


推荐阅读