openoffice docx转pdf 19-01-24

实测doc 转 docx是有问题的

先上代码

public static void main( String[] args ) throws IOException
    {
    	//读入文件流
    	FileInputStream in = new FileInputStream("D:/template/8ae48678686e1f7a01686eee6878003e.docx");
    	//输出文件流
    	FileOutputStream out = new FileOutputStream("D:/template/8ae48678686e1f7a01686eee6878003e.pdf");
    	//创建openoffice连接
    	OpenOfficeConnection connection = new SocketOpenOfficeConnection("*.*.*.*",8100);
    	//连接
    	connection.connect();
    	//获取格式工厂
    	DocumentFormatRegistry factory = new DefaultDocumentFormatRegistry();
        //产生输入格式
        DocumentFormat inputDocumentFormat = factory.getFormatByFileExtension("docx");
        //产生输出格式
        DocumentFormat outputDocumentFormat = factory.getFormatByFileExtension("pdf");
		
    	DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
    	//从输入流转换到输出流
    	converter.convert(in, inputDocumentFormat, out, outputDocumentFormat);
    	//关闭连接
    	connection.disconnect();
    	out.flush();
        in.close();
        out.close();
    }

在web程序中,使用流来避免读写硬盘,因为是个demo,所以这里用的是文件流,方法原型为

void com.artofsolving.jodconverter.DocumentConverter.convert(InputStream arg0, DocumentFormat arg1, OutputStream arg2, DocumentFormat arg3); 即支持读入InputStream输出到OutputStream

其中如果SocketOpenOfficeConnection 连接的是远程服务器,远程服务器启动openoffice服务时必须用真实ip不能使用127.0.0.1

否则会被服务器拒绝连接。

转换后缀为x的opffice文件,如docx、xlsx...等 需要jodconverter版本为2.2.2以上(好像没有以上了)。

支持的格式在 DefaultDocumentFormatRegistry种可以看到。

也支持对文件转换,转换文件是可以不用DocumentFormat 也可以使用,使用时DocumentFormatRegistry 实现为BasicDocumentFormatRegistry,这里面的方法貌似是空的。

DocumentConverter 实现有StreamOpenOfficeDocumentConverter,OpenOfficeDocumentConverter,大概看了一下,前者处理流,后者处理文件

 public static void main( String[] args ) throws IOException
    {
    	//读入文件流
//    	FileInputStream in = new FileInputStream("D:/template/8ae48678686e1f7a01686eee6878003e.docx");
//    	//输出文件流
//    	FileOutputStream out = new FileOutputStream("D:/template/8ae48678686e1f7a01686eee6878003e.pdf");
    	
    	File in = new File("D:/template/8ae48678686e1f7a01686eee6878003e.docx");
    	
    	File out = new File("D:/template/8ae48678686e1f7a01686eee6878003e.pdf");
    	
    	//创建openoffice连接
    	OpenOfficeConnection connection = new SocketOpenOfficeConnection("*.*.*.*",8100);
    	//连接
    	connection.connect();
    	//获取格式工厂
//      DocumentFormatRegistry factory = new DefaultDocumentFormatRegistry();
    	//产生输入格式
//      DocumentFormat inputDocumentFormat = factory.getFormatByFileExtension("docx");
        //产生输出格式
//      DocumentFormat outputDocumentFormat = factory.getFormatByFileExtension("pdf");
		
    	//DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
    	
    	DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    	//从输入文件换到输出
    	converter.convert(in, out);
    	//关闭连接
    	connection.disconnect();
//    	out.flush();
//      in.close();
//      out.close();
    }

pom.xml 依赖

    <!-- 有用 -->
	<dependency>
      <groupId>com.artofsolving</groupId>
      <artifactId>jodconverter</artifactId>
      <version>2.2.2</version>
    </dependency> 
    <!-- 有用 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>1.5.6</version>
    </dependency>
    <!-- 有用 -->
    <dependency>
	    <groupId>org.openoffice</groupId>
	    <artifactId>juh</artifactId>
	    <version>4.1.2</version>
	</dependency>
	<!-- 有用 -->
	<dependency>
	    <groupId>org.openoffice</groupId>
	    <artifactId>unoil</artifactId>
	    <version>4.1.2</version>
	</dependency> 
	<!-- 有用 -->
	<dependency>
	    <groupId>commons-io</groupId>
	    <artifactId>commons-io</artifactId>
	    <version>2.4</version>
	</dependency>

真实依赖

最后附上openpffice的安装

1、压缩文件传到服务器

2、执行

tar -xzvf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz

解压

3、切换到解压好的目录下 的文件夹RPMS

执行 rm -rf ./openoffice-gnome-integration-4.1.6-9788.x86_64

再执行 rpm -ivh *.rpm

4、如果不修改安装路径,默认安装在/opt下

cd /opt/openoffice4/program下

5执行

./soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

主机127.0.0.1 端口8100

 

如果远程访问,如10.100.6.248 需要用真实ip启动,开发用

./soffice -headless -accept="socket,host=10.100.6.248,port=8100;urp;" -nofirststartwizard & 

6、检验是否启动成功

lsof -i:8100 查看8100端口是否被openoffice监听

7、中央仓库没有2.2.2 自己找一个装上去

mvn install:install-file -DgroupId=com.artofsolving -DartifactId=jodconverter -Dversion=2.2.2 -Dpackaging=jar -Dfile=C:\Users\Administrator\Downloads\jodconverter\jodconverter\jodconverter-2.2.2.jar

发布了16 篇原创文章 · 获赞 4 · 访问量 2033

猜你喜欢

转载自blog.csdn.net/qq_36592473/article/details/86602602
今日推荐