添加依赖:

        <!-- freemarker生成word文件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><dependency><groupId>com.aspose.cells</groupId><artifactId>aspose-cells</artifactId><version>20.4 - c</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-20.4 - c.jar</systemPath></dependency><dependency><groupId>com.aspose.words</groupId><artifactId>aspose-words</artifactId><version>words-18.10-jdk16</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/lib/aspose-words-18.10-jdk16.jar</systemPath></dependency>

freemarker生成WORD的工具类

package com.vxdata.pdf.aspose.utils;import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;@Component
public class WordUtil {/*** 存放模板的目录*/@Value("${word2pdf.templatePath}")private String templatePath;/*** 生成 word 文档方法** @param dataMap      要填充的数据* @param templateName 模版名称.ftl (只有名称)* @param fileName     要输出的文件路径 xxx/xxx/xxx.doc* @throws Exception 抛出的异常*/public void createWord(Map<String, Object> dataMap, String templateName, String fileName) throws Exception {// 设置FreeMarker的版本和编码格式Configuration configuration = new Configuration(new Version("2.3.28"));configuration.setDefaultEncoding("UTF-8");// 设置FreeMarker生成Word文档所需要的模板的路径configuration.setDirectoryForTemplateLoading(new File(templatePath));// 此处把模版文件都放在 resources 下的 templates 中// configuration.setClassForTemplateLoading(WordUtils.class, "/templates");// 设置FreeMarker生成Word文档所需要的模板Template tem = configuration.getTemplate(templateName, "UTF-8");// 创建一个Word文档的输出流Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), StandardCharsets.UTF_8));// FreeMarker使用Word模板和数据生成Word文档tem.process(dataMap, out);out.flush();out.close();}
}

注意点

生成图片: 需要base64格式, 先用图片占位, 调整好格式, 然后把图片的base64换成占位符
生成复选框: 整体替换, 不要一个个替换

aspose 生成PDF的工具类

Jar包:

链接:https://pan.baidu.com/s/1YKXbt13w-Bu6-h1TjFTX9A
提取码:ci5q

破解文件: license.xml

aspose需要破解, 破解文件要放在resources下

<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
package com.vxdata.pdf.aspose.utils;import com.aspose.words.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;import java.io.FileOutputStream;
import java.io.InputStream;/*** 使用Aspose将word转成pdf** @author sunhongyun*/
@Slf4j
@Component
public class Word2PdfUtil {/*** 破解方法* 需要jar包: aspose-words-15.12.0-jdk16.jar* 同时配置 license.xml** @return*/private static boolean getLicense() {boolean result = false;try {InputStream is = new ClassPathResource("/license.xml").getInputStream();License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** word 转 pdf** @param inputPath word文件path(全路径)* @param outPath   pdf文件path(全路径)* @return*/public Boolean word2pdf(String inputPath, String outPath) {try (FileOutputStream os = new FileOutputStream(outPath)) {if (getLicense()) {Document doc = new Document(inputPath);doc.save(os, SaveFormat.PDF);return true;}log.error("转换失败!", "破解失败");return false;} catch (Exception e) {log.error("转换失败!", e);return false;}}
}

调用工具类

package com.vxdata.pdf.aspose.service.impl;import com.vxdata.pdf.aspose.service.IPdfService;
import com.vxdata.pdf.aspose.utils.Word2PdfUtil;
import com.vxdata.pdf.aspose.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
import java.util.UUID;@Component
public class PdfServiceImpl implements IPdfService {@Autowiredprivate WordUtil wordUtil;@Autowiredprivate Word2PdfUtil word2PdfUtil;@Value("${word2pdf.tmpPath}")private String tmpPath;/*** 生成pdf** @param map              数据* @param response         返回流* @param templateFileName 模板名称*/@Overridepublic void word2pdf(Map<String, Object> map, HttpServletResponse response, String templateFileName) {long start = System.currentTimeMillis();try {// word与pdf的随机文件名String tmpFile = tmpPath + File.separator + UUID.randomUUID().toString();String wordFileName = tmpFile + ".docx";String pdfFileName = tmpFile + ".pdf";// 生成wordwordUtil.createWord(map, templateFileName, wordFileName);// 生成pdfBoolean flag = word2PdfUtil.word2pdf(wordFileName, pdfFileName);if (flag) {byte[] buf = new byte[8 * 1024];int len = 0;try (InputStream is = new FileInputStream(pdfFileName); OutputStream os = response.getOutputStream();) {while ((len = is.read(buf, 0, buf.length)) != -1) {os.write(buf, 0, len);}os.flush();} finally {try {deleteFile(wordFileName);deleteFile(pdfFileName);} catch (Exception e) {}}}} catch (Exception e1) {e1.printStackTrace();}long end = System.currentTimeMillis();System.out.println("pdf转换成功,共耗时:" + +(end - start) / 1000.0);}/*** 删除文件** @param filePath 文件* @return*/public static boolean deleteFile(String filePath) {boolean flag = false;File file = new File(filePath);// 路径为文件且不为空则进行删除if (file.isFile() && file.exists()) {file.delete();flag = true;}return flag;}
}

java 使用 freemarker模板 生成 word 并用 aspose 转换成PDF相关推荐

  1. word 模板生成ftl并实现转换成pdf

    最近在处理doc转换成pdf的时候碰到个比较奇葩的问题,正常的doc文件转换成pdf,使用openoffice,poi,itextpdf,都能进行转换 但是,我需要转换的doc是一个word模板类型的 ...

  2. java利用Freemarker模板生成docx格式的word文档(全过程)

    参考汇总: wordexport: JAVA生成并导出Word文档技术论证 java利用Freemarker模板生成docx格式的word文档(全过程) - 旁光 - 博客园 # 参考资料 - 其他项 ...

  3. Java使用FreeMarker自动生成Word文档(带图片和表单)

    Java使用FreeMarker自动生成Word文档(带图片和表单) 1 背景 2 目标效果 3 创建Word模板 3.1 创建模板文档 3.2 转换模板文档 3.3 处理模板文档中的占位符 3.4 ...

  4. 利用 freemarker 模板生成 word 小结

    在企业级开发时,不可避免的会遇到生成 word 文档的需求,有两种常用的方案,1.使用 Apache POI 在后台通过代码生成 word 文档:2.使用模板生成 word 文档.第二种方法比较简单, ...

  5. java将WORD文档转换成pdf文件

    总结对jacob和Itext学习总结.本文试验的是将WORD转换成PDF文件. 实现思路 一.先将WORD文档转换成HMTL文件格式(参阅我的前一文<JAVA操作WORD文档). 二.用流读取H ...

  6. WORD文档转换成PDF格式

    由于一个客户的项目中需要将WORD文档转换成PDF格式,实战教程如下: 需求分析:客户的项目以B/S结构为主,提供一个WORD文件在后台自动转换成PDF,经过实际 测试, 如果该篇WORD文档有100 ...

  7. 怎么把word文档转换成PDF?

    Word文件完成编辑之后,想要转发给他人,但是担心在转发过程中出现了格式错乱的情况,将word文档转换成PDF格式再转发就可以避免类似情况了.那么如何将word文档转换成PDF文件? 方法一: 在编辑 ...

  8. Java freemarker 模板生成word动态表格

    1.新建一个word文档 2.把调整完的word另存为xml格式: 3.使用文本编辑器打开 4.xml格式化 XML 在线格式化 | 菜鸟工具菜鸟工具-XML 在线格式化..https://c.run ...

  9. java生成word 带表格_【java】Freemarker 动态生成word(带图片表格)

    1.添加freemarker.jar 到java项目. 2.新建word文档. 3.将文档另存为xml 格式. 4.将xml格式化后打开编辑(最好用notepad,有格式),找到需要替换的内容,将内容 ...

最新文章

  1. C++ Primer 5th笔记(chap 19 特殊工具与技术)定位 new 表达式
  2. Jenkins拾遗--第三篇(用户权限管理)
  3. SpringCloud Gateway的组成结构
  4. 基于LAMP实现web日志管理查看
  5. ECC密钥结构和密码学基础
  6. WPF 正確理解ContentPresenter
  7. ×××linux下vsftp服务器
  8. mysql+缓冲池脏块率高_什么是数据库的 “缓存池” ?(万字干货)
  9. Flask安装首页显示
  10. 关于 asp.net 服务器控件几个 ID 的说明
  11. Ubuntu 18.04LTS系统设置窗口打不开或者消失解决办法
  12. 蓝桥杯比赛的RTC时钟配置
  13. linux/unix编程手册-6_10
  14. 在IIS上部署.net core的webapi项目 以及502.5错误的两种解决方法
  15. html (第四本书第九章参考)
  16. c语言字符程序示例,C语言程序设计实例大全
  17. python检测刀具_科研一角|Python语言在人工智能加工中心机器人方面的应用
  18. 搜狗微信公众号文章搜索器---网赚必备工具
  19. Shared_ptr循环引用解决(weak_ptr的作用)
  20. 华芯微特SWM260读写W25Q128

热门文章

  1. java作用域外调用对象,Java Web应用中往往通过设置不同作用域的属性来达到通讯的目的。 如果某个对象只在同一请求中共享,通过调用哪个类 的setAttribute方法设置属性。( )...
  2. 【Java Web】jsp与jspf文件的区别
  3. 【Linux操作系统——我的地盘我做主】
  4. WIN7下使用超级终端
  5. 亲测无限坐席在线客服系统源码/PHP在线客服系统源码
  6. 椒图科技发布免费服务器安全产品
  7. react常用ui组件库
  8. 同程旅游火车票部门面经
  9. 中国第一块区块链牌照
  10. 【AU】单链表就地逆置