Skip to content

Conversation

@francoVV02
Copy link

@francoVV02 francoVV02 commented Aug 8, 2025

Problem

When generating ODT documents with styled text (bold, italic, underline, etc.) inside headers, the ODTDocumentHandler incorrectly creates paragraph elements (<text:p>) inside header elements (<text:h>). This violates the ODT specification where both headers and paragraphs are block-level elements that cannot be nested.

Current Invalid Output

<text:h text:style-name="Heading_20_1" text:outline-level="1">
    <text:p><text:span text:style-name="XDocReport_Bold">Bold Header Text</text:span>
</text:h>
<!-- Missing </text:p> closing tag -->

Excpected Valid Output

<text:h text:style-name="Heading_20_1" text:outline-level="1">
    <text:span text:style-name="XDocReport_Bold">Bold Header Text</text:span>
</text:h>

Errors Caused

SAX Parser Exception when parsing the generated ODT:

org.xml.sax.SAXParseException: The element type "text:p" must be terminated by the matching end-tag "</text:p>"

ClassCastException during PDF conversion:

java.lang.ClassCastException: Insertion of illegal Element: 12
at com.lowagie.text.Phrase.add(Phrase.java:301)

Solution

Modified startParagraphIfNeeded in ODTDocumentHandler.java:

  • Added check for insideHeader flag
  • Prevents paragraph creation when inside header elements

Fixes #685

Added an early return to startParagraphIfNeeded if we are currently inside a header.
@francoVV02 francoVV02 marked this pull request as ready for review August 8, 2025 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error using ODT and HTML styling to generate PDF

1 participant