'Arabic Support for tFPDF (based on fpdf)
I am using tFPDF (linked below). I cannot find a way to render text right --> left for Arabic language. It renders fine but it is left to right. Below is the library I am using. I looked at source code and couldn't figure out how to render right to left.
Solution 1:[1]
Unfortunately it would be too hard to make the tFPDF library work for Arabic. This library only supports Arabic characters in their isolated form, instead of using their initial, medial or final form when they are in an initial, medial or final position in a word. See the document “Text Layout Requirements for the Arabic Script” by the W3C.
Furthermore it does not support true bidirectionality. Have a look at the document “Unicode Bidirectional Algorithm basics”, also by the W3C.
My suggestion is to use another library. The only non-commercial PHP library supporting RTL (right-to-left) scripts that I was able to find is TCPDF. The source code is on GitHub. It's based on the fpdf library just like tFPDF.
To try it out, all you have to do is
git clone https://github.com/tecnickcom/TCPDF.git
cd TCPDF/examples
# this is an example of an RTL script:
php example_018.php > example_018.pdf
open example_018.pdf
Starting from the example files I have put together the following example for Arabic (must be placed in the examples directory):
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Walter Tross');
$pdf->SetTitle('TCPDF Arabic Example');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'ar'; // 'ar' for Arabic
$lg['w_page'] = 'page';
$pdf->setLanguageArray($lg);
$pdf->SetFont('dejavusans', '', 12); // several fonts in TCPDF/fonts work
$pdf->AddPage();
$txt = <<<EOD
????? ?? ????
hello world
EOD;
$pdf->setRTL(true); // optional here, depends on the desired BASE direction
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0); // 'C' for centered
$pdf->Output('hello_world_in_Arabic.pdf', 'I');
This is what is rendered at the center top of the resulting PDF:

For some reason that I don't have the time to investigate now you have to redirect the output to a file, like with example_018.pdf above, but I'm sure this can be solved. (A quick hack would be to use output buffering.)
Instead of including tcpdf_include.php, which in turn includes all that is needed and much more, you will have to find a reasonable place for what you really need and a reasonable way to include only that.
And, of course, you will have to study a bit the examples in order to put together the code that works for you.
P.S.: I have taken the translation of "hello world" from the Wikipedia entry for the ??? programming language.
Solution 2:[2]
use this package to reshape your arabic string
import arabic_reshaper # pip install arabic_reshaper
expression = r"^[a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z]"
if (re.search(expression, yourstring) is None):
pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
pdf.set_font('DejaVu', '', 10)
arabic_string = arabic_reshaper.reshape(yourstring)
arabic_string = arabic_string[::-1]
w = pdf.get_string_width(arabic_string) + 6
pdf.cell(w, 9, arabic_string, 0, 1, 'L', 0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
