I am using dompdf to convert a html page using dompdf, but it is showing arabic text in reverse order
For eg if text is
ايبنسيالرمس
then it is displaying as
مرلايسنبيا in PDF
Any idea why?
I am using dompdf to convert a html page using dompdf, but it is showing arabic text in reverse order
For eg if text is
ايبنسيالرمس
then it is displaying as
مرلايسنبيا in PDF
Any idea why?
dompdf does not currently support directionality, so RTL languages will not display correctly in regard to character flow. There is a hack for displaying characters in the correct order, though it does require modification of the dompdf code.
If you would like to try the modification two steps are required. First, style any text that should display RTL with direction: rtl; text-align: right;
. Then, in the file dompdf/include/text_renderer.cls.php add the following lines before each instance of $canvas->text()
(or any variant, such as $this->_canvas->text()
):
if (strtolower($style->direction) == 'rtl') {
preg_match_all('/./us', $text, $ar);
$text = join('',array_reverse($ar[0]));
}
(You may have to change the name of the $text
variable to match what's used in the code.)
References:
Additionally we've seen an issues where characters don't join together as expected when rendering words. This is an issue we haven't had a chance to explore yet.
References:
Your best option right now for full support of directionality is to use a headless browser, e.g. PhantomJS.
Only issue with @BrianS answer is that left-to-right characters in the line are displayed right-to-left. This is how I solved it (in my case the check is for Hebrew characters):
// check if the line contains Hebrew characters from the start too
// to avoid flipping dates etc.
if( strtolower( $style -> direction ) == 'rtl' && preg_match( "/\p{Hebrew}/u", $text ) ):
preg_match_all('/./us', $text, $ar);
// reverse the whole line
$text = join('',array_reverse($ar[0]));
// flip english back to ltr
$words = explode( ' ', $text );
foreach( $words as $i => $word ):
if( !preg_match( "/\p{Hebrew}/u", $word ) ):
$words[$i] = implode( '', array_reverse( str_split( $word ) ) );
endif;
endforeach;
$text = implode( ' ', $words );
endif;