plutext / docx4j-ImportXHTML

Converts XHTML to OpenXML WordML (docx) using docx4j
135 stars 124 forks source link

Generated Docx is corrupted #85

Open ChristophKronberger opened 2 years ago

ChristophKronberger commented 2 years ago

I tried to parse a xhtml file into a word document. i use Spring Boot 2.6.7 with following maven dependencys:

    `<dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-ImportXHTML</artifactId>
            <version>11.4.6</version>
        </dependency>
<dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
            <version>11.4.6</version>
        </dependency>
   <!-- API, java.xml.bind module -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>4.0.0</version>
        </dependency>

        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>jakarta.activation</groupId>
            <artifactId>jakarta.activation-api</artifactId>
            <version>2.1.0</version>
        </dependency>`

Heres my Code. 
       `String htmlFilePath = "src/main/resources/templates/template.xhtml";
        File file = new File("src/main/resources/templates");
        String baseURL = file.toURI().toString();
       String stringFromFile = FileUtils.readFileToString(new File(htmlFilePath), "UTF-8");

      WordprocessingMLPackage docxOut = WordprocessingMLPackage.createPackage();
     NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
      docxOut.getMainDocumentPart().addTargetPart(ndp);
      ndp.unmarshalDefaultNumbering();

      XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(docxOut);
      XHTMLImporter.setHyperlinkStyle(null);

      docxOut.getMainDocumentPart().getContent().addAll(
              XHTMLImporter.convert(stringFromFile, baseURL) );

      docxOut.save(new java.io.File("src/main/resources/output" + "/DocxToXhtmlAndBack.docx") );`