@@ -363,6 +363,38 @@ protected function _calcAddressHeight($address)
363
363
return $ y ;
364
364
}
365
365
366
+ /**
367
+ * Detect an input string is Arabic
368
+ *
369
+ * @param string $subject
370
+ * @return bool
371
+ */
372
+ private function isArabic (string $ subject ): bool
373
+ {
374
+ return (preg_match ('/\p{Arabic}/u ' , $ subject ) > 0 );
375
+ }
376
+
377
+ /**
378
+ * Reverse text with Arabic characters
379
+ *
380
+ * @param string $string
381
+ * @return string
382
+ */
383
+ private function reverseArabicText ($ string )
384
+ {
385
+ $ splitText = explode (' ' , $ string );
386
+ for ($ i = 0 ; $ i < count ($ splitText ); $ i ++) {
387
+ if ($ this ->isArabic ($ splitText [$ i ])) {
388
+ for ($ j = $ i + 1 ; $ j < count ($ splitText ); $ j ++) {
389
+ $ tmp = $ this ->string ->strrev ($ splitText [$ j ]);
390
+ $ splitText [$ j ] = $ this ->string ->strrev ($ splitText [$ i ]);
391
+ $ splitText [$ i ] = $ tmp ;
392
+ }
393
+ }
394
+ }
395
+ return implode (' ' , $ splitText );
396
+ }
397
+
366
398
/**
367
399
* Insert order to pdf page
368
400
*
@@ -474,7 +506,7 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
474
506
if ($ value !== '' ) {
475
507
$ text = [];
476
508
foreach ($ this ->string ->split ($ value , 45 , true , true ) as $ _value ) {
477
- $ text [] = $ _value ;
509
+ $ text [] = ( $ this -> isArabic ( $ _value )) ? $ this -> reverseArabicText ( $ _value ) : $ _value ;
478
510
}
479
511
foreach ($ text as $ part ) {
480
512
$ page ->drawText (strip_tags (ltrim ($ part )), 35 , $ this ->y , 'UTF-8 ' );
@@ -491,7 +523,7 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
491
523
if ($ value !== '' ) {
492
524
$ text = [];
493
525
foreach ($ this ->string ->split ($ value , 45 , true , true ) as $ _value ) {
494
- $ text [] = $ _value ;
526
+ $ text [] = ( $ this -> isArabic ( $ _value )) ? $ this -> reverseArabicText ( $ _value ) : $ _value ;
495
527
}
496
528
foreach ($ text as $ part ) {
497
529
$ page ->drawText (strip_tags (ltrim ($ part )), 285 , $ this ->y , 'UTF-8 ' );
0 commit comments