Replies: 3   Views: 1819
How can i change the font of a template?
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 jeremie  · 18-03-2020 - 13:38

I have a template that uses only one font, Helvetica.

Can I change the template font with the API?

I'm not talking about setting the default font. I'm talking about changing the font of the text written in the template.

Posted by jeremie  · 23-03-2020 - 10:47

OK it works with Premium Edition.

One thing that doesn't work though is 'type' => '*' in order to apply the new font to all the document.


The following doesn't work:

$docx->customizeWordContent(['target' => 'document', 'type' => '*', 'contains' => '',], ['font' => 'Calibri']);

So I have to loop throught all possible types, like this:

foreach (array('break', 'image', 'list', 'paragraph', 'run', 'section', 'style', 'table', 'table-row', 'table-cell') as $type)
        $docx->customizeWordContent(['target' => 'document', 'type' => $type, 'contains' => '',], ['font' => 'Calibri']);


Posted by admin  · 23-03-2020 - 11:24


* wildcard doesn't apply to internal contents, so we recommend you to use your second approach but applying it only to content types that support font styles:

foreach (array('paragraph', 'run', 'list', 'style') as $type) {
        $docx->customizeWordContent(['target' => 'document', 'type' => $type, 'contains' => '',], ['font' => 'Calibri']);

On you can read the supported styles for each content type.