easypoi+docx4j+wkhtmltopdf生成带有富文本编辑内容的word和pdf

大家好,我是“Java分布式架构实战”的作者Jamesfu。

需求背景

最近在做一个项目,需要将CMS中的内容动态地生成word文档和pdf文档。

word模板

Word模板主要包括页眉、页脚、正文。正文中又分为标题、题注、富文本内容。

word模板

  1. 经过调研、测试发现,Easypoi能较好地满足模板化生成。但是没有发现如何插入富文本内容。
  2. 后来发现docx4j能够插入富文本内容。

使用Easypoi解析word模板

//构造规章文档生成参数
String notes = "";
if (StringUtils.isNotBlank(articleInsertVo.getNotes())) {
    notes = "(" + articleInsertVo.getNotes() + ")";
}
Map<String, String> params = new HashMap<>();
params.put("title", articleInsertVo.getTitle());
params.put("notes", notes);
params.put("content", articleInsertVo.getContent());

XWPFDocument doc = WordExportUtil.exportWord07(templatePath, data)
FileOutputStream fos = new FileOutputStream(outputFile)
doc.write(fos);
fos.flush();
fos.close();

使用docx4j插入富文本内容

### 对html进行标准化处理并增加字符集设置
Document document = org.jsoup.Jsoup.parse(htmlContent);
document.head().prepend("<meta charset=\"utf-8\"/>");
String normalizedHtmlContent = document.html();

### 将标准化后的html内容插入word文件
WordprocessingMLPackage aPackage = WordprocessingMLPackage.load(outputFile);
MainDocumentPart mainDocumentPart = aPackage.getMainDocumentPart();
mainDocumentPart.addAltChunk(AltChunkType.Html, normalizedHtmlContent.getBytes(Charsets.UTF_8));
aPackage.save(outputFile);

将本地文件outputFile上传到OSS

### 将本地文件转换成MultipartFile后,执行上传
private MultipartFile fileToMultipartFile(File localFile) throws IOException {
    FileItem fileItem = new DiskFileItem("file",
            Files.probeContentType(localFile.toPath()),
            false, localFile.getName(),
            (int) localFile.length(),
            localFile.getParentFile());
    MultipartFile multipartFile;
    try (InputStream input = new FileInputStream(localFile); OutputStream os = fileItem.getOutputStream()) {
        IOUtils.copy(input, os);
        multipartFile = new CommonsMultipartFile(fileItem);
        return multipartFile;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

最终效果

最终,通过了Windows,MacOS上的Microsoft Word\WPS测试。

遇到的问题

  1. 仅使用Easypoi生成的word文档如下

easypoi无法插入富文本内容

  1. 使用docx4j向word文件中追加html网页内容时显示html标签
    经过排查发现html内容不规范,缺少html/head/body标签,可以通过Jsoup进行标准化。

Document document = org.jsoup.Jsoup.parse(htmlContent);
document.head().prepend("<meta charset=\"utf-8\"/>");
String normalizedHtmlContent = document.html();

富文本内容缺少html根元素,导致没有解析成页面。

  1. 使用docx4j向word文件中追加html网页内容时显示乱码
    经过排查发现html内容head中缺少meta标签:<meta charset="utf-8"/>

    导出的word中html页面内容乱码

总结

本项目的难点在于正文是富文本编辑器产生的一段html内容,最终通过Easypoi和docx4j组合来生成word文档。在测试过程中,本来想用word转pdf,经过测试发现docx4j转pdf不能正常处理页眉、页脚和html内容部分。后来发现可以考虑用wkhtmltopdf来生成pdf。

/usr/local/bin/wkhtmltopdf \
--enable-local-file-access \
--header-html file:///Users/jamesfu/data/temp/ruleArticle/header.html \
--footer-html file:///Users/jamesfu/data/temp/ruleArticle/footer.html \
/Users/jamesfu/data/temp/ruleArticle/gz.html \
/Users/jamesfu/data/temp/ruleArticle/gz.pdf

wkhtmltopdf转换出来的页面乱码

看到这个结果我还是挺兴奋的,wkhtmltopdf帮助我们打印出来漂亮的页眉、页脚和内容,只是出现了乱码而已。此乱码问题,应该是文件乱码导致的。通过Visual Studio Code查看文件发现是UTF-8编码.

vscode打开本地文件时默认以UTF-8编码展示

那为什么还会打印出乱码呢?结果发现文件本身的编码是ascii

我点击右下角的编码,弹出菜单,选择“Save with Encoding”, 文件重新保存为“UTF-8”。

将文件编码修改为UTF-8

文件编码调整为“UTF-8”后,重新打印为pdf文件,一切正常了,接下来还需要研究书签和目录。

最终乱码问题解决,页眉、页脚也生成了,只是还需要再调整一下页眉和页脚的布局,正文的间距等细节。

先简单写到这里吧,这一周为这个事费了不少精力。

参考资料

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值