Forum


Replies: 4   Views: 300
Custom list style & bold using html extended with stylesreplacementtype
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 tangamampilia  · 16-08-2023 - 17:22

Hi,

I'm trying to implement a custom list style with some tags inside. This is my sample code:

$docs = new CreateDocxFromTemplate('document.docx');

$docs->createListStyle('latin', [
        ['type' => 'decimal', 'format' => '%1.'],
        ['type' => 'lowerLetter', 'format' => '%2.'],
        ['type' => 'lowerRoman',  'format' => '%3.'],
]);

$html = '
<ul>
<li><b>Hello</b> World</li>
</ul>
';

$docs->replaceVariableByHTML('LIST', 'block', $html, ['stylesReplacementType' => 'mixPlaceholderStyles', 'disableWrapValue' => true, 'addDefaultStyles' => false, 'customListStyles' => true]);

If I change the addDefaultStyles to true, the bold text works but the rest of the styles (font size & line spacing) are also updated. If addDefaultStyles is false, then the bold fails and font size & line spacing works.

Do you have any suggestion? I have the premium license. 

Thanks!

 

Posted by admin  · 16-08-2023 - 22:10

Hello,

What version of phpdocx are you using? Your username doesn't have any license tied, please send to contact[at]phpdocx.com the username or email of the user that pruchased the license you are using.

Your code is not applying the custom list style correctly. Please check the embedHTML/sample_2.php script included in the package, you need to set the class name:

$latinListOptions = array();
$latinListOptions[0]['type'] = 'lowerLetter';
$latinListOptions[0]['format'] = '%1.';
$latinListOptions[1]['type'] = 'lowerRoman';
$latinListOptions[1]['format'] = '%1.%2.';
$docx->createListStyle('latin', $latinListOptions);

$html = '
<ul class="latin">
    <li>First item.</li>
    <li>Second item with subitems:
        <ul>
            <li>First subitem.</li>
            <li>Second subitem.</li>
        </ul>
    </li>
    <li>Third item.</li>
</ul>';
$docx->embedHTML($html, array('customListStyles' => true));

Also note that using 'stylesReplacementType' => 'mixPlaceholderStyles' you are requesting to mix placeholder and HTML styles. Please check the stylesReplacementType and the available options, maybe you don't need to use that option.

Regards.

Posted by uncontested  · 18-08-2023 - 22:04

Hi, this is the account with license :)

Yes, sorry.... I forgot to add the class in the simplified code, but I have it in my code. The issue is the above sample renders the HTML in this way:

https://www.dropbox.com/scl/fi/dxgg6p7f1g3mia9zkyo2k/1.png?rlkey=5idaj2xjxhbf65zdytxvkk5y4&dl=0

 If I activate the addDefaultStyles to true, then it renders the bold but the font-size and line spacing is being changed.

https://www.dropbox.com/scl/fi/2j40bser5comgu357uj8p/2.png?rlkey=0bo1acsllpsuck8h5aijsp5op&dl=0

This is the main issue.... 

Posted by admin  · 18-08-2023 - 23:39

Hello,

When a block type replacement is done, the whole paragraph is removed to add the new block content.
Also note that some default styles (such as the font size) are applied when transforming HTML.

You can set the styles to be applied (such as the line spacing and the font size) to the imported HTML (using CSS styles). Or you can set the stylesReplacementType option (available in Premium licenses) as mixPlaceholderStyles to mix them. In this case you may need to also use the stylesReplacementTypeIgnore option.

For example:

// ignore font size when mixing the styles
$docx->replaceVariableByHTML('ADDRESS', 'block', '<p>phpdocx by <b>2mdc</b></p>', array('stylesReplacementType' => 'mixPlaceholderStyles', 'stylesReplacementTypeIgnore' => array('w:sz', 'w:szCs')));

In the Templates/replaceVariableByHTML folder available in the package you can find some samples. The addDefaultStyles option removes as many default styles as possible, but it doesn't mix styles.

If you sent to contact[at]phpdocx.com the DOCX template and the simplest script using phpdocx standalone, we'll check it.

Regards.

Posted by uncontested  · 20-08-2023 - 19:38

Finally I could reproduce the issue. This only happens when we use subvariables, I sent you a code sample.

Thanks!