openOffice + jodconverter-2.2.2将word文档转成pdf

11 篇文章 1 订阅
1 篇文章 0 订阅

直接上代码

package com.syzton.teaching.util;

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.IOException;

public class Demo {


    // 将word格式的文件转换为pdf格式
    public static void WordToPDF(String startFile, String overFile) throws IOException {
        // 源文件目录
        File inputFile = new File(startFile);
        if (!inputFile.exists()) {
            System.out.println("源文件不存在!");
            return;
        }

        // 输出文件目录
        File outputFile = new File(overFile);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }

        // 连接openoffice服务,xx.xx.xxx.xxx为启动的openOffice在linux服务器的ip
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("xx.xx.xxx.xxx", 8100);
        connection.connect();

        // 转换
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);


        converter.convert(inputFile, outputFile);

        // 关闭连接
        connection.disconnect();

        // 关闭进程
//        p.destroy();
    }
    public static void main(String[] args) {
        String start = "D:\\demo.docx";
        String over = "D:\\成了.pdf";
        try {
            WordToPDF(start, over);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

pom依赖

<!-- https://mvnrepository.com/artifact/org.openoffice/juh -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>juh</artifactId>
			<version>3.0.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/jurt -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>jurt</artifactId>
			<version>3.0.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/ridl -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>ridl</artifactId>
			<version>3.0.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/unoil -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>unoil</artifactId>
			<version>3.0.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.4</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-core -->
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-core</artifactId>
			<version>4.2.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-local</artifactId>
			<version>4.2.2</version>
		</dependency>

		<dependency>
			     <groupId>org.slf4j</groupId>
			     <artifactId>slf4j-jdk14</artifactId>
			<version>1.5.6</version>
		</dependency>

jar包下载jodconverter-2.2.2.jar包下载,下载后解压,在jodconverter-2.2.2\lib里面有所有的包。把jodconverter-2.2.2.jar和jodconverter-cli-2.2.2.jar导入项目。

这时候结合你已安装的openOffice已经可以转了,但是会有乱码问题,下载字体字体下载,解压后将里面的文件上传到/usr/share/fonts,然后执行fc-cache更新缓存,然后杀死openOffice进程,然后执行 soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard & 重启openOffice

ok!

linux安装openOffice请参考https://blog.csdn.net/github_38924695/article/details/89470960

启动时用这个命令,后台启动

/opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &

设置自启动,未验证,未知是否可行

vi /etc/rc.local
#添加如下行
/opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &

解决Linux 上openoffice崩溃后不再运行问题

首先在/opt/openoffice4/program路径(路径可自己定义)下编写runOpenOffice.sh脚本

[root@iZ2ze38tf0alwrqj7eZ program]# vim runOpenOffice.sh


#!/usr/bin/bash

OPENOFFICEPID=`ps -ef|grep "/opt/openoffice4/program/soffice"|grep -v grep|grep -v grep|awk '{print $2}'`

if [ ! -n "$OPENOFFICEPID" ];then
    echo "OPENOFFICEPID is empty"
    echo "start openoffice"
    nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;StarOffice.Service" -nofirststartwizard &
else
    echo "OPENOFFICEPID is $OPENOFFICEPID is started!"
fi

赋权

[root@iZ2ze38tf0alwrqj7eZ program]# chmod  a+x runOpenoffice.sh

编辑crontab文件

[root@localhost ~]# cd /var/spool/cron
[root@localhost cron]# crontab  -e
编辑内容如下
* * * * *  /root/runOpenoffice.sh

验证:
查看当前openoffice运行情况
[root@localhost cron]# ps -ef|grep 8100
将openoffice所有进程杀掉
ps -ef|grep “/opt/openoffice4/program/soffice”|grep -v grep|grep -v grep|awk ‘{print $2}’ | xargs kill
大概一分钟后 运行下面命令查看脚本是否执行
[root@localhost cron]# ps -ef|grep 8100

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值