Forum


Replies: 1   Views: 1070
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

Hi,

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'
);
$docx->replaceVariableByText($variables);

// 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>';
$docx->replaceVariableByHTML(
    'html', 
    'block',
    $html
);


// Save
$docx->createDocx('hello_world');

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

Any help ?

Thanks.

 

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

Hello,

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 (https://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document), 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 https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP (Replace placeholders keeping styles section).

Regards.