Forum


Replies: 4   Views: 540
Customizewordcontent + embedhtml
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 rjharrison  · 18-10-2022 - 14:07

I've embedded HTML via embedHTML(). I then want to use customizeWordContent to apply transformations to the images (eg set imageAlign = 'center').

This doesn't seem to work. Is this supported?

$docx->embedHTML($html, [
  'useHTMLExtended' => true,
]);
       

// Center all images
$docx->customizeWordContent([
  'type' => 'image',
], [
  'imageAlign' => 'center'
]);

 

Posted by admin  · 18-10-2022 - 14:35

Hello,

It's working correctly. Please run the following simple code:

$docx = new CreateDocx();

$html = '<p><img src="image.png" width="90" height="90"></p>';
$docx->embedHTML($html, ['useHTMLExtended' => true]);

$docx->customizeWordContent(['type' => 'image'], ['imageAlign' => 'center']);

$docx->createDocx('output');

HTML sets a default left align. Then the customizeWordContent method sets a center align to the image.

Maybe your HTML is floating the image, so MS Word ignores the alignment, or some other parent style of the image is applying another alignment to the image.

Regards.

Posted by rjharrison  · 18-10-2022 - 15:39

Thanks - I can reproduce your working example. 

In my case the image was inside a table: <table><tr><td><img /></td></tr></table>

The table has {width:100%} via a css style and that works, but the image does not center within it. 

Posted by admin  · 18-10-2022 - 15:52

Hello,

By default DOCXPath and DOCXCustomizer methods select elements that are direct children of the root tag, w:body for the document content.

On https://www.phpdocx.com/documentation/introduction/docxpath and the API pages you can read this same information:

parent (string, main document body as default, allows to set any parent or a specific one)

Main document body as default, allows to set any parent or a specific one. w:body (default), '/' (any parent) or any other specific parent (/w:tbl/, /w:tc/, /w:r/...).

As you are adding the image in a table, you need to query the elements in all parents:

$docx = new CreateDocx();

$html = '<table width="100%"><tr><td><img src="image.png" width="90" height="90"></td></tr></table>';

$docx->embedHTML($html, ['useHTMLExtended' => true]);

$docx->customizeWordContent(['type' => 'image', 'parent' => '/'], ['imageAlign' => 'center']);

$docx->createDocx('output');

Also note that you can add a center align image in a table with embedHTML without using DOCXCustomizer:

$html = '<table width="100%"><tr><td><p style="text-align: center;"><img src="image.png" width="90" height="90"></p></td></tr></table>';

$docx->embedHTML($html);

DOCXCustomizer allows changing styles on-the-fly, but if you are adding new contents the easiest approach is applying the styles to the contents. On the following pages you can find all options  and stylesavailable for each method and tag:

Regards.

Posted by peteruser1  · 29-10-2022 - 16:37

Deleted by admin · 30-10-2022 - 08:19