doc转pdf,(利用openoffice转换,linux的中文乱码问题)

doc转pdf,(利用openoffice转换,linux的中文乱码问题)

一、主流思路

目前doc转pdf,主要有这么几个思路,要么就是odconverter来调用openOffice或者libreoffice的服务来转换,要么就是调用第三方的jar包,不依赖本地服务。

没钱,所以就选了第一种思路。

二、代码实现

1.依赖

      <!--openoffice,这一大串,好像都是-->
        <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>ridl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.xdocreport.document</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
            <version>1.0.6</version>
        </dependency>

2 .代码

    public static void word7Pdf(String startFile, String overFile) throws IOException {
        // 源文件目录
        File inputFile = new File(startFile);
        if (!inputFile.exists()) {
            return;
        }
        // 输出文件目录
        File outputFile = new File(overFile);
        //如果输出目录不存在,则创建目录
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }

        //载入服务配置信息
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(OpenOfficeConfigure.host, OpenOfficeConfigure.port);
        boolean flag = true;
        long start = System.currentTimeMillis();
        long end;
        int num = 0;
        //连接服务失败时循环启动服务
        while (flag) {
            try {
                // 连接openoffice服务
                connection.connect();
                flag = false;
            } catch (ConnectException e) {
                logger.error(e.getMessage());
                if ((num == 0) || (num == 1000)) {
                    getProcess();
                }
                num++;
            }
            //设置两秒连接时间
            end = System.currentTimeMillis();
            if (end - start > 2000) {
                flag = false;
            }
        }

        // 转换
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile);
        connection.disconnect();
    }

OpenOfficeConfigure是自己写的一个配置类

@Component
public class OpenOfficeConfigure {
    public static final String command="/opt/openoffice4/program/soffice -headless - accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard &";
    public static String host="127.0.0.1";
    public static int port=8100;
    public static String posyspath="/opt/openoffice4/program/soffice";


    public static String getCommand() {
        return command;
    }

    public static String getHost() {
        return host;
    }

    public static void setHost(String host) {
        OpenOfficeConfigure.host = host;
    }

    public static int getPort() {
        return port;
    }

    public static void setPort(int port) {
        OpenOfficeConfigure.port = port;
    }

    public static String getPosyspath() {
        return posyspath;
    }


这里的配置,如果是springboot的话,可以不用写死,用@configurate或者@value来读取yml配置也行。

代码的思路也很简单,就调用本地服务。然后转化文件。

三、本地服务安装

根据上面的思路来说,代码根本不重要,重要的是,本地有转pdf的服务,因此我们来linux安装下openoffic。

1. 下载rmp包

http://www.openoffice.org/download/index.html

2.上传rpm包

通过xftp上传到linux中。我的目录在/opt/openoffice中,没有就新建 mkdir openoffice。上传到这个目录

3.安装

进入到这个目录,解压文件:tar -zxvf Apache_OpenOffice_4.1.7_Linux_x86-64_install-rpm_zh-CN.tar.gz。解压后进入zh-CN目录中。

进入RPMS,cd RPMS/ 里面都是rpm文件,我们需要安装这些文件,安装rpm文件: rpm -ivh *.rpm。 进入desktop-integration/目录:cd desktop-integration/。 安装rpm -ivh openoffice4.1.7-redhat-menus-4.1.7-9800.noarch.rpm安装成功后会在/opt下出现一个openoffice4文件。

4.启动

/opt/openoffice4/program/soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard 临时启动
/opt/openoffice4/program/soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard & 后台启动

5.查看状态

netstat -lnp |grep 或者 ps -ef|grep soffice
删除openoffice
在program文件夹下执行:rpm -erpm -qa |grep openofficerpm -qa |grep ooobasis。

四、运行时错误解决

运行转换,会发现中文都是乱码,因为英文系统的Linux 内字体缺少,而对应需要转换的word中字体有多种不同类型字体,转换时无法对应。

解决方案:把windows的字体转到liinux。

讲C:\Windows\Fonts的所有文件,通过文件上传传到linux的/usr/share/fonts。

然后刷新缓存,fc-cache。重启openoffice。ps -ef | grep openoffice 杀掉后重启

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值