Forum


Replies: 4   Views: 1666
How to use div tags properly when importing html?
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  · 14-01-2021 - 07:01

Hello,

We recommend you to check the available documentation to transform HTML to Word:

https://www.phpdocx.com/documentation/introduction/html-to-word-PHP

https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP (HTML Extended is only available in Premium licenses)

https://www.phpdocx.com/documentation/htmlapi-documentation

You are adding a div tag, and by default, phpdocx handles div tags as containers (from the previous documentation pages):

div: Although this tag is probably the most frequent in modern HTML code, it does not have a direct translation into Word. phpdocx offers different parsing options:

Only use it for the CSS inheritance and parse consequently its child elements.

Parse them as a "p" element with the option "parseDivs" set to "paragraph" (this may be an useful option when using HTML code coming from a WYSIWYG editor).

Parse it as a table with the option "parseDivs" set to "table". This may be the most accurate option if one may decide to preserve all available formatting but may produce complicated Word documents that may be later difficult to edit manually (if that is an interesting option).

So if you want to use div tags as paragraph tags, you need to use the parseDivs option (or change them to p tags):

$html = '<div style="padding: 30px 40px; background-color: yellow">Test docx</div>';
$docx->embedHTML($html, array('parseDivs' => 'paragraph'));

$html = '<p style="padding: 30px 40px; background-color: yellow">Test docx</p>';
$docx->embedHTML($html);

Also please note that not all CSS styles have a direct translation to MS Word. For example, MS Word doesn't allow setting paddings to run-of-text contents (such as inline texts), only to block elements such as paragraphs or tables; to add spaces to run-of-text tags you'd need to add blank spaces as MS Word does (or tabs but these elements only supported in Premium licenses when transforming HTML).

Regards.