Forum


Replies: 7   Views: 798
How to create heading with formatstyles in embedhtml?
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 Robin  · 24-11-2020 - 09:55

Hello,

I am trying to convert a HTML to DocX with embedHTML(). That works correctly!

But how can I convert a header-HTML Tag to a header in DocX with a headinglevel so that it was recognized as header that it can be displayed in the table of contents.

With regards

 

Posted by admin  · 24-11-2020 - 10:01

Hello,

What version and license of phpdocx are you using?

Regards.

Posted by Robin  · 24-11-2020 - 10:03

I am currently using the the latest trial version to test my application.

If everything works fine, I want to buy the Basic license.

Posted by admin  · 24-11-2020 - 10:11

Hello,

Using a Basic, a Trial or an Advanced package, HTML contents can be added to TOC using heading tags (h1, h2...) or a custom paragraph style. On https://www.phpdocx.com/documentation/cookbook/tables-of-contents you can read more information about adding contents to TOC.

Adding a header/footer to a DOCX from HTML using a header/footer tag is only available in Premium licenses using HTML Extended (https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP):

HTMLExtended::$tagsBlock['header'] = 'addHeader';
HTMLExtended::$tagsBlock['footer'] = 'addFooter';

$html = '
    <head>
        <style>
            p {font-style: italic;}
            p.header {font-size: 20px; font-weight: bold;}
            div.footer {font-size: 10px;}
        </style>
    </head>
    <header data-type="default">
        <p class="header">
            Custom header <strong>with strong style</strong>
        </p>
    </header>
    <p>Lorem ipsum dolor sit amet.</p>
    <footer>
        <div class="footer">
            <table border="1" style="border-collapse: collapse" width="600">
                <tbody>
                    <tr>
                        <td width="300">Cell A</td>
                        <td width="300"><img src="image.png" width="35" height="35" style="vertical-align: -15px"></p></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </footer>
';

$docx->embedHTML($html, array('useHTMLExtended' => true));

All licenses can also use replaceVariableByHTML to replace contents by HTML in templates and use addHeader/addFooter to add headers/footers with contents such as text, images, tables, HTML and others.

Regards.

Posted by Robin  · 07-12-2020 - 15:41

Thank you for your response.

I've tried it with 

$docx->addText('This is a text', array('bold' => true, 'headingLevel' => 1));

but the formarting style doesn't appears.

http://prntscr.com/vxlpqb

I would like to have the marked headingLevel. Is this possible?
 

 

Posted by admin  · 07-12-2020 - 16:24

Hello,

The headingLevel option sets the paragraph as heading (w:outlineLvl tag in the DOCX). If you want to apply also a heading style you need to set the pStyle option available in the same method.

Methods such as addHeading or embedHTML using heading tags (h1, h2, h3...) adds heading paragraphs with a custom paragraph style by default. The custom paragraph style applied can be the default ones included in phpdocx, generated from scratch using the API methods (https://www.phpdocx.com/documentation/practical/styles), or imported from a template.

Regards.

Posted by Robin  · 14-12-2020 - 15:22

$docx->addText('Contents',array('pStyle' => 'Heading1'));

Thank you for your response.

Also with pStyle, it doesn't apply a heading style.

Posted by admin  · 14-12-2020 - 15:47

Hello,

If you run

$docx->addText('Contents',array('pStyle' => 'Heading1'));

and you don't get a custom paragraph style applied to that content is because your DOCX doesn't include that style name (Heading1). Please test and run the included sample Core/addTableContents/sample_2.php that applies custom paragraph styles included in the default template of phpdocx, for example:

$docx->addText('Chapter 1', array('pStyle' => 'Heading1PHPDOCX'));

If you don't know the name of the style/s you want to apply, you can get them using importStyles (https://www.phpdocx.com/api-documentation/layout-and-general/import-styles-from-a-Word-document-with-PHP). On https://www.phpdocx.com/documentation/practical/styles you can read the practical guide about using styles with phpdocx.

Regards.