Replies: 5   Views: 1135
Extra space printed before the replaced variable with "replacevariablebyhtml"
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by Bouillou  · 01-10-2020 - 14:54

Dear support,

With version 9, replacing variable with "replaceVariableByHTML" displays an extra space before the text.

You can see the problem with this simple code :


require_once '../../../Classes/Phpdocx/Create/CreateDocx.php';

$docx = new Phpdocx\Create\CreateDocxFromTemplate('../../files/TemplateHTML.docx');

$docx->replaceVariableByHTML('CHUNK_1', 'block', "<- Should not have a space before thhis text<br />End of the text");
$docx->replaceVariableByHTML('CHUNK_2', 'inline', "<- Should not have a space before thhis text<br />End of the text");


Do you know how to fix this?

Best regards

Posted by admin  · 01-10-2020 - 14:59


It's no a phpdocx problem but from some PHP Tidy versions that wrap contents (that ommits the wrap option sets as 0 to avoid wrapping), if we run your code on our test servers we get the correct output without the extra blank spaces. You can change Tidy settings in the INI settings file (, or use the removeLineBreaks option with HTML methods or edit DOMPDF_lib.php file and replace the following line:

'wrap' => 0,

by a very high value, for example:

'wrap' => 99999999999999,

The next release of phpdocx will include the previous change to avoid that issue from PHP Tidy.


Posted by Bouillou  · 01-10-2020 - 15:29

Thanks for your answer.

So, if I understand well your answer, this code should work isn't it?

$docx->replaceVariableByHTML('CHUNK_1', 'block', "<- Should not have a space before thhis text<br />End of the text", array('removeLineBreaks' => true));

Because it is not :o(

P.S. : I don't have access to the TIDY settings

Posted by admin  · 01-10-2020 - 15:39


Depending on the Tidy setup you may need to set removeLineBreaks to true or edit DOMPDF_lib.php to apply the change explained in our previous reply:


'wrap' => 0,

by a very high value, for example:

'wrap' => 99999999999999,

You can find DOMPDF_lib in the classes folder of phpdocx (Classes/Phpdocx/Libs folder in the namespace package).

All users with the same issue have solved it applying one of the previous fixes. If ater testing both you don't solve the issue, please send the DOCX output to contact[at] with the PHP version you are using and we'll check it it's the same or other issue.


Posted by Bouillou  · 02-10-2020 - 11:09

OK, I have just wrapped the text with a <span> tag and the render is fine.

Thanks for your support, removeLineBreaks description point me the right direction to fix the issue.

Posted by admin  · 02-10-2020 - 11:29


OK, now we understand the exact issue. It wasn't a Tidy issue, but the content that was being added (we misunderstood the code). Replacing and adding HTML require using at least one tag to wrap the content; to use plain strings without HTML we recommend using replaceVariableByText.