Forum


Replies: 2   Views: 438
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 grizzlydev  · 26-10-2022 - 13:33

I'm currently using the function replaceVariableByHTML in order to import html code in my word document.

I would like to add titles as <h1>...</h1>, <h2>...</h2> without any style. But i want them to be added with the document style for the titles h1, h2 ...

The problem is that there are insert with the standard style, not as titles.

I tried many things, I don't know if it's possible or not ?

Thanks for any help.

 

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.

Posted by peteruser1  · 29-10-2022 - 16:36

Deleted by admin · 30-10-2022 - 08:19