使用docx4j+docx4j-ImportXHTML实现将html转成word

使用docx4j+docx4j-ImportXHTML实现将html转成word

1.依赖

复制代码
        <!--docx4j + docx4j-ImportXHTML实现html转word -->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>6.1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-reload4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-ImportXHTML</artifactId>
            <version>8.3.11</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.1</version>
        </dependency>

2.测试代码

前置条件:

复制代码
String htmlContent = pdfService.generateHtmlContent();

这个是得到填充后的模板的内容!!!

复制代码
import com.example.pdfhtml.util.PdfService;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;

@RestController
public class HtmlToWordController {
    @Autowired
    private PdfService pdfService;

    @GetMapping("/html-to-word")
    public ResponseEntity<byte[]> convertHtmlToWord() {
        try {
            // 创建 WordprocessingMLPackage 实例
            WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
            MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
            String htmlContent = pdfService.generateHtmlContent();
            // 使用 XHTMLImporterImpl 将 HTML 导入到 Word 文档中
            XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordPackage);
            mainDocumentPart.getContent().addAll(xhtmlImporter.convert(htmlContent, null));

            // 将 Word 文档写入 ByteArrayOutputStream
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            wordPackage.save(outputStream);

            // 设置响应头,指定文件名和内容类型
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "document.docx");
            return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
相关推荐
qq_294940191012 小时前
WORD个人简历单页326款模版分享下载
word·个人简历·doc模版
JOYUAGV12 小时前
Word压缩解决方案
python·word
开开心心就好19 小时前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
wqqqianqian2 天前
国产linux系统(银河麒麟,统信uos)使用 PageOffice自定义Word模版中的数据区域
linux·word·自定义·pageoffice·数据区域
CodeCraft Studio3 天前
国产化Word处理控件Spire.Doc教程:通过C# 删除 Word 文档中的超链接
开发语言·c#·word
小小Ruby3 天前
利用vba替换word中多个表格,相邻单元格的文字
word
T0uken4 天前
【前端】:单 HTML 去除 Word 批注
前端·html·word
诸葛大钢铁4 天前
WORD压缩两个免费方法
word
KingCruel4 天前
NPOI 操作 Word 文档
word
小可的科研日常5 天前
保持Word中插入图片的清晰度
word