Benjamin
Benjamin

Reputation: 196

Can't get the Euro symbol (€) to display in a phpword-generated docx

I can't get the Euro symbol (€) to display in a generated .docx. All I get is a square shape.

My first thought is it's an encoding issue, but there's no encoding specified anywhere in my script. I've specified charsets for HTML pages and in SQL databases for other projects (UTF8 would do it) but the implementation in question uses neither.

Another thing that occurs to me is that the docx gets created on the server, and is downloaded from there for printing. Could the server environment be to blame? I can't see any way to change encoding for the docx when it's online.

Ideally I'd like an in-document fix rather than getting the hosting company to tinker with settings.

This is the code:

$textrun->addText('If you wish to have a script remarked we will charge €35 for this.);

Upvotes: 0

Views: 3045

Answers (3)

kGrozdanovski
kGrozdanovski

Reputation: 48

Although this answer might come late, it might be of use to those who still encounter difficulties related to this issue.

A short and simple solution would be to use PHP's utf8_decode() function, which converts a string into UTF-8 format (PHP Documentation).

When passed a UTF-8 format string while assigning a value in the PHPWord Document, special characters including currency symbols, German characters with umlauts will be inserted properly in the document.

Example:

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('template.docx');

$document->setValue('variable', utf8_decode($value));

A special case scenario is when editing your script through Nano (CLI editor) in certain Linux distributions - if you have hardcoded any strings containing special characters they will be converted to another encoding upon saving the file.

Edit: Be cautious when using the htmlspecialchars function without a third parameter on legacy systems as the parameter's default value is different depending on the PHP version (PHP 5.4 Compatibility).

Upvotes: 1

sipti
sipti

Reputation: 21

phpword don't support special symbol like & or < or > etc.. for solve it i've used this:

$n = htmlspecialchars("your string with special chars");
$section->addText($n);

Upvotes: 0

Artur Kedzior
Artur Kedzior

Reputation: 4263

$fa_euro = html_entity_decode('&#xf153;', 0, 'UTF-8');
$section->addText(utf8($fa_euro));

Google is your friend: https://github.com/PHPOffice/PHPWord/issues/780

Also on the top of the script set this:

mb_internal_encoding("UTF-8");

Upvotes: 1

Related Questions