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".

Posted by admin  · 13-02-2018 - 13:14

Hello,

A Word style only applies to an specific tag. In your code, 'myStyle' only applies to 'h1', and you can't apply a custom paragraph style to a table as you are doing, you need to set specific styles.

If you want to apply a Word style to a table, you need to create a table style. Other important point about your code, as you can read on the API page of the method (https://www.phpdocx.com/api-documentation/word-content/embed-html-Word-document-with-PHP):

strictWordStyles : If true ignores all CSS styles and uses the styles set via the wordStyles option (see next)

If this option is true, all CSS are ignored.

We recommend you to use two approaches:

· Generate a template using MS Word with all styles you want to use and then import them to the DOCX to be able to be used (https://www.phpdocx.com/api-documentation/layout-and-general/import-styles-from-a-Word-document-with-PHP).

· Use CSS to set global properties:

body {
...
}

p {
...
}

 

Regards.