Forum


Replies: 1   Views: 3401
How do i set a default style for the whole embedded html?
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 WTO  · 13-02-2018 - 09:35

I want to embed HTML snippets, and want to apply the document styles to it. However, it seems I can only apply styles to specific selectors, and the styles don't even cascade down to the child elements.

For example, I would expect this to at least style my header and table:

<?php

require_once '../../../classes/CreateDocx.inc';

$docx = new CreateDocx();
$html = '<h1 style="color: #b70000">An embedHTML() example</h1>';
$html .= '<p>We draw a table with border and rawspans and colspans:</p>';
$html .= '<table border="1" style="border-collapse: collapse" width="600">
            <tbody>
                <tr width="600">
                    <td style="background-color: yellow">1_1</td>
                    <td rowspan="3" colspan="2">1_2</td>
                </tr>
                <tr width="600">
                    <td>Some random text.</td>
                </tr>
                <tr width="600">
                    <td>
                        <ul>
                            <li>One</li>
                            <li>Two <b>and a half</b></li>
                        </ul>
                    </td>
                </tr>
                <tr width="600">
                    <td>3_2</td>
                    <td>3_3</td>
                    <td>3_3</td>
                </tr>
            </tbody>
        </table>';

$docx->createParagraphStyle('myStyle', array(
  'font' => 'Verdana',
  'fontSize' => 20,
  'bold' => 'on',
  'smallCaps' => 'on',
));
$docx->embedHTML($html, array(
  'strictWordStyles' => true,
  'wordStyles' => array(
    'h1' => 'myStyle',
    'table' => 'myStyle',
  ),
));

$docx->createDocx('example_embedHTML_1');

But it doesn't. It does cancel the default styles, but it will put everything in Calibri, 11. I want my document to have default styles for body text, as well as tables. How can I make sure it applies correctly to the HTML I import? And I don't want to list all imported tags, classes and IDs explicitly; there are thousands of snippets to import, and although they're all fairly consistent, the chance of missing something is too great. We really need to be able to apply a style "globally".