Search code examples
phpdompdf

Dompdf Arabic Support


I am using Dompdf to convert html to pdf.

Here's the code:

require_once 'dompdf/autoload.inc.php';
$tmp = sys_get_temp_dir();

$dompdf = new Dompdf([
   'logOutputFile' => '',
    // authorize DomPdf to download fonts and other Internet assets
    'isRemoteEnabled' => true,
    // all directories must exist and not end with /
    'fontDir' => $tmp,
    'fontCache' => $tmp,
    'tempDir' => $tmp,
    'chroot' => $tmp,
 ]);

$html = // Html code here

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("fitphase",array("Attachment"=>0));

I have used the following font:

<style>
* { 
   font-family: DejaVu Sans, sans-serif;
}
</style>

Now, issue is that the text displayed in arabic is showing from left to right and I want the direction to be from right to left.

I have found this link https://github.com/learn05/arabiclang/ for Laravel but need the solution with php.


Solution

  • Found this link arabic fonts display in reverse order in dompdf

    Add the below code in the vendor/dompdf/dompdf/Renderer/Text.php

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

    Put above this line in Text.php

    $this->_canvas->text($x, $y, $text,$font, $size,$style->color, $word_spacing, $letter_spacing);

    Add the style where one wants to see the result

    p {
    direction: rtl;
    text-align: right;
    }