使用openoffice将文件转为pdf

一.安装

1、Windows版

十分简单,360软件管家就能下载安装。

2.LInux版

安装也比较简单,可以参考:linux中安装openoffice

启动命令:/opt/openoffice4/program/soffice -headless -accept= "socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

二、jar包的问题

说明:Windows和Linux部署的openoffice,代码中所使用的jar包和代码是不同的,不能统一使用。

导入openoffice的maven依赖:

        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.2.1</version>
        </dependency>

1.Windows版

添加maven依赖:

<dependency>
    <groupId>org.artofsolving.jodconverter</groupId>
    <artifactId>jodconverter-core</artifactId>
    <version>3.0-beta-4</version>
</dependency>

这个包maven引入不了,可以下载下jar包,导入到maven库中:

在jar包同一文件目录下建一个txt文件写入如下内容,然后将txt改成bat文件,双击执行即可导入maven库,依赖便可以导入。

mvn install:install-file -DgroupId=org.artofsolving.jodconverter -DartifactId=jodconverter-core -Dversion=3.0-beta-4 -Dpackaging=jar -Dfile=jodconverter-core-3.0-beta-4.jar

 

2.LInux版

添加maven依赖:

<dependency>
   <groupId>com.artofsolving</groupId>
   <artifactId>jodconverter</artifactId>
   <version>2.2.2</version>
</dependency>

这个包maven只能引入2.2.1版的,2.2.1的包不要使用!因为它不支持docx、xlsx、pptx,遇到这些文件会报错找不到文件。

2.2.2的maven无法加载,也是先下载jar包,导入到maven库中:

在jar包同一文件目录下建一个txt文件写入如下内容,然后将txt改成bat文件,双击执行即可导入maven库,依赖便可以导入。

mvn install:install-file -DgroupId=com.artofsolving -DartifactId=jodconverter -Dversion=2.2.2 -Dpackaging=jar -Dfile=jodconverter-2.2.2.jar

 三、java代码

Windows和Linux的是不同的

private void doc2pdf(String docFilePath, String pdfFilePath) throws Exception {

        File docFile = new File(docFilePath);
        File pdfFile = new File(pdfFilePath);
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
        try {
            connection.connect();
            // DocumentConverter converter = new OpenOfficeDocumentConverter(connection);       / /   windows下运用
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); //  linux下运用
            if (extName.equals("txt") || extName.equals("TXT")) {
                converter.convert(odtFile, pdfFile);
            } else
                converter.convert(docFile, pdfFile);
            // 关闭连接
            connection.disconnect();
            System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
        } catch (java.net.ConnectException e) {
            e.printStackTrace();
            System.out.println("****swf转换器异常,openoffice服务未启动!****");
            throw e;
        } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
            e.printStackTrace();
            System.out.println("****swf转换器异常,读取转换文件失败****");
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }


    }

四、转pdf中文乱码

1.在使用中我发现一个ppt转的pdf中,一些中文无法识别,显示的都是一个个框框,我发现能转的和不能转的字体不同,有个宋体,有的黑体。

2.解决方案

是因为linux中openoffice支持的字体比较少,只需要将windows中office的字体复制进来就行了。

先查看linux上openoffice的字体在什么位置:

cat /etc/fonts/fonts.conf

就可以看到在/usr/share/fonts下

windows中office的字体在C:\Windows\Fonts下面

只需要将Fonts整个文件夹放到 /usr/share/fonts下就可以了。

然后更新缓存:fc-cache

重启openoffice

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

五,卸载:

rpm -e `rpm -qa |grep openoffice` `rpm -qa |grep ooobasis`

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值