Search code examples
phphtmlxmldocxphpword

Inserting HTML formatted text in a .docx template


I have some .docx templates on which I need to insert text generated by a php script and formatted with HTML markups.

I have tried using phpword template manipulation option, but you can't enter full blocks, you can only enter a non-formatted single-line value.

There is the solution to convert the template in HTML, insert the generated text, then save it back with a msword MIME, but then it would not fully keep the integrity of headers and footers.

Next one I found is phpdocx, but 249 euros just to use the embedHTML function is too much for me.

So my next idea would be the following:

1) insert a tag in the word template : {{{tag}}}

2) load the template .docx file in my php script as an XML stream.

3) create a .docx document with phpword containing my generated text between 2 tags {{{tag1}}} and {{{tag2}}}

4) load the document generated in 3) as an XML stream and extract the blocks between the 2 tags.

5) Insert the block extracted in 4) to replace the initial {{{tag}}} in the XML stream loaded in 2)

But I'm really not sure it would work as I don't really understand how Word build its XML...

Is XML structure hierarchival so this idea of a block replacement by another one including subblocks could work ?


Solution

  • You can try the pandoc library in linux which is meant for this very purpose.