PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Importing HTML Headings inserts text, not heading and export to html wrong elements

Open constip opened this issue 5 years ago • 5 comments

When importing from HTML via \PhpOffice\PhpWord\Shared\Html::addHtml, all heading-elements are handled by parseHeading as normal text with the extra styles paragraph as Heading1,2,.... This should be handled by the usual addTitle.

The above seems to work fine when exporting to Word2007, however when exporting to html, addTitle produced a h1,h2,...-element, while the former produces <p class="Heading1,2,...">, which is a problem, because the styles for the headings don't apply.

Also, when exporting to html, a title (depth 0) is written as h0, while the styles are applied to the element Title.

Finally, when exporting to html, all texts get style="margin-top: 0; margin-bottom: 0;". This should not be there. They also don't get the Normal-class, meaning paragraph styles don't get applied.

This is a minor thing, but please also consider instead of * {font-family: Arial; font-size: 10pt;} only applying this to body. This way, the font is initialized and all other elements inherit it instead of re-setting it every time. This causes problems when for example using a span inside a h1 (the font-size is wrong).

constip avatar Aug 13 '19 12:08 constip

On an unrelated note, I would suggest using $doc->loadHTML( ... ) instead of loadXML in PhpOffice\PhpWord\Shared\Html addHtml. HTML is more often than not not valid XHTML which only causes problems. Adding libxml_use_internal_errors(true); would complete that.

constip avatar Aug 13 '19 13:08 constip

I have a fix for this (see previous references), but waiting on #1669 to be merged so I can get it in. (May be a bit; it's a large PR and merges seem to be delayed by a couple weeks.)

0b10011 avatar Aug 30 '19 15:08 0b10011

any update ??

the error always occurs for me

BenaddiRar avatar Sep 15 '20 13:09 BenaddiRar

This is still an issue with headings, and the fixes in the commits from @0b10011 seem to work. Any chance to get those in?

jhedstrom avatar Dec 14 '22 18:12 jhedstrom

Cant believe this is still an issue, 4 years later

JoppeDC avatar Dec 21 '23 20:12 JoppeDC