Forum


Replies: 1   Views: 1093
Adddefaultstyles: where are the styles drawn from?
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 Lochspring  · 16-08-2021 - 19:13

Hi there.  We're working with PHPDocX 10, and generating our docs using the CreateDocxFromTemplate function.  I'm trying to figure out exactly where a set of styles are being drawn from when using the embedHTML or ReplaceVariableWithHTML function.

Let me start by saying I have a Heading1-6 style in my Word Template.  This, in turn, is connected to the <h1>-<h6> tags in the call to the function, as shown here:
 

$this->document->replaceVariableByHTML(
    $sectionTag,
    'block',
    $value,
    [
        'stylesReplacementType' => 'mixPlaceholderStyles', 
        'useHTMLExtended'       => true,
        'isFile'                => false,
        'parseFloats'           => true,
        'embedFonts'            => true,
        'cssEntityDecode'       => true,
        'downloadImages'        => true,
        'addDefaultStyles'      => false,
        'strictWordStyles'      => false, 
        'wordStyles'            => [     
            '<h1>' => 'Heading1',
            '<h2>' => 'Heading2',
            '<h3>' => 'Heading3',
            '<h4>' => 'Heading4',
            '<h5>' => 'Heading5',
            '<h6>' => 'Heading6',
            '<table>' => 'ZimitTable',
        ],
    ]
);

My issue arises with addDefaultStyles.  When I set this value to "TRUE", inline styles in my HTML (say, setting color:#ff0000) are honored, and work perfectly.  However, when I do that, I am also getting a 12pt Black style being applied to all Headings and MOST Normal-styled paragraph elements.  I have removed all CSS stylesheets from the run, including the base one found in /templates/html.css. I have been unable to find where this style is being forced into the document inline.  

Please note that I can't force strictWordStyles to be true here.  I need the inline styles to function so we can override on a case by case basis.  

Posted by admin  · 16-08-2021 - 20:45

Hello,

Firt, we must say that the mixPlaceholderStyles option in the replaceVariableByHTML method is available since phpdocx 11 (https://www.phpdocx.com/news/post/phpdocx-v11-release-notes/222), it's not available in phpdocx 10. Please check if you are using phpdocx 11 Premium; otherwise, that option is not available.

About your question, maybe your issue comes from using mixPlaceholderStyles in placeholders that already have those styles (if using phpdocx 11) or your code is not overwritting the default styles correctly (in template/html.css or the contents you are adding).
By default, the addDefaultStyles option is true; it can be set as false to avoid adding default styles in the very specific cases that is needed.
The library handles some styles automatically, such as font-size, to do the transformations correctly, so the strictWordStyles option may be needed to avoid not adding them when needed. Using the stylesReplacementType option you can mix and use styles from placeholders and HTML.

The easiest approach is applying the styles to be used in the CSS you are adding instead of removing the default ones.

As your other topic, we'd need to check your template and HTML you are adding for futher support and be able to generate a custom script. In addition to know if you are using phpdocx 11 or phpdocx 10 and the correct options for the version you are using.

Regards.