Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPWord: Creating an Arabic right to left word document

I'm trying to use PHPWord to create a word document that will include dynamic data pulled out from a MySQL database. The database has MySQL charset: UTF-8 Unicode (utf8) MySQL connection collation: utf8_unicode_ci and so does the table fields.

Data is stored and previewed fine in HTML, however when creating the document with the arabic variables, the output in Word looks like أحÙد Ùبار٠اÙÙرÙ.

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('templates/.../wtvr.docx');
$document->setValue('name', $name);
$document->setValue('overall_percent_100', $overall_percent_100);
$document->save('Individual Report - ' . $name . '.docx');

Is there anyway to fix that?

like image 610
TechMafioso Avatar asked Feb 18 '23 03:02

TechMafioso


2 Answers

Well, yes. But you must unfortunately modify the library. The author of the library uses utf8_encode/utf8_decode obviously without understanding what they do at all.

On line 150, of Shared/String.php:

Replace

public static function IsUTF8($value = '') {
    return utf8_encode(utf8_decode($value)) === $value;
}

With

public static function IsUTF8($value = '') {
    return mb_check_encoding($value, "UTF-8");
}

Then, if you do

$ grep -rn "utf8_encode" .

On the project root, you will find all lines where utf8_encode is used. You will see lines like

$linkSrc = utf8_encode($linkSrc); //$linkSrc = $linkSrc;

$givenText = utf8_encode($text); //$givenText = $text;

You can simply remove the utf8_encode as shown in the comments.

Why is utf8_encode/utf8_decode wrong? First of all, because that's not what they do. They do from_iso88591_to_utf8 and from_utf8_to_iso88591. Secondly, ISO-8859-1 is almost never used, and usually when someone claims they use it, they are actually using Windows-1252. ISO-8859-1 is a very tiny character set, not even capable of encoding , let alone arabic letters.

You can do fast reviews of a library by doing:

$ grep -rn "utf8_\(en\|de\)code" .

If you get matches, you should move on and look for some other library. These functions simply do the wrong thing every time, and even if someone needed some edge case to use these functions, it's far better to be explicit about it when you really need ISO-8859-1, because you normally never do.

like image 146
Esailija Avatar answered Feb 20 '23 16:02

Esailija


  • Please find the following points to write all types of utf-8 right to left data insertion in phpword template.

    1. In setValue function (line #95) in Template.php please comment the following portion of code

      //if(!is_array($replace)) {
      //    $replace = utf8_encode($replace);
      //}
      
    2. If you have problem with right to left which in some language the text mix up with left to right text add the following code in the same setValue function.

      $replace = "<w:rPr><w:rtl/></w:rPr>".$replace; 
      

//==== here is a working example of how the word data can be write inside the word template //--- load phpword libraries ----

    $this->load->library("phpword/PHPWord");
    $PHPWord  = new PHPWord();
    $document = $PHPWord->loadTemplate('./forms/data.docx');

    $document->setValue('NAME', 'شراف الدين');
    $document->setValue('SURNAME', 'مشرف');
    $document->setValue('FNAME', 'ظهرالدين');
    $document->setValue('MYVALUE', '15 / سنبله / 1363');
    $document->setValue('PROVINCE', 'سمنگان');
    $document->setValue('DNAME', 'عبدالله');
    $document->setValue('DMOBILE', '0775060701');   
    $document->setValue('BOX','<w:sym w:font="Wingdings" w:char="F06F"/>');
    $document->setValue('NO','<w:sym w:font="Wingdings" w:char="F06F"/>');
    //$document->setValue('BOX2','<w:sectPr w:rsidR="00000000"><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/>');
    $document->setValue('YES','<w:sym w:font="Wingdings" w:char="F0FE"/>');

    $document->setValue('CLASS1','<w:sym w:font="Wingdings" w:char="F06F"/>');
    $document->setValue('CLASS2','<w:sym w:font="Wingdings" w:char="F0FE"/>');
    $document->setValue('DNAME','يما شاه رخي');
    $document->setValue('TEL','0799852369');
    $document->setValue('ENTITY','مشاور حقوقي و نهادي');
    $document->setValue('ENTITY','مشاور حقوقي و نهادي');
    $document->setValue('REMARKS','در مسابقات سال 2012 میلادی در میدان Judo   بر علاوه به تعداد  39 نفر در تاریخ 4/میزان/ سال 1391 قرار ذیل اند.');

    $file = "./forms/data2.docx";
    $document->save($file);
    header("Cache-Control: public");     
    header("Content-Description: File Transfer");     
    header("Content-Disposition: attachment; filename=data2.docx");     
    header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");     
    header("Content-Transfer-Encoding: binary");         
    ob_clean();
    flush();
    readfile($file);

//need how design can change the looking. colr #E4EDF9enter image description here

like image 40
Naser Gulzade Avatar answered Feb 20 '23 17:02

Naser Gulzade