Forum


Replies: 2   Views: 4030
Replace image in a template table
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 Simplexe  · 26-07-2016 - 12:59

hello,

I use CreateDocxFromTemplate() class
I have a table with 3 columns in this template and i want to replace variables with some data.
but the third column is an image which is different on each line

It could be like this code :

<?php

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

$data = array(
            array(
                'ITEM' => 'Product A',
                'REFERENCE' => '107AW3',
                'IMAGE' => '<img src="http://www.w.javadocx.com/img/logo_badge.png" />'
            ),
            array(
                'ITEM' => 'Product B',
                'REFERENCE' => '204RS67O',
                'IMAGE' => '<img src="http://www.w.javadocx.com/img/preview/phpdocx_box_corporate.png" />'
            ),
            array(
                'ITEM' => 'Product C',
                'REFERENCE' => '25GTR56',
                'IMAGE' => '<img src="http://www.w.javadocx.com/img/preview/phpdocx_box_enterprise.png" />'
            )
        );

$docx->replaceTableVariable($data, array('parseLineBreaks' => true));

?>

but i don't know if :

- replaceTableVariable method can support HTML code instead of basic text

- replaceVariablebyHTML can work on template table (as replaceTableVariable does)

- maybe i have to create wordFragment for each "IMAGE" value in $data php array

thanks for help

regards

Posted by admin  · 27-07-2016 - 07:31

Hello,

UPDATE: latest versions of phpdocx support adding WordFragments as table and list values when doing replacements.

Using the current version of phpdocx, the best approach is to do this task in two steps:

1. Add placeholders as values.

2. Use the method replaceVariableByWordFragment to replace the previous placeholders to add the images.

We're working on adding WordFragments support as values in list and table replace methods.

Regards.

Posted by Simplexe  · 27-07-2016 - 10:02

Hello,

Thanks for answer.

that's exactly the solution i finally found.

not very fast to code, but that works fine!

Note that there is bug in replaceVariableByWordFragment method with addImage Context in case of file name contains dot character

for example, when file name is "my.file.jpg', it does not work

with "myfile.jpg', it works

 

thanks