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?
I had to fix it in two place different than Nasers's way:
1- in Section.php addText function:
I did this:
2- in cell.php addText function
I did this:
now your word file will display unicode characters in right way. And then i had a problem in texts directions. i found the solution by using this code
u can see the two constants in the cell.php file
note that u can not apply other array combined styles like Paragraph before than 'textDirection' , because whose styles make 'textDirection' disabled.
Open PHPWord\Template.php
Change in setValue function (line no 89.) as below.
Change $replace = utf8_encode($replace); to $replace = $replace;
Find
In Writer/Word2007/Base.php
replace with
Also, make sure you don't use any styles to make it work, or else you will have to repeat this step in every function you use.
Please find the following points to write all types of utf-8 right to left data insertion in phpword template.
In
setValue
function (line #95) in Template.php please comment the following portion of codeIf 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.//==== here is a working example of how the word data can be write inside the word template //--- load phpword libraries ----
//need how design can change the looking. colr #E4EDF9
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
With
Then, if you do
On the project root, you will find all lines where
utf8_encode
is used. You will see lines likeYou 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 dofrom_iso88591_to_utf8
andfrom_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:
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.