Forum


Replies: 2   Views: 671
Add headings (titles in html) in word document keeping document styles for these titles ?
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 admin  · 26-10-2022 - 14:13

Hello,

You can set a custom paragraph style to the HTML. This style can exists in the DOCX template, generated from scratch using phpdocx methods or imported using the importStyles method.

On the following page you can find all information about working with headings using PHP methods and transforming HTML:

https://www.phpdocx.com/documentation/cookbook/working-with-headings

For example, if you want to apply the Heading1 style to the transformed HTML:

$docx = new CreateDocxFromTemplate('template.docx');

$html = '
    <style>
        h1 {
            font-weight: normal;
        }
    </style>
    <h1>Heading 1</h1>
';
$docx->replaceVariableByHTML(
    'MY_VAR',
    'block',
    $html, 
    array(
        'wordStyles' => array(
            '<h1>' => 'Heading1', 
        ),
        'strictWordStyles' => true,
    )
);

$docx->createDocx('output');

or using HTML Extended features included in Premium licenses:

$docx = new CreateDocxFromTemplate('template.docx');

$html = '
    <style>
        h1 {
            font-weight: normal;
        }
    </style>
    <h1 data-style="Heading1">Heading 1</h1>
';
$docx->replaceVariableByHTML(
    'MY_VAR',
    'block',
    $html,
    array('strictWordStyles' => true, 'useHTMLExtended' => true),
);

$docx->createDocx('output');

The style name to be applied must exist in the DOCX. If you want to mix HTML styles and MS Word styles when calling replaceVariableByHTML you need to use the stylesReplacementType available in Premium licenses.

For further support, if you send to contact[at]phpdocx.com the most simple DOCX template you are using with a placeholder to be replaced, we'll generate a sample script.

Regards.