java将数据库数据转换成word文档并且生成pdf文件最后转换成对应的图片
目的:将数据转换成word图片的方式展现给用户
工具:openoffice +java代码 +数据库,其中openoffice有windows版本的和Linux版本的。
其中openoffice的下载地址为:链接:https://pan.baidu.com/s/1Y5Ra3TfNCcvVX1bF-29hjw 密码:44ms
openoffice的安装及启动不在描述:网上自行百度。
下面主要是目的实现的步骤。
第一步:创建openoffice的连接池,使用linkedlist实现。简单的连接池,避免过多的消耗资源!
依赖的jar如下所以,部分jar无法下载,可以本地install 链接:https://pan.baidu.com/s/1wa65hYuxJ8wbIDETvuGDZQ 密码:pamx
<!--word转pdf以及pdf转图片使用的jar--><dependency><groupId>wisemax</groupId><artifactId>jodconverter</artifactId><version>2.2.2</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>ridl</artifactId><version>3.2.1</version></dependency><dependency><groupId>xstream</groupId><artifactId>xstream</artifactId><version>1.1.3</version></dependency><dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.2</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.3</version></dependency><dependency><groupId>com.artofsolving</groupId><artifactId>jodconverter</artifactId><version>2.2.1</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>jurt</artifactId><version>3.0.1</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>juh</artifactId><version>3.0.1</version></dependency><!--<dependency>--><!--<groupId>org.slf4j</groupId>--><!--<artifactId>slf4j-jdk14</artifactId>--><!--<version>1.5.6</version>--><!--</dependency>--><dependency><groupId>org.icepdf</groupId><artifactId>icepdf-core</artifactId><version>4.1.1</version></dependency><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.3.0</version></dependency><dependency><groupId>org.openoffice</groupId><artifactId>unoil</artifactId><version>3.2.1</version></dependency>
创建连接openoffice的类,模仿连接池的效果。
public class OpenSource {private String url = "127.0.0.1";private int port = 8100;private static int initCount = 10;//初始化连接数private static int maxCount = 15;//最大连接数private static int currentCount = 10;//当前连接数private static LinkedList<OpenOfficeConnection> connectionsPoll = new LinkedList<OpenOfficeConnection>();public OpenSource() {try {for (int i = 0; i <= initCount; i++) {//初始化生成10个数据库连接connectionsPoll.addLast(this.createConnection());}} catch (Exception e) {e.printStackTrace();}}private OpenOfficeConnection createConnection() throws Exception {return new SocketOpenOfficeConnection(url, port);}public void free(OpenOfficeConnection conn) {connectionsPoll.addLast(conn);//将连接放回连接池System.out.println("连接池的大小为"+connectionsPoll.size());}public OpenOfficeConnection getConnection() throws Exception {synchronized (connectionsPoll) {//多线程并发处理if (connectionsPoll.size() > 0) {return connectionsPoll.removeFirst();} else if (currentCount < maxCount) {//未超过最大连接数,则新建连接OpenOfficeConnection conn = createConnection();connectionsPoll.add(conn);currentCount++;return conn;} else {throw new RuntimeException("连接已经用完");}}}}
再创建一个工具类
public class OpenOfficeUtil {private static OpenSource openOfficeSource = null;public OpenOfficeUtil() {}static {try {openOfficeSource = new OpenSource();} catch (Exception e) {e.printStackTrace();}}public static OpenOfficeConnection getConnection() throws Exception {return openOfficeSource.getConnection();}public static void free(OpenOfficeConnection conn) {openOfficeSource.free(conn);}private static int office2PDF(String sourceFile, String destFile, OpenOfficeConnection connection) {try {File inputFile = new File(sourceFile);if (!inputFile.exists()) {return -1;// 找不到源文件, 则返回-1}// 如果目标路径不存在, 则新建该路径File outputFile = new File(destFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}connection.connect();DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile);return 0;} catch (Exception e) {e.printStackTrace();} finally {free(connection);}return 1;}
}
第二步:将数据转换成对应的模板信息。我利用的是github上的开源项目Word模板引擎 https://github.com/Sayi/poi-tl
具体的步骤和生成文档的方法,里面有很详细的介绍。
下面是我的模板样式:
其中带有{{xx}}为动态生成的数据。后台提供数据即可将数据写入到对应的变量中去。
第三步:生成数据到word文档
①:准备一个如上图所示的模板或者自己定义一个模板,将需要动态生成的数据用{{xx}}进行占位。
//根据word模板和相关数据生成word文档private static String dataToWord(String templatePath) throws Exception {//String templatePath = "D:\\worddata\\picc.docx";XWPFTemplate template = XWPFTemplate.compile(templatePath).render(new HashMap<String, Object>() {{put("caseNo", "订单信息");put("info", "案件信息");put("baoxian", "☑ 能繁母猪 □ 育肥猪");put("baoxian", "今天");}});String filePath = "";FileOutputStream out = new FileOutputStream("D:\\worddata\\out_template11.docx");template.write(out);out.flush();out.close();template.close();//多线程下是否需要close掉return filePath;}
如上代码便是将数据生成word的方法。输出路径为D:\\worddata\\out_template11.docx
其中map中put的key 是模板中的占位符变量。value是对应的值。
第四步:将word转换成pdf,此时要用到了openoffice工具。检查工具是否开启。
private static String officeToPdf(String sourceFile, String outPdfPath, String caseNo) throws Exception {log.info("生成的PDF的路径为:{}", outPdfPath);OpenOfficeConnection connection;File inputFile = new File(sourceFile + File.separator + caseNo + ".docx");if (!inputFile.exists()) {return null;// 找不到源文件, 则返回-1}// 如果目标路径不存在, 则新建该路径File outputFile = new File(outPdfPath + File.separator + caseNo + ".pdf");if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}//连接虚拟机上的openOfficeconnection = OpenOfficeUtil.getConnection();connection.connect();// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile);connection.disconnect();//释放连接OpenOfficeUtil.free(connection);return outPdfPath + caseNo + ".pdf";}
第五步:将pdf转成图片,图片的格式可以是png 或者jpg,图片的名字自定义。并且将下载的url返回
private String pdfToPicture(String pdfPath, String outPath, String caseNo) throws Exception {String outImagePath = "";Document document = new Document();document.setFile(pdfPath);//缩放比例float scale = 2f;//旋转角度float rotation = 0f;synchronized (this) {for (int i = 0; i < document.getNumberOfPages(); i++) {BufferedImage image = (BufferedImage)document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);RenderedImage rendImage = image;try {String imgName = i + ".png";System.out.println(imgName);File file = new File(outPath + caseNo + imgName);if (1 == i) {outImagePath = outPath + imgName;}ImageIO.write(rendImage, "png", file);} catch (IOException e) {log.error("PDF转成图片错误信息为:{}", e.getMessage());}image.flush();}}document.dispose();return outImagePath;}
生成的图片部分
具体的位置可以进行调整。生成的文字样式可以根据github上的word模板引擎进行修改。里面有很详细的介绍。
提醒建议:
一:openoffice的安装,网上百度都有,很简单的,仔细阅读github上的开源项目。
二:生成word的模板是自己定义的,文字的位置加粗斜体下划线等由POI-tl提供,类似于freemaker。但是我觉得比freemaker好用些。
三:生成的word是一页,但是pdf可能是两页,最后转换图片是根据pdf的页数进行转换的,反正最后都是会转换的。
四:最后生成的图片url可以根据自身的需求进行修改。
五:jar包可能缺乏,博主的jar是刚好的,如果有冲突或者缺少,大家可以搜一下资料进行修改。
欢迎大家提出问题。
java将数据库数据转换成word文档并且生成pdf文件最后转换成对应的图片相关推荐
- java将数据库数据转换成word文档并且生成pdf文件
目的:将数据转换成word图片的方式展现给用户 工具:openoffice +java代码 +数据库,其中openoffice有windows版本的和Linux版本的. 其中openoffice的下载 ...
- 音频文件变成html文档,C#生成音频文件以及转换成需要的格式
花了三天才做出来这么个小东西,其中碰到了好多意向不到的问题与难题,直接看代码吧还是.其中需要DotNetSpeech.dll(生成因文件)和lame_enc.dll(用于音频格式的转换)两个类库,还需 ...
- Excel如何转换成Word文档?教你如何实现转换
怎么把excel表格转换成word文档呢?对文件的格式转换相信大家都经历过很多次了,很多时候都是我们在工作中的办公需求.最近有看到有很多小伙伴想了解怎么把excel表格转换成word文档,对于这个比较 ...
- 计算机一级保存文件,计算机等级一级技巧:Word文档保存为PDF文件
全国计算机等级考试(National Computer Rank Examination,简称NCRE)是由国家教育部考试中心主办,面向社会,用于考查应试人员计算机操作.理论应用知识与技能的全国性计算 ...
- 使用PDF24Tools工具怎么将Word文档转化成PDF文件?
Word和PDF格式的文件在日常办公中都是比较常用的文件格式,两者都可以用来记录文字内容,但是有些工作场景下可能需要特殊格式的文件才能使用,这时候我们就需要进行文档格式的转换. Word文档和PDF格 ...
- mapgis明码文件转为点线面文件_手机上word文档可以转为pdf文件吗?
此前小北曾经为大家介绍了如何在电脑上转换文件的格式,例如WORD转PDF,PDF转EXCEL,PPT转PDF等等,这不,就有网友来问了,说日常办公其实很少会用到电脑,因为长期都需要在外面跑业务,很多时 ...
- 使用PHP将Word文档转化为pdf文件(用户提交数据到Word模板并修改部分内容,再将Word转为PDF)
需要实现的功能: 1.用户提交信息,例如:名称,年龄,电话等等 2.将这些信息填充到固定好的word模板中. 3.将生成的word转化为PDF. 4.使用的语言PHP 开始demo.docx文件中的内 ...
- Java Word文档如何转为PDF文件
最近做的功能要求是Java 调用ftl模板生成Word文件,再将Word文件转为PDF 1.首先是ftl模板生成Word文件.Java 利用ftl模板生成Word用的技术是freemarker,相关技 ...
- html转换成word文档没有边框,解决 apache poi 转换 word(docx) 文件到 html 文件表格没边框的问题...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ${表格匹配信息} 30 31 32 33 ...
- html怎么在图片上加文字_怎么把图片文字转换成word文档
我们在和别人交流的时候,有时候为了方便对方查看和理解,我们会使用文字图片进行交流,这是一种非常方便交流的形式.那如果我们想要将图片里的文字转换成Word文档应该怎么操作? 如果我们想要将图片识别为Wo ...
最新文章
- ssh 登陆mysql数据库_mysql命令行客户端如何通过ssh服务器连接数据库啊?
- Hinton再挖新坑:改进胶囊网络,融合Transformer神经场等研究
- 解决Chrome浏览器打开虾米音乐网页播放器时的排版问题
- 给你十年时间你可以做到吗?
- 关于格雷码的规律、转换
- 原生js获取css样式
- 201521123070 《JAVA程序设计》第13周学习总结
- PyTorch深度学习:60分钟入门(Translation)
- 工作回忆总结(第二年)
- J2EE框架搭建大集合
- php实现两张图片合成一张,如何把两张图片拼成一张
- OpenNLP进行中文命名实体识别(上:预处理及训练模型)
- 2010 年全国大学生数学建模竞赛甘肃赛区 获奖情况
- android推箱子实验报告,android开发——推箱子小游戏(前序)
- linux平台基于python语言的MYO手环手势识别开发(二)
- jira是干什么_JIRA的使用介绍(一)- 概念篇
- 2022年第十四届蓝桥杯模拟赛【核酸日期】C语言详解
- 细说微信h5棋牌游戏如何在微信中做好防封防屏蔽下载工作
- 支付服务代码设计(策略模式,可扩展,接入方便)
- QT ui添加菜单栏和工具栏