Java使用freemarker生成word文档后转pdf

  • 先来看看效果图
  • 进入正题

项目需求: 为订单后生成对应的pdf文档,文档内包含图片。
方案一:使用freemarker和itext把html转pdf,存在中文乱码,宋体不识别,需下载simsun.ttc字体,空白占位符不识别等等问题,对前端依赖性较大,不推荐;
方案二:freemarker生成word文档后通过微软提供的openoffice工具转pdf文档;
方案三: freemarker生成word文档后通过aspose.words转pdf文档,该方式不需要安装插件,只需导入对应jar包就好,但生成pdf存在水印,需要进行破解;(aspose.words需要付费的哦,用于商业请授权购买正版)
方案四:python用pdfkit生成pdf,各位有时间自己研究吧;

先来看看效果图

进入正题

1.生成pdf文档流程
1):准备一个word表格,然后另存为xml格式的文件;
2):为了便于观看文档结构和编辑代码,对xml文件进行格式化(在线格式化网址);
3):把xml文件的后缀名改为ftl,使用freemarker表达式进行数据的动态添加(模板中图片是以base64编码展示的,替换成自己的图片编码);
4):导入相关jar包;
5):通过freemarker模板引擎技术把ftl模板文件转为word文档;
6):通过aspose.words把word文档转为pdf文档,至此完成;

2.相关jar包准备

    <!--freemarker jar--><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.22</version></dependency><!--二维码操作jar--><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.0.0</version></dependency><!-- https://mvnrepository.com/artifact/com.aspose/aspose-words --><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>14.7.0</version></dependency>

因为我们用的破解版,所以请点击此处下载我给各位准备的aspose-words.jar和ftl模板( 提取码: jrs5)

2.开始我们的代码

文档生成工具类Xml2Word2Pdf.java

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;/*** @Author: zzr-zhangs* @Date: 2020-01-14 13:12* @Content: xml->word->pdf (通过freemarker把通过word生成的xml模板动态添加数据并生成word文档,通过aspose.words把word文档转为pdf文档)* @Description: aspose.words需付费购买,目前使用破解版,需要引入freemarker的jar和aspose-words-jdk16-16.4.0.jar*/
public class Xml2Word2Pdf {private static Configuration configuration = null;/*** 初始化配置并设置默认编码UTF-8*/static {configuration = new Configuration();configuration.setDefaultEncoding("UTF-8");}/*** 通过模板文件创建word文档(模板文件的格式可到网站进行在线格式化,网址推荐:https://tool.oschina.net/codeformat/xml/)** @param tpmplateFilePath 模板文件路径(完整路径,不包含文件如:D:/templates)* @param tpmplateFileName 模板文件名称* @param outFilePath      输出文件路径(完整路径,包含文件名称 如:D:/templates/order.doc)* @param dataMap          需要动态添加的数据*/public static void createWord(String tpmplateFilePath, String tpmplateFileName, String outFilePath, Map<String, Object> dataMap) {try {//如果不传模板文件路径就默认取resources下的templates文件夹中的模板文件if (StringUtils.isEmpty(tpmplateFilePath)) {configuration.setClassForTemplateLoading(Xml2Word2Pdf.class, "/templates");} else {configuration.setDirectoryForTemplateLoading(new File(tpmplateFilePath)); // XML文件所存在的位置}//获取文档XML模板Template template = configuration.getTemplate(tpmplateFileName);//设置输出文件位置和文件名File outFile = new File(outFilePath);Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));template.process(dataMap, out);out.close();} catch (Exception e) {e.printStackTrace();}}/*** @Description: 验证aspose.word组件是否授权:无授权的文件有水印标记*/public static boolean getLicense() {boolean result = false;try {String s = "<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>";ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes());//InputStream inputStream = Xml2Word2Pdf.class.getClassLoader().getResourceAsStream("\\license.xml");com.aspose.words.License license = new com.aspose.words.License();license.setLicense(inputStream);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 使用aspose.word把word文档转为pdf文档** @param sourceFile word文档绝对路径(如:D:/templates/order.doc)* @param destFile   pdf文档绝对路径(如:D:/templates/order.pdf)*/public static String word2Pdf(String sourceFile, String destFile) throws Exception {destFile = StringUtils.isEmpty(destFile) ? sourceFile.replace(".doc", ".pdf") : destFile;// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {throw new Exception("生成PDF文档,验证License失败!");}try {File file = new File(destFile);  //新建一个空白pdf文档FileOutputStream os = new FileOutputStream(file);Document doc = new Document(sourceFile);//通过sourceFile创建word文档对象doc.save(os, SaveFormat.PDF);os.close();} catch (Exception e) {e.printStackTrace();throw new Exception("生成PDF文档失败!");}return destFile;}/*** 图片转base64编码** @param imgFilePath 图片路径* @return 返回base64编码字符串*/public static String ima2Base64(String imgFilePath) {InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgFilePath);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);}/*** 简单的测试** @remark 需要在D盘创建templates文件夹并准备相应的的xml和图片*/public static void main(String[] args) throws Exception {long start = System.currentTimeMillis();//1.基础数据定义String templatePath = "D:/templates/";//模板路径/生成文档存放路径String sourceFile = templatePath+"order.doc";//生成的word文档路径和名称String templateName = "word_2003.ftl";//模板名称String sealImgPath = "D:/templates/liangzai.png";//盖章图片路径String nameImgPath = "D:/templates/zhangjie.jpg";//名字图片路径//2. dataMap的赋值Map<String, Object> dataMap = new HashMap<>();dataMap.put("orderNo", "ZRP202001150004");dataMap.put("sealImgBase64", ima2Base64(sealImgPath));dataMap.put("nameImgBase64", ima2Base64(nameImgPath));//生成word文档createWord(templatePath, templateName, sourceFile, dataMap);//word文档转pdf文档word2Pdf(sourceFile, null);long end = System.currentTimeMillis();System.out.println("文档生成耗时:" + ((end - start) / 1000.0) + "秒"); }
}

参考博客:https://blog.csdn.net/qq_41507845/article/details/90518846

Java使用freemarker生成word文档并转pdf文档相关推荐

  1. Java 用 Freemarker 生成 Word 时内容换行

    一.原因 试过各种换行符 '\r' '\n' '\r\n' '(char)11' 全部都不行!!! 原因是:Java 用 Freemarker 生成 Word 时的换行符被自动解析忽略掉 二.解决方法 ...

  2. Java使用freemarker生成word文件

    首先声明我的项目是一个web项目,生成的word文件直接通过response响应发送给前端.如果不是web项目的话可以像网上的其他教程一样将生成的word保存在本地. 要利用freemarker生成w ...

  3. Java通过freemarker生成word文档

    文档生成目录 摘要 项目预期效果 使用freemaker生成word文档并下载 一:导入所需要的maven依赖 二:根据word文档生成我们需要的ftl模板文件 三:将word需要的数据存入一个map ...

  4. java使用freemarker生成word

    一.前端生成base64编码 npm install echarts --save 到vue的main.js中加入: import echarts from 'echarts'Vue.prototyp ...

  5. Java实现freemarker生成word模板

    博主在做的是将word源文件中的内容读取出来,然后按照固定的格式输出,当然,源文件有大量,所以编写了程序来批量自动生成目标文件. 本文要介绍的就是如何生成模板,并将从源文件截取的内容填充到模板文件(即 ...

  6. java根据 freemarker 生成word文档包含图片和动态表格

    需求 根据提供的模板样式,生成相对应的word文档,之前可以用xdoc生成,但是目前这个需求 是需要动态生成excel表格,有的单元格需要隐藏不展示,所以这边利用freemarker标签解析, 根据数 ...

  7. Java通过POI或Freemarker生成word文档,使用Jfreechart创建统计图表

    最近做了一个使用Java生成统计分析报告word文档的功能,有提前制作好的word文档,其中共包含了普通文本变量,普通表格,动态表格.统计图表(柱状图.饼状图.折线图等),在此记录下POI和freem ...

  8. spring使用freemarker生成word文档包含表格、图片(循环插入)

    spring使用freemarker生成word文档包含表格.图片(循环插入) 效果图 因为测试数据是重复的,所以显示都是重复的数据,替换导入map中的数据可以显示不重复的数据. 操作步骤 1,创建一 ...

  9. 用freemarker生成word文档,并插入图片

    用freemarker生成word文档,并插入图片 最近需要做一个问卷功能,要求用户填写完问卷后,后台会生成一个word文档,将用户提交的数据插入到word中. 创建word模板 新建一个word文档 ...

最新文章

  1. 通过7个函数解密区块链(附代码)
  2. linux grouplist groupinstall groupremove 简介
  3. 僵尸进程的生成以及几种避免方法
  4. 【Python-ML】SKlearn库线性回归器LinearRegression
  5. GridControl详解(八)菜单
  6. eclipse html自动对齐,MyEclipse和Eclipse中jsp、html格式化自动排版问题
  7. large_margin
  8. html5,css3, bootstraps
  9. 【DVRP】基于matlab蚁群算法求解带距离的VRP问题【含Matlab源码 1040期】
  10. 专题页汇聚seo流量的葵花宝典
  11. Windows 10 开启代理软件代理流量之后,系统的某些自带软件无法联网
  12. IE11 js导出excel提示Automation 服务器不能创建对象
  13. 基线检查工具Python代码
  14. 连续翻页浏览器面临的共同问题
  15. 2022江西省职业院校技能大赛春季赛网络安全赛项样题
  16. 离散数学-数理逻辑知识整理(修改版)
  17. 解决java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false
  18. matlab if嵌套函数,MATLAB嵌套函数的应用
  19. java校招笔试题目_Java校招笔试题
  20. 【国家局发布】医疗器械注册流程及相关法规大全

热门文章

  1. 在微信公众号中使用jquery和微信SDK
  2. php word权限设置密码,在php中加密和解密word docx文件的问题
  3. 内存耗用:VSS/RSS/PSS/USS 的介绍
  4. biosrecovery什么意思_recovery是什么意思
  5. GitHub 自动下载 Release 固件
  6. 【xlwings api语言参考】Range.VerticalAlignment 属性
  7. 三大运营商物联卡哪家网络稳定
  8. 阿里云服务器(Windows)使用IE浏览器打开网站提示“增强安全配置正在阻止来自下列网站内容”如何处理?
  9. 美国商务旅游白本面签经历分享
  10. orale数据库的SQL查询