首页 > 解决方案 > 使用 docx4j 将 HTML 字符串转换为 docx 图像未嵌入 docx

问题描述

我有带有图像 base64 的 HTML 字符串,想转换成 DOCX。我正在使用 AlternativeFormatInputPart 进行转换。如果我使用 XHTMLImporterImpl 那么我在 HTML 中面临格式化问题。

String html = "<html><body><p>Hello Docx4j</p> <p> <img src="data:image/png;base64,iVBORw0KGgoAAA..." alt=""/> </p> </body></html>";
htmlToDocx(html, wordMLPackage);

private void htmlToDocx(String htmlString, WordprocessingMLPackage wordMLPackage) throws Exception {
        AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
        afiPart.setContentType(new ContentType("text/html"));
        afiPart.setBinaryData(htmlString.getBytes());
        Relationship altChunkRel = documentPart.addTargetPart(afiPart);
        CTAltChunk chunk = factory.createCTAltChunk();
        chunk.setId(altChunkRel.getId());
        documentPart.addObject(chunk);
    }

它在没有图像的情况下进行转换,如果我将提供图像路径而不是 base64,那么它将起作用,但我有多个图像在服务器(linux)上动态出现。如何使用 AlternativeFormatInputPart 嵌入图像。

请帮我添加base64图像。

提前致谢!

标签: javaspringdocxdocx4jdoc

解决方案


推荐阅读