关于Lunix安装Libreoffice用于Word转PDF的详细步骤及问题

一、背景

        由于项目中遇到word转pdf需求,在过程中对比Aspose.Words和Libreoffice,由于前者收费,故此选择开源的后者,测试环境中一切顺利,但正式环境遇到很多问题,目前已经顺利解决,故此想通过此篇文章,总结下详细的步骤和我遇到的问题,希望可以对其他人所有帮助。

二、资源下载(多种方式)

1、命令下载

(1)指令:我这里用到的是Lunix CentOs7系统,所以是yum。如果你是CentOs7以上系统则需使用dnf,若是Ubuntu系统则需使用apt,具体根据实际环境来。查看系统环境可使用【cat /etc/os-release】命令进行查看

sudo yum update
sudo yum install libreoffice

(2)问题:输入命令报错“网络不可达”,此问题说明受限国外镜像资源无法访问,导致安装失败

(3)解决方法:修改镜像资源文件地址,我这里使用【vim /etc/yum.repos.d/CentOS-Base.repo】命令,将其中的地址替换为阿里的镜像资源地址,你也可以利用其他资源,例如清华、中科大、腾讯登镜像资源。

#阿里云镜像资源
[base]
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64/
[updates]
baseurl=http://mirrors.aliyun.com/centos/7/updates/x86_64/
[extras]
baseurl=http://mirrors.aliyun.com/centos/7/extras/x86_64/
[centosplus]
baseurl=http://mirrors.aliyun.com/centos/7/centosplus/x86_64/

(4)总结:替换完镜像资源地址后,再利用第一步的命令进行安装,即可成功。若没有成功或者成功后使用还有问题,请继续向下阅读。

2、官网下载

1、中文下载地址:下载 LibreOffice | LibreOffice 简体中文官方网站 - 自由免费的办公套件,建议使用稳定版本。

2、历史版本:可在官网的“存档下载中心”中下载,或者百度搜索其他资源。

3、选择版本:我这里选择的是7.4.5.1,有需要的可以访问:Index of /libreoffice/old/7.4.5.1/rpm/x86_64

(1)下载rpm.tar.gz

(2)下载zh-CN.tar.gz

4、上传服务器:将下载好的文件,通过xftp上传到服务器目录下,自选目录,我这里上传的是/opt

5、解压安装:进入/opt目录,通过解压命令,解压后安装,注意安装命令不是install而是localinstall,如果安装过程中出现上述“网络不可达”情况,请将镜像资源替换为国内镜像资源,入上述阿里云镜像资源。

tar -zxvf LibreOffice_7.4.5.1_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
tar -zxvf LibreOffice_7.4.5.1_Linux_x86-64_rpm.tar.gz
	
cd /LibreOffice_7.4.5.1_Linux_x86-64_rpm/RPMS/
yum localinstall *.rpm
cd /LibreOffice_7.4.5.1_Linux_x86-64_rpm_langpack_zh-CN/RPMS/
yum localinstall *.rpm

6、安装解压后:/opt目录下会出现相应文件夹,如下图:

7、配置环境变量

#1、打开编辑文件
vim /etc/profile
#2、在文件底部添加
# libreoffice
export LibreOffice_PATH=/opt/libreoffice7.4/program
export PATH=$LibreOffice_PATH:$PATH
#3、刷新生效
source /etc/profile

8、校验:校验安装是否成功,需输入如下命令。

#libreoffice 7版本之前
libreoffice -version
#或者
libreoffice --help
#我这里安装的是7.4.5.1
libreoffice7.4 -version
#或者
libreoffice7.4 --help

出现以下情况,即说明安装成功:

 

9、word转pdf 命令

#libreoffice7.4 --invisible --convert-to pdf --outdir 转换后存放目录 源文件目录
示例:libreoffice7.4 --invisible --convert-to pdf --outdir /opt/pdfFile /opt/wordFile/xgyj.docx
或者
#libreoffice7.4 --headless --invisible --convert-to pdf 源文件目录 --outdir 转换后存放目录
示例:libreoffice7.4 --headless --invisible --convert-to pdf /opt/wordFile/xgyj.docx --outdir /opt/pdfFile

三、安装问题及解决方案

1、oosplash问题

        出现此类问题,说明安装过程中丢失了libXinerama.so.1文件,此文件又有很多依赖文件。

/opt/libreoffice7.4/program/oosplash: error while loading shared libraries: libXinerama.so.1: cannot open shared object file: No such file or directory

2、soffice问题

        出现此类问题,说明安装过程中丢失libcairo.so.2文件,此文件又有很多依赖文件。

/opt/libreoffice7.5/program/soffice.bin: /lib64/libdbus-1.so.3: no version information available

3、解决方案

        上述问题,此处整理了一份需要安装的依赖,下载后可上传到自定义目录下,我这里还是放在/opt中。使用【yum localinstall *.rpm】进行安装,安装过程中出现“网络不可达”情况,依旧需要切换镜像资源。

avahi-libs-0.6.31-20.el7.x86_64.rpm
cairo-1.15.12-4.el7.x86_64.rpm
cups-libs-1.6.3-51.el7.x86_64.rpm
fontconfig-2.13.0-4.3.el7.x86_64.rpm
libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
libICE-1.0.9-9.el7.x86_64.rpm
libSM-1.2.2-2.el7.x86_64.rpm
libX11-1.6.7-2.el7.x86_64.rpm
libXau-1.0.8-2.1.el7.x86_64.rpm
libxcb-1.13-1.el7.x86_64.rpm
libXext-1.3.3-3.el7.x86_64.rpm
libXinerama-1.1.3-2.1.el7.x86_64.rpm
libXrender-0.9.10-1.el7.x86_64.rpm
libpng15-1.5.30-7.el8.x86_64.rpm

上述依赖资源下载:这里得感谢一位老哥的整理,链接:https://pan.baidu.com/s/1kodG6JwHB9_cSo1nRsoWKw
提取码:6666

        全部安装完成后,再次进行校验,校验成功即可。

【注意】在校验成功后,执行转换命令进行尝试,如果出现(Error: source file could not be loaded)则需要先解决下述中文字体编码,和libreoffice-writer的安装问题。如果出现“网络不可达”情况,请切换镜像资源,具体命令为:yum install libreoffice-writer

四、中文字体编码问题及解决方案

1、添加字体

        如果服务器中无所需字体,可将本地win下的C:\Windows\fonts中的字体压缩个.zip文件上传到服务器的/usr/share/fonts目录下解压。因为是zip包,所以需要用到unzip命令,如果没有安装,可通过一下命令安装。

#安装命令
yum -y install unzip
#解压文件
unzip file.zip

安装解压后,授权Fonts文件夹:

chmod -R 755 /usr/share/fonts/Fonts

再安装ttmkfdir

yum -y install ttmkfdir
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

上述完成后,编辑/etc/fonts/fonts.conf文件,添加字体位置

#打开文件编辑
vim /etc/fonts/fonts.conf
#添加到文件
<dir>/usr/share/fonts/Fonts</dir>

结束后,刷新字体:

fc-cache

2、查询系统中文环境

        利用命令【locale】,查看,确保中文环境和编码格式一致即可

 如果不是中文编码,可通过以下命令设置:

export LANG=zh_CN.UTF-8

五、代码示例

package com.iot.util;


import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @ClassName WordToPdf
 * @Description word转pdf
 */
@Slf4j
public class WordToPdfUtil {

    public static String wordConvertPdf(String wordPath, String pdfPath,String pdfFileName){
        String url = "";
        // 获取当前系统名称
        String osName = System.getProperty("os.name").toLowerCase();
        System.out.println("系统名称:"+osName);
        // 根据系统选择执行方法
        if (osName.contains("win")) {
            url = winWordToPdf(wordPath, pdfPath,pdfFileName);
        } else if (osName.contains("nux") || osName.contains("nix")) {
            url = linuxWordToPdf(wordPath, pdfPath,pdfFileName);
        }
        return url;
    }
    /**
     *
     * @param wordPath          word 文件路径
     * @param pdfPath           pdf 输出路径
     * @throws IOException
     */
    public static String winWordToPdf(String wordPath, String pdfPath,String pdfFileName){
        String pdfSavePath = "";
        try{
            InputStream wordInputStream = Files.newInputStream(Paths.get(wordPath));
            // 转成 pdf
            File file = new File(pdfPath+ File.separator + pdfFileName);
            if (!file.getParentFile().exists()) { // 如果父目录不存在,创建父目录
                file.getParentFile().mkdirs();
            }
            if (file.exists()) { // 如果已存在,删除旧文件
                file.delete();
            }
            file.createNewFile();
            OutputStream outputStream = Files.newOutputStream(Paths.get(pdfPath+ File.separator + pdfFileName));
            IConverter converter = LocalConverter.builder().build();
            converter.convert(wordInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            converter.shutDown();
            pdfSavePath = "/downloadPdf" + File.separator + pdfFileName;
            wordInputStream.close();
            outputStream.close();
            System.out.println("pdf文档路径:" + pdfPath + File.separator + pdfFileName);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("word转pdf异常!");
        }


        return pdfSavePath;
    }

    /**
     * linux系统word转pdf
        * 使用LibreOffice转换。系统需安装LibreOffice
        * 转换命令 libreoffice --invisible --convert-to pdf --outdir output_dir source_path
        * 转换后的pdf文件名使用的是源文件的名称,所以如果要指定输出文件名称,就需把源文件名称改成想要输出的名称
     */
    public static String linuxWordToPdf(String wordPath, String pdfPath,String pdfFileName){
        String pdfSavePath = "";
        // 执行转换命令
        try {
            // 待转换的word文件
            File wordFile = new File(wordPath);
            // 转换后的pdf文件
            // 转成 pdf
            File pdfFile = new File(pdfPath+ File.separator + pdfFileName);
            if (!pdfFile.getParentFile().exists()) { // 如果父目录不存在,创建父目录
                pdfFile.getParentFile().mkdirs();
            }
            if (pdfFile.exists()) { // 如果已存在,删除旧文件
                pdfFile.delete();
            }
            pdfFile.createNewFile();

            // 获取word文件的绝对路径
            String sourcePath = wordFile.getAbsolutePath();
            // 获取pdf文件存放文件夹的绝对路径
            String outDir = pdfFile.getAbsolutePath().substring(0, pdfFile.getAbsolutePath().lastIndexOf(File.separator));
            // 构建LibreOffice的命令行工具命令
            //线上环境libreoffice版本是7.4
            String command = "libreoffice7.4 --invisible --convert-to pdf --outdir " + outDir + " " + sourcePath;
            //测试环境libreoffice版本是6.4
//            String command = "libreoffice --invisible --convert-to pdf --outdir " + outDir + " " + sourcePath;
            System.out.println("pdf文件存放文件夹的绝对路径:"+outDir);
            log.info(command);
            executeLinuxCmd(command);
            pdfSavePath = "/downloadPdf" + File.separator + pdfFileName;
            System.out.println("pdf返回路径:"+pdfSavePath);
        } catch (Exception e) {
            log.error("linuxWordToPdf linux环境word转换为pdf时出现异常:", e);
        }
        return pdfSavePath;
    }

    /**
     * 执行命令行
     *
     * @param cmd 命令行
     * @return
     */
    private static boolean executeLinuxCmd(String cmd)  {
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            process.waitFor();
        } catch (InterruptedException e) {
            log.error("executeLinuxCmd 执行Linux命令异常:", e);
            Thread.currentThread().interrupt();
            return false;
        } catch (IOException e) {
            log.error("获取系统命令执行环境异常", e);
        }
        return true;
    }

}

 六、案例

        此示例用poi生成word后转pdf,打印用的浏览器自带打印功能。

注:以上内容如有错误,欢迎大家指正,提出意义。转载请标注出处 谢谢。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值