CentOS 7 安装OpenOffice并实现WordToPDF(Java调用)
2018-06-08 17:25
549 查看
1. 下载OpenOffice的安装包。
wget https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.5/binaries/zh-CN/Apache_OpenOffice_4.1.5_Linux_x86-64_install-rpm_zh-CN.tar.gz/download 2.将OpenOffice解压出来。
tar -xzvf Apache_OpenOffice_4.1.5_Linux_x86-64_install-rpm_zh-CN.tar.gz
3. 解压后会有一个文件夹生成 zh-CN 。
cd RPMS/
yum localinstall *.rpm
4.首先进入desktop-integration目录下。
cd desktop-integration/
yum localinstall openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm
5.启动OpenOffice服务。
cd /opt/openoffice4/program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
6.服务启动验证。
netstat -nlp | grep 8100 // linux
netstat -ano | findstr "3306" // window
6.下载JodConverter。
7.Java端调用代码。
8.OpenOffice格式转换中文乱码解决方案。
1、将字体拷贝到linux系统下 /usr/share/fonts
2、更新缓存 fc-cache
3、kill掉openoffice进程 ps -ef | grep openoffice kill -9 3045
4、重启后台运行openoffice soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
9.GitHub项目实例地址:https://github.com/Fillavacancy/ExportWord
wget https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.5/binaries/zh-CN/Apache_OpenOffice_4.1.5_Linux_x86-64_install-rpm_zh-CN.tar.gz/download 2.将OpenOffice解压出来。
tar -xzvf Apache_OpenOffice_4.1.5_Linux_x86-64_install-rpm_zh-CN.tar.gz
3. 解压后会有一个文件夹生成 zh-CN 。
cd RPMS/
yum localinstall *.rpm
4.首先进入desktop-integration目录下。
cd desktop-integration/
yum localinstall openoffice4.1.5-redhat-menus-4.1.5-9789.noarch.rpm
5.启动OpenOffice服务。
cd /opt/openoffice4/program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
6.服务启动验证。
netstat -nlp | grep 8100 // linux
netstat -ano | findstr "3306" // window
6.下载JodConverter。
<dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.1</version> </dependency>
7.Java端调用代码。
package com.xnck.demo; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.ConnectException; public class OfficeToPDF { /** * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为 * http://www.openoffice.org/ * <p> * <pre> * 方法示例: * String sourcePath = "F:\\office\\source.doc"; * String destFile = "F:\\pdf\\dest.pdf"; * Converter.office2PDF(sourcePath, destFile); * </pre> * * @param sourceFile 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, * .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc * @param destFile 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0, * 则表示操作成功; 返回1, 则表示转换失败 */ public static int office2PDF(String sourceFile, String destFile) throws FileNotFoundException { OpenOfficeConnection connection = new SocketOpenOfficeConnection("211.149.224.37", 8100); 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 StreamOpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); connection.disconnect(); return 0; } catch (ConnectException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.disconnect(); connection = null; } } catch (Exception e) { } } return 1; } public static void main(String[] args) { try { System.out.println(office2PDF("C:\\Users\\Gibbons\\Desktop\\合同模板.doc", "C:\\Users\\Gibbons\\Desktop\\合同模板.pdf")); } catch (FileNotFoundException e) { e.printStackTrace(); } } } |
1、将字体拷贝到linux系统下 /usr/share/fonts
2、更新缓存 fc-cache
3、kill掉openoffice进程 ps -ef | grep openoffice kill -9 3045
4、重启后台运行openoffice soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
9.GitHub项目实例地址:https://github.com/Fillavacancy/ExportWord
相关文章推荐
- java使用wkhtmltopdf实现Html转pdf,并在centos服务器部署步骤。
- java调用Apache OpenOffice实现TXT、HTML、OFFICE转PDF
- Centos5下OpenOffice的安装部署,实现office文件转换为pdf文件
- JAVA调用PageOffice实现Word文档中加盖印章
- Java通过OpenOffice将word、ppt、excel、图片、txt文档转换成PDF
- Java用OpenOffice将word转换为PDF
- iText的使用例子说明(利用Itext画模板导出word、pdf,纯java实现)
- OpenOffice2PDF,利用openoffice实现将word和excel转为pdf
- java通过调用OpenOffice将office文件转化为pdf 在线预览之步骤一
- Java用OpenOffice将word转换为PDF
- Linux/Centos环境安装配置Swftools pdf转swf 及java代码转化
- java 实现word 转PDF (采用第三方技术 IText、Poi、Jsoup)
- java调用Word中Replace的实现方法(jacob)
- Java利用OpenOffice将word等office文档转换成PDF
- java调用WPS或pdfcreator的com接口实现doc转pdf
- 求助:php 用windows COM组件调用openoffice接口实现word转pdf文件报错
- java OpenOffice把word转html(Convert word to html)
- jacob 实现Office Word文件格式转换 docTotxt,docTopdf,docxTodoc,pdfTotxt ...
- java实现MsOffice文档向pdf转化之OpenOffice软件
- JAVA调用PageOffice实现Word文档中加盖印章