Java使用openOffice对于word的转换及遇到的问题解决

时间:2021-05-20

一:需求详情:

OpenOffice.org 是一套跨平台的办公室软件套件,能在 Windows、Linux、MacOS X (X11)、和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用、及推广它。

公司需要存储合同文件,用户上传word文档的合同,通过openOffice去把word转换为pdf、再把pdf转换为图片格式,并分别存储。因为openOffice的转换需要耗费挺大的内存,所以设计为task任务,凌晨自动转换。

记录本次需求完成的时候遇到的问题。

openoffice既有windows版本也有linux版。不用担心生产环境是linux系统。

关于linux系统安装openoffice软件请参照:点击这里

二:过程

1:本地环境编码(windows)

第一步:因为是本地环境的编码而且是Windows环境,所以从安装openOffice开始,到启动服务并没有遇到难题。

第二步:转换所需要的工具包;

<dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>juh</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>jurt</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>ridl</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.openoffice</groupId> <artifactId>unoil</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>fontbox</artifactId> <version>2.0.8</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.8</version> </dependency>

问题1:在这里遇到了第一个问题,就是在maven的中央仓库找不到关键的依赖jar包的问题。

jodconverter-cli 这个jar包中央仓库找不到jar包依赖,jodconverter 版本才到2.2.1(这个版本之前的不能支持docx格式转换,2.2.2及以后才开始支持。)

然后和大牛商量,加入到公司内网自己的maven仓库。

第三步:工具类

/** * @author GH * 输入文件 * 输出文件 */ public class WordToPdf {//word转pdf public static void docToPdf(File inputFile, File outputFile){ OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try{ connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); }catch(ConnectException cex){ cex.printStackTrace(); }finally{ if(connection!=null){ connection.disconnect(); connection = null; } } } } /** * @author GH * 参数1:要装换的pdf位置 * 参数2:转换后的图片存放位置 * 参数3:中间要拼接的名字 * return:转换后的img名字集合 */ public class PdfToImage {//pdf转img public static List<String> pdfToImagePath(String srcFile,String contractFromSrc,String name){ List<String> list = new ArrayList<>(); String imagePath; File file = new File(srcFile); try { File f = new File(contractFromSrc); if(!f.exists()){ f.mkdir(); } PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for(int i=0; i<pageCount; i++){ // 方式1,第二个参数是设置缩放比(即像素) // BufferedImage image = renderer.renderImageWithDPI(i, 296); // 方式2,第二个参数是设置缩放比(即像素) BufferedImage image = renderer.renderImage(i, 2f); //第二个参数越大生成图片分辨率越高,转换时间也就越长 imagePath = contractFromSrc+name+"-"+i +".jpg"; ImageIO.write(image, "PNG", new File(imagePath)); list.add(name+"-"+i +".jpg"); } doc.close(); } catch (IOException e) { e.printStackTrace(); } return list; } }

第四步:编码

首先从数据库读取没有转换过的集合,循环下载oss对象存储文件到指定临时文件夹。

通过工具类转换下载的word为pdf,录入数据pdf记录,上传oss对象pdf图片。

通过工具类转换得到的pdf图片,录入数据路图片记录,上传转换得到的img图片。

try catch捕捉异常,有异常就回滚数据库,删除oss对象上传的文件。

修改word的转换状态为已转换。

问题2:因为到最后测试环境和生产环境都是Linux系统的,因为涉及到文件的操作,但是Linux和Windows的文件路径是不一样的,例如:Windows文件路径为(C:\tmp\test.txt)Linux则为(/tmp/test.txt)

因此 采用这种方式

  public final static String Convert_Tmp_Url="C:"+File.separator+"temp"+File.separator+"contractToImg"+File.separator;//进行word——img转换的时候的暂时存放路径 window public final static String Convert_Tmp_Url2=File.separator+"tmp"+File.separator+"contractToImg"+File.separator;//进行word——img转换的时候的暂时存放路径 linux

File.separator 与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串 在Linux此字段的值为 '/' Windows为'\'

第五步:本地测试,没有问题。

2:测试环境测试(windows)

问题3:在Linux环境下word转换word中文出现乱码 空白,导致的原因是Linux缺少中文字体编码。

解决方法:

步骤1:创建路径。

在centos的/usr/java/jdk1.8.0_91/jre/lib/fonts下新建路径:fallback。

步骤2:上传字体。

将字体:simhei.ttf 黑体、simsun.ttc 宋体(windows下通过everything找下)上传至/usr/java/jdk1.8.0_91/jre/lib/fonts/fallback路径下。

步骤3:查看系统字体文件路径。

查看方案:

[root@80ec6 fallback]# cat /etc/fonts/fonts.conf<!-- Font directory list --><dir>/usr/share/fonts</dir><dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF</dir> <dir>/usr/local/share/fonts</dir><dir>~/.fonts</dir>

步骤4:字体拷贝。

将 /usr/java/jdk1.8.0_91/jre/lib/fonts的全部内容,拷贝到步骤3查看的路径下, 我的字体路径为:/usr/share/fonts。

步骤5:更新缓存

执行命令:fc-cache

步骤6:kill掉openoffice进程。

  [root@80ec6 fonts]# ps -ef | grep openoffice  root 3045 3031 0 06:19 pts/1 00:00:03 /opt/openoffice4/program/soffice.bin -headless -accept=socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard

执行kill:kill -9 3045

步骤7:重启后台运行openoffice。

 [root@a3cf78780ec6 openoffice4]# soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

3:测试环境和生产环境内核不一样,安装的安装包不一样。

测试环境的安装的是deb文件,使用 dpkg命令安装所有的deb文件,启动服务就能使用。

生产环境的是dpkg命令找不到。改换安装prm文件,执行安装之后,竟然启动不了,查找原因之后尽然是没有安装完,RPMS目录下有desktop-integration文件夹,进入到desktop-integration目录,里面有四个rpm  文件,选择相应的安装即可,这里我选择的是redhat版本。

执行 rpm -ivh openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章