前言
以前写过一篇 freemarker导出word 的文章,然后在文章最后我说有空写个转pdf的,然后一直没写(其实我以为我写过了😂)。
好久都没用过 freemarker 了,用法都忘得差不多了,以前的那些模板代码也不知道什么时候被我给删掉了,现在重新写都是参考我之前的文章(这就是写博客的好处,不止是分享,还有记录😎)。
所以一些freemarker 的具体用法我这里就不再重复了,有兴趣的可以参考下我之前的文章:
freemarker导出word,带表格和多张图片,解决图片重复和变形
freemarker合并单元格,if、else标签的使用,null、空字符串处理
因为word转pdf,需要一个叫 aspose-words 的包,mvn依赖仓库 中的是很旧的版本了,所以我在网上找了个 16.8.0 版本的。如果引入外部的第三方jar包,项目打包的时候,外部的第三方jar包没有一起打包进去,转pdf的时候无法加载,就会报错。那么如何将外部jar一起打包或者是将外部jar转成本地maven仓库依赖,可以参考下面这篇文章:
springboot打包成jar运行无法访问resources下的资源,以及jar包运行时引用的第三方jar包也无法加载
需求
1、通过freemarker模板,导出word文档,同时可将word转为pdf。
2、导出的word带图片,如果图片太大,可通过等比缩放解决图片尺寸变小后变形的问题。
3、导出时,将文档里面的图片作为单独的附件一起下载下来;或者是还有其他文件需要和文档一起下载。(这一点也可以忽略😊)
准备
😁 1、aspose-words16.8.0.jar 包。
😀 2、word转pdf需要的验证文件:license.xml(不验证转化出的pdf会有水印)。
😋 3、simsun.ttc 字体文件(Linux要读取字体,否则pdf字体为方格)。
🥰 4、word模板。
😉 5、将word模板另存为xml文件,将后缀名改为 ftl。
用到的这些我都放到文章结尾了(^∀^●)ノシ。
效果😎
1、我们先来看看模板是什么样的Ψ( ̄∀ ̄)Ψ







2、再来看看导出效果 (。・∀・)ノ



实现😏
现在我们开始用代码实现,既然是用freemarker,那肯定得引入依赖:
<!-- word生成工具类 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<!--pdf生成工具类-->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
<!--引入word转pdf jar包-->
<dependency>
<groupId>aspose</groupId>
<artifactId>words</artifactId>
<version>16.8.0</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
然后我们把我上面说的要准备的几个文件分别放到resources目录下的static 和 template目录下:

之后我们写个工具类,导出成word和word转pdf:
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.net.MalformedURLException;
import java.util.Map;
/**
* word、pdf处理工具类
*/
public class WordPDFUtil {
protected static Logger logger = LoggerFactory.getLogger(WordPDFUtil.class);
/**
* 获取破解码文件内容
*/
public static boolean getLicense() throws IOException {
boolean result = false;
InputStream is = null;
try {
// license.xml应放在..\WebRoot\WEB-INF\classes路径下
is = WordPDFUtil.class.getClassLoader().getResourceAsStream("static/license.xml");
License license = new License();
license.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != is) is.close();
}
return result;
}
/**
* 通过模板导出word格式文件
*
* @param dataMap 导出数据
* @param templateName 模板名称
* @param path 导出word的路径以及文件名称
*/
public static void exportWord(Map<String, Object> dataMap, String templateName, String path) {
try {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return;
}
//Configuration 用于读取ftl文件
Configuration configuration = new Configuration(new Version("2.3.0"));
configuration.setDefaultEncoding("utf-8");
//指定路径(根据某个类的相对路径指定)
configuration.setClassForTemplateLoading(WordPDFUtil.class, "/template");
//输出文档路径及名称
File outFile = new File(path);
FileOutputStream os = new<

本文介绍如何利用Freemarker模板引擎生成Word文档,并进一步将其转换为PDF格式。文章涵盖解决图片过大导致的变形问题、图片按比例缩放的方法以及如何将Word文档内的图片作为附件一同下载。
最低0.47元/天 解锁文章
4520

被折叠的 条评论
为什么被折叠?



