3

I'm working on an assignment where I need to capture the content generated on html and send it to word document so that it could be sent further. I have captured the content and sent it to php using json object and generated the word document as well, however when html has special characters, it causes the word document to be corrupted. I tried using htmlspecialchars in addText function, but it didn't help.

Here is my php code

\PhpOffice\PhpWord\Autoloader::register();
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$jsonData2 =json_decode($_POST['outputData'],true);
foreach ($jsonData2 as $key => $value) {
        $section-> addTextBreak();
        $section->addText($key, array('size' => 16, 'bold' => true, 'color' => '#3b5999'));
        $section-> addTextBreak(2);
        foreach ($value as $key => $value) {
        $section->addText($value, array('size' => 11));
        $section-> addTextBreak();  
    }
}

can someone how to capture the special characters and display it to the word doc so that it doesn't cause file to be corrupted

user4943236
  • 5,914
  • 11
  • 27
  • 40

2 Answers2

4

Writing documents of some formats, especially XML-based, requires correct output escaping. Without it your document may become broken when you put special characters like ampersand, quotes, and others in it.

Escaping can be performed in two ways: outside of the library by a software developer and inside of the library by built-in mechanism.

By default, the built-in mechanism is disabled for backward compatibility with versions prior to v0.13.0. To turn it on set outputEscapingEnabled option to true in your PHPWord configuration file

or

Use the following instruction at runtime:

\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
Madhu Jayarama
  • 415
  • 1
  • 5
  • 15
0

Try putting the Content-Type header at the top of the file:

header('Content-type: text/html; charset=utf-8');

Similar question How to set UTF-8 encoding for a PHP file

Community
  • 1
  • 1
serialworm
  • 761
  • 3
  • 13
  • this is not working.. the content sent thro json object is in correct format, but php word is not able to display it correclty – user4943236 Nov 17 '15 at 08:20