Forum


Replies: 5   Views: 2542
Replacevariablebyhtml nevers displays a 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 intRE  · 16-01-2018 - 13:38

Hi,

 

When replacing a variable with HTML containing a table, it never displays the table, just the text in the table as a block of text. I am testing using a clean install, latest trial copy version, on openSUSE using example code I found in your documentation, specifically:

 

$html = '<table border="1" style="border-collapse: collapse" width="400">
            <tbody>
                <tr>
                    <td style="background-color: yellow">1_1</td>
                    <td rowspan="3" colspan="2">1_2</td>
                </tr>
                <tr>
                    <td>Some random text.</td>
                </tr>
                <tr>
                    <td>
                        <ul>
                            <li>One</li>
                            <li>Two <b>and a half</b></li>
                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>';

$docx->replaceVariableByHTML('CHUNK_2', 'inline', $html, array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));

 

The result in the docx document:

1_11_2Some random text.OneTwo and a half

Any ideas? The functionality is crucial, the client large, and I'm getting nervous... 8)

Thanks,

Rian

Posted by admin  · 16-01-2018 - 15:36

Hello,

A table can't be replaced as an inline element or you'll only get its inline text. You must do a block replacement for tables and other block elements:

$docx->replaceVariableByHTML('CHUNK_2', 'block', $html, array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));

All samples available in the package and on the website use block replacements when replacing a placeholder with a table HTML.

We recommend you to read the tutorial page about HTML to DOCX (https://www.phpdocx.com/documentation/introduction/html-to-word-PHP); from this page:

The type of substitution that may be:
block: The whole paragraph containing the variable is replaced by the corresponding HTML.
inline: Only the variable itself is replaced by the inline HTML content (block elements are removed from the code).

Regards.

Posted by intRE  · 17-01-2018 - 08:25

Hi,

Sorry, missed that. Also needed a clean template, guess the one I had took some damage.

On the topic of moving tables to docx, docx does not seem to respond to white-space: nowrap; directives in CSS. Is there a way of avoiding breaks/wraps in table titles when transferring to docx?

Thanks,

Rian

 

Posted by admin  · 17-01-2018 - 09:33

Hello,

The white-space property is not supported when importing HTML/CSS. On https://www.phpdocx.com/documentation/introduction/html-to-word-PHP you can read about the supported tags and properties (Supported HTML tags and attributes and Supported CSS properties sections).

For not supported properties, you can use native MS Word styles. On the previous page you can find samples about how to use MS Word styles when importing HTML.

Regards.

Posted by intRE  · 18-01-2018 - 09:46

Thanks. Good to know, although not respecting the white-space directive, often used for table headings, does seem a shame. It means more effort for simple tables. Perhaps in a future version... 8)

Posted by admin  · 18-01-2018 - 10:05

Hello,

We have moved your request to the dev team to be added to a next release of phpdocx.

Regards.