使用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);
        }
    }
}
相关推荐
玩泥巴的21 小时前
.NET驾驭Word之力:COM组件二次开发全攻略之连接Word与创建你的第一个自动化文档
word·二次开发·com互操作
辣香牛肉面2 天前
[Windows] 搜索文本2.6.2(从word、wps、excel、pdf和txt文件中查找文本的工具)
word·excel·wps·搜索文本
FlYFlOWERANDLEAF3 天前
DevExpress中Word Processing Document API学习记录
学习·c#·word
AnySpaceOne3 天前
PDF转Word在线转换教程:多种实用方法分享
学习·pdf·word
我命由我123455 天前
Word - Word 的 5 种视图(页面视图、阅读视图、Web 版式视图、大纲视图、草稿视图)
ui·word·excel·photoshop·表格·ps·美工
XYZLHL5 天前
Word怎么设置页码总页数不包含封面和目录页
word
传而习乎5 天前
Word添加图/表题注
word
YAY_tyy5 天前
基于 Vue3 + VueOffice 的多格式文档预览组件实现(支持 PDF/Word/Excel/PPT)
前端·javascript·vue.js·pdf·word·excel
东风西巷5 天前
Atlantis Word Processor:全方位的文字处理专家
前端·学习·word·软件需求