0
votes

In my project, I want to extract a certain page into a PDF file. the page is mostly in the Arabic language.

I am using barryvdh/laravel-dompdf On the first try, there were no characters showing, only question marks instead.

After digging around, I found that I should add the following CSS to my blade page:

* { 
    font-family: DejaVu Sans, sans-serif; 
    direction: rtl;
}

now the Arabic letters are showing in the PDF but their order is inverted (ABC is CBA). after another round of research, I found a solution somewhere that I should add the following into my dompdf/dompdf/src/Renderer/Text.php right before

if (strtolower($style->direction) == 'rtl') {
    preg_match_all('/./us', $text, $ar);
    $text = join('',array_reverse($ar[0]));
}

// this is added here to know where exactly i put this code
$this->_canvas->text($x, $y, $text

this works and inverts back the text as it should be, but now the letters are separated... (Arabic letters belonging to the same word are connected)

ex: البناء is showing as ‫ا ل ب ن ا ء

this is a known issue for this library that is still not resolved. one of the commentators suggested using the composer library chehivskiy/i18n-arabic‬ and use it as new \I18N_Arabic('Glyphs')

But I keep getting a class not found error. this library does not have any namespaces.

can anyone help me to either find a library that works to transform Arabic HTML to PDF, or maybe try to find a way to fix this library for it to work, or perhaps suggest a way to use a composer non-Laravel library such as chehivskiy/i18n-arabic‬ in a Laravel project.

Thank you so much.

1

1 Answers

0
votes

Alright so what I did, I removed the composer library chehivskiy/i18n-arabic‬ and downloaded ar-php which is kind of the same but not throught composer.

I put the extracted I18N in the app/Libraries directory that I created.

Next i changed the name of Arabic.php into I18N_Arabic.php to be the same as the class name inside of it. Note that there is another class in that file ArabicException which i put in its own file.

I also added a namespace in that file namespace App\Libraries\I18N;

Next, since i'm using the Glyph class in app\Libraries\I18N\Arabic\Glyphs.php, i also did the same thing for it, changed its file name into I18N_Arabic_Glyphs to match the classname and added namespace namespace App\Libraries\I18N\Arabic;.

Next back in my I18N_Arabic class, replace the following:

$this->myClass = '\I18N_Arabic_' . $library;
$class         = '\I18N_Arabic_' . $library;

to

$this->myClass = 'App\Libraries\I18N\Arabic\I18N_Arabic_' . $library;
$class         = 'App\Libraries\I18N\Arabic\I18N_Arabic_' . $library;

and remove the following:

if (!$this->_useAutoload) {
    include self::getClassFile($this->myFile); 
}

Lastly, in the vendor\dompdf\dompdf\src\Renderer\Text.php, search for $this->_canvas->text($x, $y, $text, and put the following right before it:

$Arabic = new \App\Libraries\I18N\I18N_Arabic('Glyphs');
$text = $Arabic->utf8Glyphs($text);

and that's it. Now using the PDF class provided in the packages above will generate a pdf in Arabic with correct format.

Note that here i am using the Glyph class, if you are using another class from this library, you need to do all the above for the class as well