Forum


Replies: 3   Views: 354
Replacevariablebyhtml and img with a float
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 ninof  · 05-02-2024 - 02:47

There appears to be a bug with having an image with a float attribute when using replaceVariableByHTML The following code always produces a corrupt .docx - at least when trying to open the output in Word 2007 (I know).

$docx=new CreateDocxFromTemplate("../tmp/Test.docx",array('parseMode'=>true));
$docx->setTemplateSymbol('|');

$html="<p><img src='../tmp/test.jpg' style='float:right;' alt='img_qrcode'>Testing floating image<br>line 1<br>line 2<br>line 3<br>line 4<br>line 5<br>line 6<br></p>";
$docx->replaceVariableByHTML('iHowtovote','block',$html, array('isFile' => false,'parseFloats' => true, 'downloadImages' => true));

$docx->createDocx("../tmp/Output.docx");

 

Posted by admin  · 05-02-2024 - 07:50

Hello,

We have done some tests with your code and in all cases the DOCX output is correct and no corrupted error appears when opening the DOCX with MS Word 2021, MS Word 2019, MS Word 2016, MS Word 2013, MS Word 2010, MS Word 2007, MS Word Online and LibreOffice 7.

After doing some more tests with an outdated version of MS Word 2007 (a very old version of MS Word 2007), we think we have been able to replicate your issue. With other MS Word 2007 revisions and any other MS Word versions these error don't appear. It only happens with some outdated old versions of MS Word 2007 when the float style is applied transforming HTML. It's a very strange error from the order of a specific tag.
Please do the following change in your version of phpdocx:

Edit HTML2WordML.php and go to the following line (around line 1676):

if (isset($properties['float']) && ($properties['float'] == 'left' || $properties['float'] == 'right')) {

In this if block, replace the following lines:

$this->wordML .= '<wp:extent cx="' . $cx . '" cy="' . $cy . '" /><wp:wrapSquare wrapText="bothSides" /><wp:docPr id="' . $docPr . '" name="' . $tempName . '" descr="' . rawurlencode($descr) . '">'.$docPrContent.'</wp:docPr>';
$this->wordML .= '<wp:effectExtent b="'.$distance['bottom'].'" l="'.$distance['left'].'" r="'.$distance['right'].'" t="'.$distance['top'].'"/>';

with:

$this->wordML .= '<wp:extent cx="' . $cx . '" cy="' . $cy . '" />';
$this->wordML .= '<wp:effectExtent b="'.$distance['bottom'].'" l="'.$distance['left'].'" r="'.$distance['right'].'" t="'.$distance['top'].'"/>';
$this->wordML .= '<wp:wrapSquare wrapText="bothSides" /><wp:docPr id="' . $docPr . '" name="' . $tempName . '" descr="' . rawurlencode($descr) . '">'.$docPrContent.'</wp:docPr>';

(as you can check, we have just moved wp:effectExtend after wp:extent).

After applying this minor change, please try again, it should be solved now with your MS Word 2007 version. This same change has been added to the current testing branch to be included in the stable release. We have done tests with all MS Word versions after applying this change and the DOCX is correct in all cases.

Regards.

Posted by ninof  · 05-02-2024 - 12:50

Thank you.

I am super impressed by your debugging - that mod certainly worked!

In my use case I am using a Word template to create a template so the Alt attribute is important to my application. I need to add alt='|img_qrcode|' (including placeholders) but replaceVariableByHTML does not carry the alt value but instead replaces it with the name of the image file ie alt='qrcode.png'.

Is this by design or is there a way to do this?

Regards

Posted by admin  · 05-02-2024 - 13:38

Hello,

Sorry, but the current stable release of phpdocx doesn't support setting an alt attribute in images when transforming HTML (https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-image-Word-document-with-HTML) and set it as descr value.

We have opened a task to the dev team and they have generated the needed changes in the current testing branch to be added in the next stable release of phpdocx. Please note that only users with active LUS can access beta/testing updates (https://www.phpdocx.com/updates). If you purchase LUS for your license (on MY PHPDOCX page after login), please send a message to contact[at]phpdocx.com and the dev team will send you the class updated and a sample using it.

Regards.