Forum


Replies: 3   Views: 1646
Html to docx conversion does not create headings in word?
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  · 18-01-2021 - 11:47

Hello,

UPDATE: Working with headings using PHP methods and HTML contents

Yes, phpdocx transforms HTML headings to MS Word headings. You can read this information on HTML to Word documentation:

h1, h2, h3, h4, h5 and h6: They are parsed as Word headings and as such they may show up in a TOC included in the Word document.

When you add a heading HTML (h1, h2, h3...), phpdocx applies a w:outlineLvl tag to the transformed content. This tag sets the content as MS Word heading (outline level) and apply default styles:

$html = '
    <h1>Heading 1</h1>
    <h2>Heading 2</h2>
    <h3>Heading 3</h3>
    <h4>Heading 4</h4>
    <h5>Heading 5</h5>
    <h6>Heading 6</h6>
    <p>Not heading text</p>
';
$docx->embedHTML($html);

If you also want to highlight a style in the MS Word interface when transforming HTML, you need to set what Word styles you want to apply for each tag (or class or id, on the Using native Word formatting with HTML section you can read more information about this feature). For example, using the default styles included in phpdocx (any other styles can be applied when using a template or custom styles):

$html = '
    <h1>Heading 1</h1>
    <h2>Heading 2</h2>
    <h3>Heading 3</h3>
    <h4>Heading 4</h4>
    <h5>Heading 5</h5>
    <h6>Heading 6</h6>
    <p>Not heading text</p>
';
$docx->embedHTML($html, 
    array('wordStyles' => array(
        '<h1>' => 'Heading1PHPDOCX', 
        '<h2>' => 'Heading2PHPDOCX', 
        '<h3>' => 'Heading3PHPDOCX', 
        '<h4>' => 'Heading4PHPDOCX', 
        '<h5>' => 'Heading5PHPDOCX', 
        '<h6>' => 'Heading6PHPDOCX')
    )
);

Using a Premium license you can enable HTML Extended to assign these styles using a custom attribute:

$html = '
    <h1 data-style="Heading1PHPDOCX">Heading 1</h1>
    <h2 data-style="Heading2PHPDOCX">Heading 2</h2>
    <h3 data-style="Heading3PHPDOCX">Heading 3</h3>
    <h4 data-style="Heading4PHPDOCX">Heading 4</h4>
    <h5 data-style="Heading5PHPDOCX">Heading 5</h5>
    <h6 data-style="Heading6PHPDOCX">Heading 6</h6>
    <p>Not heading text</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Using a template or custom styles the default styles used by phpdocx can be replaces by other style names.

Regards.