Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP word doc: capturing special characters

Tags:

json

php

phpword

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

like image 492
user4943236 Avatar asked Oct 26 '25 11:10

user4943236


1 Answers

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);
like image 141
Madhu Jayarama Avatar answered Oct 29 '25 00:10

Madhu Jayarama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!