Skip to content

Commit 1320b9d

Browse files
author
Nikita Chubukov
committed
MAGETWO-95816: Invoice PDF contain different address when use arabic symbols
- Reverse text with Arabic characters
1 parent c515b40 commit 1320b9d

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,38 @@ protected function _calcAddressHeight($address)
363363
return $y;
364364
}
365365

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+
366398
/**
367399
* Insert order to pdf page
368400
*
@@ -474,7 +506,7 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
474506
if ($value !== '') {
475507
$text = [];
476508
foreach ($this->string->split($value, 45, true, true) as $_value) {
477-
$text[] = $_value;
509+
$text[] = ($this->isArabic($_value)) ? $this->reverseArabicText($_value) : $_value;
478510
}
479511
foreach ($text as $part) {
480512
$page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8');
@@ -491,7 +523,7 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
491523
if ($value !== '') {
492524
$text = [];
493525
foreach ($this->string->split($value, 45, true, true) as $_value) {
494-
$text[] = $_value;
526+
$text[] = ($this->isArabic($_value)) ? $this->reverseArabicText($_value) : $_value;
495527
}
496528
foreach ($text as $part) {
497529
$page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8');

0 commit comments

Comments
 (0)