开发者

How to dynamically insert images to docx template?

In my web-application i'm using phplivedocx for text changing. But i also need to dynamically change images in my docx template. What tool do you recommend?

Than开发者_如何转开发ks in advance.


Found the solution.

quote from phplivedocx forum: "I found a way to insert dynamic image in docx template before sending it to LiveDocx (so it don't work with hosted templates).

That's the way I do it (in Php): - First I put a default image in my local word template - Before generating the PDF, considere the docx as if it was a zip archive (you can extract manually to confirm it), so: - In this archive replace the file /word/media/image1.png by the dynamic image (flat png formated, don't know about format limitations) - Close the archive.

At this step, if you open the docx template in word, you'll be able to see the dynamic image replacing the default image.

After that, you can process the request to liveDocx.

There's some limitations in this method and I hope Boilerplate mecanism will come soon to do it a better way, but in my case it just work like I want. "

Here is the code how to change image in docx template in php:

$zip = new ZipArchive;
$zip->open('documents/template_tm.docx');
$zip->addFile('new_image.png', 'word/media/image1.png');
$zip->close();


It s also possible for more than one image. In the document.xml you can find the image with the original name. Linked to the image is a with an ambed code. This same code you can find back in a relationship in _rels/document.xml.rels. The target in this relationships points to the correct image number


If you are interested I have created extension, that can easily make this work - https://github.com/igorrebega/docx-replacer

This code will replace text $search to image that are located in $path in $pathToDocx file

$docx = new IRebega\DocxReplacer($pathToDocx);

$docx->replaceTextToImage($search, $path);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜