Forum


Replies: 2   Views: 1387
Embedhtml renders h* with bold character style
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 pwctechnicalsecurity  · 03-04-2021 - 09:05

Hi,

embedHTML renders all headers with bold characterstyle.

See the snippet below. How can I ensure that this is not the case?

The issue is present in our own template, as well as test.docx (which is basically a brand new Word default template). In itself the default template has no bold style, yet this snippet renders it as bold.

Any suggestions?

require_once '../../../Classes/Phpdocx/Create/CreateDocx.php';

$docx = new Phpdocx\Create\CreateDocxFromTemplate('test.docx');

$html = '<h1>An embedHTML() example</h1>';
$html .= '<h2>Test</h2>';
$html .= '<h3>Test</h3>';
$html .= '<h4>Test</h4>';
$html .= '<h5>Test</h5>';
$html .= '<h6>Test</h6>';

$docx->embedHTML($html, array(
    'strictWordStyles' => true,
    'wordStyles' => array(
        '<h1>' => 'Heading1',
        '<h2>' => 'Heading2',
        '<h3>' => 'Heading3',
        '<h4>' => 'Heading4',
        '<h5>' => 'Heading5',
        '<h6>' => 'Heading6'
    )
));

$docx->createDocx('issue');

Posted by admin  · 03-04-2021 - 12:01

Hello,

Heading tags apply a bold style because this style is included in the default CSS template when adding HTML (as any other browser or library that work with HTML, phpdocx has a default CSS template to handle styles correctly):

h1, h2, h3, h4, h5, h6 {
  display: block;
  font-weight: bold;
}

You can find this default CSS in the following path in the package: templates/html.css. 

You can customize this template if you don't want to apply bold styles to heading tags, or change it when importing HTML/CSS applying a custom style as CSS allows:

h1, h2, h3, h4, h5, h6 {
  font-weight: normal;
}

You can apply it by changing the base template (html.css) to do a global change, when calling HTML methods such as embedHTML:

$html = '<style>h1, h2, h3, h4, h5, h6 {font-weight: normal;}</style>';
$html .= '<h1>An embedHTML() example</h1>';

to only change it for that specific HTML, or with addBaseCSS available in HTML Extended (https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP) to apply the styles to all contents added using embedHTML/replaceVariableByHTML in the same script.

A few specific styles are always added although the strictWordStyles option is set to true to guarantee the correct internal workflow of the conversion and compatiblity reasons with previous versions of phpdocx.

Regards.

Posted by pwctechnicalsecurity  · 05-04-2021 - 17:48

Works. Thank you!