Forum


Replies: 5   Views: 2685
Html in table cell
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 developercoffeeshop  · 07-11-2018 - 13:37

How do I get HTML to format inside a table cell with addTable? I'll consider any method, even replacing HTML tags with wml

Posted by admin  · 07-11-2018 - 14:38

Hello,

The addTable method, as other methods, allows adding WordFragments (https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml) as contents.

You just need to create a new WordFragment, insert the HTML and then add the values to the table:

$html = new WordFragment($docx);
$html->embedHTML($htmlContent);

$valuesTable = array(
  array(
    'Title A',
    'Title B',
    'Title C',
  ),
  array(
   'Line A',
   'Line B',
   $html,
  ),
);

$docx->addTable($valuesTable);

You can see a full sample using WordFragments on the API page of the method (https://www.phpdocx.com/api-documentation/word-content/add-table-Word-document-with-PHP).

You can also add a placeholder to the table as value an then use replaceVariableByHTML or replaceVariableByWordFragment to replace it with a WordFragment of the HTML.

Regards.

Posted by developercoffeeshop  · 07-11-2018 - 17:17

Thanks, I understand how this would work, but since moving to the live server embedHTML no longer works, it actually gives an error 500 with nothing in the log. Any idea what could be causing this?

Posted by admin  · 07-11-2018 - 17:39

Hello,

Maybe Tidy for PHP (http://php.net/manual/en/book.tidy.php) is not installed or enabled?

A 500 error is a generic PHP error and it should always being added to the log, we recommend to check your server configuration to find where are those errors added.

We also recommend you test the included samples (addText and embedHTML) using PHP CLI mode to check if the problem is from some missing PHP extension (ZIP, XML, Tidy), missing rw access, a license error or any other source.

Regards.

Posted by developercoffeeshop  · 08-11-2018 - 06:40

Got it working by installing PHPtidy and validating the HTML, it seemed that & signs were causing some issues

$doc = new DOMDocument();
$data_text = str_replace(' ', ' ', $data_text);
$data_text = str_replace('&', '&', $data_text);
                                                                
$doc->loadHTML($data_text);

$html = new WordFragment($docx, 'document');
$html->embedHTML($doc->saveHTML());

 

Posted by admin  · 08-11-2018 - 07:17

Hello,

phpdocx handles all protected XML characters when adding new contents. Please check the exact content you are adding.

Regards.