Forum


Replies: 3   Views: 939
Can you add font stylings on template variable replacements?
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 daveiroquois  · 23-03-2022 - 18:29

We have a Word template that works fine with simple text replacement, but we need it to be more robust. We have multiple templates but in certain circumstances, the replaced variables need to be "styled" with different font settings while in others they need to be "clean" with no font changes, so we can't put the styling on the actual text in the template. Is this possible with your tool?

We have a Premium License and are using LibreOffice for our Word Conversions, if that helps at all. 

Posted by admin  · 23-03-2022 - 18:56

Hello,

Yes, you can replace placeholders by a new text that includes styles. Even by other contents such as images, tables, charts... or multiple contents.

You need to use WordFragments and the replaceVariableByWordFragment method. On the previous page and the package you can find samples that detail how to use this method. Please also check the following page availabler in the Practical guide about working with WordFragments : https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml

The following sample replaces a text placeholder by a new text content with styles (bold, color and fontSize):

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

$textFragment = new WordFragment($docx, 'document');
$textStyles = array(
    'bold' => true,
    'color' => 'B70000',
    'fontSize' => 24,
);
$textFragment->addText('new text', $textStyles);

$docx->replaceVariableByWordFragment(array('WORDFRAGMENT' => $textFragment), array('type' => 'inline'));

$docx->createDocx('output');

WordFragment is a very powerful class included in phpdocx to add contents.

HTML could also be used to replace a placeholder using replaceVariableByHTML, but please note that transforming HTML adds default styles.

Please note the license you are using includes support tickets. If needed, you can open a ticket on MY PHPDOCX page after log in with the user that purchased the license, attaching a DOCX template you are using and we'll generate a custom sample using it.

Regards.

Posted by daveiroquois  · 24-03-2022 - 18:06

Thanks! The code you gave for the document works fine, but not for the header for some reason. 

I looked at the documentation here: https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml

$textStyles = array(
        'bold' => true,
        'color' => 'B70000'
                        );
foreach ($templateValues as $key => $value) {
        $textFragment = new WordFragment($docx, 'document');
        $textFragment->addText($value, $textStyles);
        $docx->replaceVariableByWordFragment(array($key => $textFragment), array('type' => 'inline'));
        }
$textFragment1 = new WordFragment($docx, 'firstHeader');
$textFragment1->addText('[Contract #]', $textStyles);
$docx->replaceVariableByWordFragment(array('Contract'=> $textFragment1), array('type' => 'inline'));

No errors are thrown but the header text is not replaced. Also using 'defaultHeader' doesn't work either. Please advise, thanks!

Posted by admin  · 24-03-2022 - 18:24

Hello,

By default the replacements are done in the document (body) target. If you want to replace placeholders in other targets you need to set the target option available in template methods.
In the samples included in the examples/Templates/replaceVariableByWordFragment folder of all packages you can find sample codes that use this option. For example, from sample_5.php:

// replace the text variable in headers
$docx->replaceVariableByWordFragment(array('VAR_HEADER_1' => $imageHeader, 'VAR_HEADER_2' => $textHeader, 'VAR_HEADER_3' => $textOther), array('type' => 'inline', 'target' => 'header'));

// replace the text variable in the document
$docx->replaceVariableByWordFragment(array('VAR_BODY_1' => $textBody1, 'VAR_BODY_2' => $textBody2, 'VAR_BODY_3' => $imageBody));

// replace the text variable in footers
$docx->replaceVariableByWordFragment(array('VAR_FOOTER_1' => $textFooter, 'VAR_FOOTER_2' => $imageFooter), array('type' => 'inline', 'target' => 'footer'));

You can read about this option on:

target    string  Possible values are: document (default), header, footer, footnote, endnote, comment. This option sets the scope of the replacement procedure.
By default, methods that replace placeholders in templates like replaceVariableByText or replacePlaceholderImage substitute the existing placeholders in the body of the document with new contents.

If you need to replace placeholders in other targets, use the target option, available in all methods. This way, it is possible to exchange placeholders in any target: document, header, footer, footnote, endnote and comment.
Template methods replace placeholders in the main document body. To replace placeholders in other scopes such as header, footer, comment or footnote, the target option is available.

If you want to replace the placeholders in all targets you can iterate all targets. The previous cookbook page details the following code using replaceVariableByText, but you can use other template methods:

foreach (array('document', 'header', 'footer', 'footnote', 'endnote', 'comment') as $target) {
  $docx->replaceVariableByText($variables, array('target' => $target));
}

Regards.