使用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);
        }
    }
}
相关推荐
pingan87871 天前
试试 docx.js 一键生成 Word 文档,效果很不错
开发语言·前端·javascript·ecmascript·word
封印师请假去地球钓鱼1 天前
问题解决|word中单独一页设置横向
word
小小算法师1 天前
分节符、分页符、分栏符,一次性讲明白
word
封印师请假去地球钓鱼1 天前
问题解决|Word大纲层级混乱?在word导航窗格中调整目录缩进
word
汽车芯猿1 天前
Word频繁崩溃?一步解决 Office 加载项冲突
word
东小黑1 天前
一些论文word格式
word
T0uken1 天前
【Python】docxnote:优雅的 Word 批注
开发语言·python·word
热爱生活的五柒2 天前
word中如何一键修改英文字母数字为新罗马字体Times New Roman
word·西文·times new roman
伟贤AI之路2 天前
Markdown写彩色文字+公式,MD2WD直接转Word,保留所有格式
word·markdown
bu_shuo2 天前
Mathtype的公式在word中显示不全的问题
word·学位论文·mathtype·论文格式