Replies: 8   Views: 4667
Using wordstyles to format replacevariablebyhtml output possible?
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 nicks  · 28-12-2015 - 21:37


Having issues with replaceVariableByHTML to replace a tag in a Word file, and trying to format this replaced HTML code from CKEditor to a customised Word Style (font2) that is already in the Word Template. It works fine with addText, though just doesn't work with replaceVariableByHTML.

Any idea what I am doing wrong?

Test code below:

require_once '../phpdocx_corporate-new/classes/';
$docx = new CreateDocxFromTemplate('template4.docx');
$styles = array('pStyle' => 'font2');
$docx->replaceVariableByHTML('PIC', 'block', '<p>This lack of <span style="color:#FF0000">detail</span> could mean a system does not <span style="background-color:#00FFFF">function correctly.</span></p>
', array('isFile' => false,'wordStyles' => $styles));
$docx->addText('This is some sample paragraph test', array('pStyle' => 'font2'));

Posted by admin  · 29-12-2015 - 07:22


You need to associate a class, id or tag to the style and use the strictWordStyles option is some to overwrite Word styles is needed.

We recommend you to check the examples available on ( Using native Word formatting with HTML). They use embedHtml but the same applies to replaceVariableByHTML.


Posted by nicks  · 30-12-2015 - 12:10

Thanks for the quick reply. I'll give that a go!

Posted by nicks  · 30-12-2015 - 19:39


I've experimented with this. I've found that it doesn't follow the applied formatting unless I add 

'strictWordStyles' => true

If I do this, then it stripes out the formatting such as text colouring though does change the font size. Is it meant to act like this?


Posted by admin  · 04-01-2016 - 09:29


You can't mix both Word and CSS styles using the same replace method. You need to do it using CSS styles or Word styles.

To mix both styles, you need to change the library, please check this post:

That explains how to do that.


Posted by nicks  · 04-01-2016 - 16:39

Thanks for the pointers. I had tried using standard CSS in the past with no luck, then read about the requirement of Tidy. Once installed that I could set a CSS for my output from CKEditor to ensure the font for <p> tags was set as I required with the following:

<style>p { font-size: 10pt; } </style>

Posted by nicks  · 12-01-2016 - 10:24

When using the replaceVariableByHTML on <p> tags I've found that the text within the generated Word document has 10pt spacing before and after on the formatted text.

Is there a way to remove this?

Posted by nicks  · 12-01-2016 - 17:51

Managed to solve this by specifying a margin and padding:


Posted by admin  · 13-01-2016 - 09:24


That's a good solution. Phpdocx adds default spacing for paragraphs, so you need to set it manually when you add the paragraph or the placeholder in the template.