Forum


Replies: 3   Views: 1637
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 martijnlg  · 18-01-2021 - 10:57

Hi, we are evaluating PHPdocx for a project and have issues with converting HTML headings <h1><h2><h3> etc, into actual headings in MS Word. The generated docx file contains text that visually looks like headings, but are all standard text instead of actual headings. Is this a know limitation or is there something we can do to actually get proper headings in the Word file?

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.

Posted by martijnlg  · 19-01-2021 - 08:21

Thanks for the swift reply. This works, but makes the style of a heading "Heading 1 PHPDOCX". Is there a way to use the default MS Word style "Heading 1" instead?

Using 

'<h1>' => 'Heading1',

Does not work?

Posted by admin  · 19-01-2021 - 08:38

Hello,

UPDATE: Working with headings using PHP methods and HTML contents

To use other style names than the default ones, you can use a custom template (with data or empty) or generate the styles dynamically with phpdocx methods. If you want to use the default MS Word style names (that don't have always names such as Heading1, because style names may depend on the DOCX program used to generate the document and the language of the DOCX editor), you can create an empty template (with MS Word or other DOCX program) and open it with the CreateDocxFromTemplate class:

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

CreateDocxFromTemplate inherits CreateDocx, so you can use the methods available in both classes.

Using the parseStyles method you can generate a DOCX with the existing styles in the template. And also use importStyles to import styles from an external DOCX if needed.

As you are using the trial package, we recommend you to read the documentation available, for example on the Practical guide: https://www.phpdocx.com/documentation/practical. If you purchase a license, do not hesitate to send an email to contact[at]phpdocx.com, we'll generate a custom template and script that illustrates how to use an empty template and its styles if needed.

Regards.