Replies: 1   Views: 1096
Createfromtemplate, replacevariablebyhtml : apply template style
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 xiii29  · 10-01-2021 - 14:24


FYI : I'm using the trial version of PhpDocX.

I would like to use a template in which I would like to replace content of some variables by HTML. It's working but I can't manage that content coming from HTML respect the original template style.

Here is an example :

$docx = new CreateDocxFromTemplate('template.docx');

// Template
$variables = array(
    'title' => 'Replace title',
    'txt' => 'Replace text'

// Html
$html = '<h1> My Main Title </h1> <h2> Sub Title </h2> <h3> Sub sub title </h3> <p> Some text </p> <ul><li> Item 1 </li><li> Item 2 </li><li> Item 3 </li></ul>';

// Save

As a result, the title have a style but the one from the original doc ...

Any help ?



Posted by admin  · 10-01-2021 - 14:33


When HTML is transformed to DOCX, default styles are applied that you can change using custom styles (CSS styles, MS Word styles).

To preserve the styles of a placeholder when replacing HTML in a template you need to use the stylesReplacementType option (, but it's only available in HTML Extended for Premium licenses, the trial package doesn't include it. Using a not Premium license you need to set the styles to be applied in the HTML you are adding to the template. You can read this same information on (Replace placeholders keeping styles section).